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.56 by jsr166, Sat Feb 28 19:59:23 2015 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  
# Line 35 | Line 47 | public class LinkedBlockingDequeTest ext
47      }
48  
49      /**
50 <     * Create a deque of given size containing consecutive
50 >     * Returns a new deque of given size containing consecutive
51       * Integers 0 ... n.
52       */
53      private LinkedBlockingDeque<Integer> populatedDeque(int n) {
# Line 80 | Line 92 | public class LinkedBlockingDequeTest ext
92      }
93  
94      /**
95 <     * offer(null) throws NPE
95 >     * offerFirst(null) throws NullPointerException
96       */
97      public void testOfferFirstNull() {
98 +        LinkedBlockingDeque q = new LinkedBlockingDeque();
99          try {
87            LinkedBlockingDeque q = new LinkedBlockingDeque();
100              q.offerFirst(null);
101              shouldThrow();
102          } catch (NullPointerException success) {}
103      }
104  
105      /**
106 +     * offerLast(null) throws NullPointerException
107 +     */
108 +    public void testOfferLastNull() {
109 +        LinkedBlockingDeque q = new LinkedBlockingDeque();
110 +        try {
111 +            q.offerLast(null);
112 +            shouldThrow();
113 +        } catch (NullPointerException success) {}
114 +    }
115 +
116 +    /**
117       * OfferFirst succeeds
118       */
119      public void testOfferFirst() {
# Line 253 | Line 276 | public class LinkedBlockingDequeTest ext
276       */
277      public void testRemoveFirstOccurrence() {
278          LinkedBlockingDeque q = populatedDeque(SIZE);
279 <        for (int i = 1; i < SIZE; i+=2) {
279 >        for (int i = 1; i < SIZE; i += 2) {
280              assertTrue(q.removeFirstOccurrence(new Integer(i)));
281          }
282 <        for (int i = 0; i < SIZE; i+=2) {
282 >        for (int i = 0; i < SIZE; i += 2) {
283              assertTrue(q.removeFirstOccurrence(new Integer(i)));
284              assertFalse(q.removeFirstOccurrence(new Integer(i+1)));
285          }
# Line 268 | Line 291 | public class LinkedBlockingDequeTest ext
291       */
292      public void testRemoveLastOccurrence() {
293          LinkedBlockingDeque q = populatedDeque(SIZE);
294 <        for (int i = 1; i < SIZE; i+=2) {
294 >        for (int i = 1; i < SIZE; i += 2) {
295              assertTrue(q.removeLastOccurrence(new Integer(i)));
296          }
297 <        for (int i = 0; i < SIZE; i+=2) {
297 >        for (int i = 0; i < SIZE; i += 2) {
298              assertTrue(q.removeLastOccurrence(new Integer(i)));
299              assertFalse(q.removeLastOccurrence(new Integer(i+1)));
300          }
# Line 298 | Line 321 | public class LinkedBlockingDequeTest ext
321          assertSame(four, q.peekLast());
322      }
323  
301
324      /**
325       * A new deque has the indicated capacity, or Integer.MAX_VALUE if
326       * none given
# Line 309 | Line 331 | public class LinkedBlockingDequeTest ext
331      }
332  
333      /**
334 <     * Constructor throws IAE if capacity argument nonpositive
334 >     * Constructor throws IllegalArgumentException if capacity argument nonpositive
335       */
336      public void testConstructor2() {
337          try {
338 <            LinkedBlockingDeque q = new LinkedBlockingDeque(0);
338 >            new LinkedBlockingDeque(0);
339              shouldThrow();
340          } catch (IllegalArgumentException success) {}
341      }
342  
343      /**
344 <     * Initializing from null Collection throws NPE
344 >     * Initializing from null Collection throws NullPointerException
345       */
346      public void testConstructor3() {
347          try {
348 <            LinkedBlockingDeque q = new LinkedBlockingDeque(null);
348 >            new LinkedBlockingDeque(null);
349              shouldThrow();
350          } catch (NullPointerException success) {}
351      }
352  
353      /**
354 <     * Initializing from Collection of null elements throws NPE
354 >     * Initializing from Collection of null elements throws NullPointerException
355       */
356      public void testConstructor4() {
357 +        Collection<Integer> elements = Arrays.asList(new Integer[SIZE]);
358          try {
359 <            Integer[] ints = new Integer[SIZE];
337 <            LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
359 >            new LinkedBlockingDeque(elements);
360              shouldThrow();
361          } catch (NullPointerException success) {}
362      }
363  
364      /**
365 <     * Initializing from Collection with some null elements throws NPE
365 >     * Initializing from Collection with some null elements throws
366 >     * NullPointerException
367       */
368      public void testConstructor5() {
369 +        Integer[] ints = new Integer[SIZE];
370 +        for (int i = 0; i < SIZE-1; ++i)
371 +            ints[i] = i;
372 +        Collection<Integer> elements = Arrays.asList(ints);
373          try {
374 <            Integer[] ints = new Integer[SIZE];
348 <            for (int i = 0; i < SIZE-1; ++i)
349 <                ints[i] = new Integer(i);
350 <            LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
374 >            new LinkedBlockingDeque(elements);
375              shouldThrow();
376          } catch (NullPointerException success) {}
377      }
# Line 358 | Line 382 | public class LinkedBlockingDequeTest ext
382      public void testConstructor6() {
383          Integer[] ints = new Integer[SIZE];
384          for (int i = 0; i < SIZE; ++i)
385 <            ints[i] = new Integer(i);
385 >            ints[i] = i;
386          LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
387          for (int i = 0; i < SIZE; ++i)
388              assertEquals(ints[i], q.poll());
# Line 383 | Line 407 | public class LinkedBlockingDequeTest ext
407       * remainingCapacity decreases on add, increases on remove
408       */
409      public void testRemainingCapacity() {
410 <        LinkedBlockingDeque q = populatedDeque(SIZE);
410 >        BlockingQueue q = populatedDeque(SIZE);
411          for (int i = 0; i < SIZE; ++i) {
412              assertEquals(i, q.remainingCapacity());
413 <            assertEquals(SIZE-i, q.size());
414 <            q.remove();
413 >            assertEquals(SIZE, q.size() + q.remainingCapacity());
414 >            assertEquals(i, q.remove());
415          }
416          for (int i = 0; i < SIZE; ++i) {
417              assertEquals(SIZE-i, q.remainingCapacity());
418 <            assertEquals(i, q.size());
419 <            q.add(new Integer(i));
418 >            assertEquals(SIZE, q.size() + q.remainingCapacity());
419 >            assertTrue(q.add(i));
420          }
421      }
422  
423      /**
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    /**
424       * push(null) throws NPE
425       */
426      public void testPushNull() {
# Line 436 | Line 438 | public class LinkedBlockingDequeTest ext
438          try {
439              LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
440              for (int i = 0; i < SIZE; ++i) {
441 <                Integer I = new Integer(i);
442 <                q.push(I);
443 <                assertEquals(I, q.peek());
441 >                Integer x = new Integer(i);
442 >                q.push(x);
443 >                assertEquals(x, q.peek());
444              }
445              assertEquals(0, q.remainingCapacity());
446              q.push(new Integer(SIZE));
# Line 456 | Line 458 | public class LinkedBlockingDequeTest ext
458          assertSame(four, q.peekFirst());
459      }
460  
459
461      /**
462       * pop removes next element, or throws NSEE if empty
463       */
# Line 471 | Line 472 | public class LinkedBlockingDequeTest ext
472          } catch (NoSuchElementException success) {}
473      }
474  
474
475      /**
476       * Offer succeeds if not full; fails if full
477       */
# Line 485 | Line 485 | public class LinkedBlockingDequeTest ext
485       * add succeeds if not full; throws ISE if full
486       */
487      public void testAdd() {
488 +        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
489 +        for (int i = 0; i < SIZE; ++i)
490 +            assertTrue(q.add(new Integer(i)));
491 +        assertEquals(0, q.remainingCapacity());
492          try {
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());
493              q.add(new Integer(SIZE));
494              shouldThrow();
495          } catch (IllegalStateException success) {}
496      }
497  
498      /**
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    /**
499       * addAll(this) throws IAE
500       */
501      public void testAddAllSelf() {
502 +        LinkedBlockingDeque q = populatedDeque(SIZE);
503          try {
515            LinkedBlockingDeque q = populatedDeque(SIZE);
504              q.addAll(q);
505              shouldThrow();
506          } catch (IllegalArgumentException success) {}
507      }
508  
509      /**
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    /**
510       * addAll of a collection with any null elements throws NPE after
511       * possibly adding some elements
512       */
513      public void testAddAll3() {
514 +        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
515 +        Integer[] ints = new Integer[SIZE];
516 +        for (int i = 0; i < SIZE-1; ++i)
517 +            ints[i] = new Integer(i);
518 +        Collection<Integer> elements = Arrays.asList(ints);
519          try {
520 <            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
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));
520 >            q.addAll(elements);
521              shouldThrow();
522          } catch (NullPointerException success) {}
523      }
524  
525      /**
526 <     * addAll throws ISE if not enough room
526 >     * addAll throws IllegalStateException if not enough room
527       */
528      public void testAddAll4() {
529 +        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE - 1);
530 +        Integer[] ints = new Integer[SIZE];
531 +        for (int i = 0; i < SIZE; ++i)
532 +            ints[i] = new Integer(i);
533 +        Collection<Integer> elements = Arrays.asList(ints);
534          try {
535 <            LinkedBlockingDeque q = new LinkedBlockingDeque(1);
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));
535 >            q.addAll(elements);
536              shouldThrow();
537          } catch (IllegalStateException success) {}
538      }
# Line 574 | Line 552 | public class LinkedBlockingDequeTest ext
552              assertEquals(ints[i], q.poll());
553      }
554  
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
555      /**
556       * all elements successfully put are contained
557       */
558      public void testPut() throws InterruptedException {
559          LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
560          for (int i = 0; i < SIZE; ++i) {
561 <            Integer I = new Integer(i);
562 <            q.put(I);
563 <            assertTrue(q.contains(I));
561 >            Integer x = new Integer(i);
562 >            q.put(x);
563 >            assertTrue(q.contains(x));
564          }
565          assertEquals(0, q.remainingCapacity());
566      }
# Line 604 | Line 570 | public class LinkedBlockingDequeTest ext
570       */
571      public void testBlockingPut() throws InterruptedException {
572          final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
573 <        Thread t = new Thread(new CheckedRunnable() {
573 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
574 >        Thread t = newStartedThread(new CheckedRunnable() {
575              public void realRun() throws InterruptedException {
576                  for (int i = 0; i < SIZE; ++i)
577                      q.put(i);
578                  assertEquals(SIZE, q.size());
579                  assertEquals(0, q.remainingCapacity());
580 +
581 +                Thread.currentThread().interrupt();
582                  try {
583                      q.put(99);
584                      shouldThrow();
585                  } catch (InterruptedException success) {}
586 +                assertFalse(Thread.interrupted());
587 +
588 +                pleaseInterrupt.countDown();
589 +                try {
590 +                    q.put(99);
591 +                    shouldThrow();
592 +                } catch (InterruptedException success) {}
593 +                assertFalse(Thread.interrupted());
594              }});
595  
596 <        t.start();
597 <        delay(SHORT_DELAY_MS);
596 >        await(pleaseInterrupt);
597 >        assertThreadStaysAlive(t);
598          t.interrupt();
599 <        t.join();
599 >        awaitTermination(t);
600          assertEquals(SIZE, q.size());
601          assertEquals(0, q.remainingCapacity());
602      }
603  
604      /**
605 <     * put blocks waiting for take when full
605 >     * put blocks interruptibly waiting for take when full
606       */
607      public void testPutWithTake() throws InterruptedException {
608          final int capacity = 2;
609          final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
610 <        Thread t = new Thread(new CheckedRunnable() {
610 >        final CountDownLatch pleaseTake = new CountDownLatch(1);
611 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
612 >        Thread t = newStartedThread(new CheckedRunnable() {
613              public void realRun() throws InterruptedException {
614 <                for (int i = 0; i < capacity + 1; i++)
614 >                for (int i = 0; i < capacity; i++)
615                      q.put(i);
616 +                pleaseTake.countDown();
617 +                q.put(86);
618 +
619 +                pleaseInterrupt.countDown();
620                  try {
621                      q.put(99);
622                      shouldThrow();
623                  } catch (InterruptedException success) {}
624 +                assertFalse(Thread.interrupted());
625              }});
626  
627 <        t.start();
628 <        delay(SHORT_DELAY_MS);
645 <        assertEquals(q.remainingCapacity(), 0);
627 >        await(pleaseTake);
628 >        assertEquals(0, q.remainingCapacity());
629          assertEquals(0, q.take());
630 <        delay(SHORT_DELAY_MS);
630 >
631 >        await(pleaseInterrupt);
632 >        assertThreadStaysAlive(t);
633          t.interrupt();
634 <        t.join();
635 <        assertEquals(q.remainingCapacity(), 0);
634 >        awaitTermination(t);
635 >        assertEquals(0, q.remainingCapacity());
636      }
637  
638      /**
# Line 671 | Line 656 | public class LinkedBlockingDequeTest ext
656              }});
657  
658          await(pleaseInterrupt);
659 +        assertThreadStaysAlive(t);
660          t.interrupt();
661          awaitTermination(t);
662      }
# Line 686 | Line 672 | public class LinkedBlockingDequeTest ext
672      }
673  
674      /**
675 <     * Take removes existing elements until empty, then blocks interruptibly
675 >     * take removes existing elements until empty, then blocks interruptibly
676       */
677      public void testBlockingTake() throws InterruptedException {
678          final LinkedBlockingDeque q = populatedDeque(SIZE);
679 <        Thread t = new Thread(new CheckedRunnable() {
679 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
680 >        Thread t = newStartedThread(new CheckedRunnable() {
681              public void realRun() throws InterruptedException {
682                  for (int i = 0; i < SIZE; ++i) {
683                      assertEquals(i, q.take());
684                  }
685 +
686 +                Thread.currentThread().interrupt();
687 +                try {
688 +                    q.take();
689 +                    shouldThrow();
690 +                } catch (InterruptedException success) {}
691 +                assertFalse(Thread.interrupted());
692 +
693 +                pleaseInterrupt.countDown();
694                  try {
695                      q.take();
696                      shouldThrow();
697                  } catch (InterruptedException success) {}
698 +                assertFalse(Thread.interrupted());
699              }});
700  
701 <        t.start();
702 <        delay(SHORT_DELAY_MS);
701 >        await(pleaseInterrupt);
702 >        assertThreadStaysAlive(t);
703          t.interrupt();
704 <        t.join();
704 >        awaitTermination(t);
705      }
706  
710
707      /**
708       * poll succeeds unless empty
709       */
# Line 736 | Line 732 | public class LinkedBlockingDequeTest ext
732      public void testTimedPoll() throws InterruptedException {
733          LinkedBlockingDeque q = populatedDeque(SIZE);
734          for (int i = 0; i < SIZE; ++i) {
735 <            assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
736 <        }
737 <        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
735 >            long startTime = System.nanoTime();
736 >            assertEquals(i, q.poll(LONG_DELAY_MS, MILLISECONDS));
737 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
738 >        }
739 >        long startTime = System.nanoTime();
740 >        assertNull(q.poll(timeoutMillis(), MILLISECONDS));
741 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
742 >        checkEmpty(q);
743      }
744  
745      /**
# Line 776 | Line 777 | public class LinkedBlockingDequeTest ext
777       * putFirst(null) throws NPE
778       */
779      public void testPutFirstNull() throws InterruptedException {
780 +        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
781          try {
780            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
782              q.putFirst(null);
783              shouldThrow();
784          } catch (NullPointerException success) {}
# Line 789 | Line 790 | public class LinkedBlockingDequeTest ext
790      public void testPutFirst() throws InterruptedException {
791          LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
792          for (int i = 0; i < SIZE; ++i) {
793 <            Integer I = new Integer(i);
794 <            q.putFirst(I);
795 <            assertTrue(q.contains(I));
793 >            Integer x = new Integer(i);
794 >            q.putFirst(x);
795 >            assertTrue(q.contains(x));
796          }
797          assertEquals(0, q.remainingCapacity());
798      }
# Line 801 | Line 802 | public class LinkedBlockingDequeTest ext
802       */
803      public void testBlockingPutFirst() throws InterruptedException {
804          final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
805 <        Thread t = new Thread(new CheckedRunnable() {
805 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
806 >        Thread t = newStartedThread(new CheckedRunnable() {
807              public void realRun() throws InterruptedException {
808                  for (int i = 0; i < SIZE; ++i)
809                      q.putFirst(i);
810                  assertEquals(SIZE, q.size());
811                  assertEquals(0, q.remainingCapacity());
812 +
813 +                Thread.currentThread().interrupt();
814                  try {
815                      q.putFirst(99);
816                      shouldThrow();
817                  } catch (InterruptedException success) {}
818 +                assertFalse(Thread.interrupted());
819 +
820 +                pleaseInterrupt.countDown();
821 +                try {
822 +                    q.putFirst(99);
823 +                    shouldThrow();
824 +                } catch (InterruptedException success) {}
825 +                assertFalse(Thread.interrupted());
826              }});
827  
828 <        t.start();
829 <        delay(SHORT_DELAY_MS);
828 >        await(pleaseInterrupt);
829 >        assertThreadStaysAlive(t);
830          t.interrupt();
831 <        t.join();
831 >        awaitTermination(t);
832          assertEquals(SIZE, q.size());
833          assertEquals(0, q.remainingCapacity());
834      }
835  
836      /**
837 <     * putFirst blocks waiting for take when full
837 >     * putFirst blocks interruptibly waiting for take when full
838       */
839      public void testPutFirstWithTake() throws InterruptedException {
840          final int capacity = 2;
841          final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
842 <        Thread t = new Thread(new CheckedRunnable() {
842 >        final CountDownLatch pleaseTake = new CountDownLatch(1);
843 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
844 >        Thread t = newStartedThread(new CheckedRunnable() {
845              public void realRun() throws InterruptedException {
846 <                for (int i = 0; i < capacity + 1; i++)
846 >                for (int i = 0; i < capacity; i++)
847                      q.putFirst(i);
848 +                pleaseTake.countDown();
849 +                q.putFirst(86);
850 +
851 +                pleaseInterrupt.countDown();
852                  try {
853                      q.putFirst(99);
854                      shouldThrow();
855                  } catch (InterruptedException success) {}
856 +                assertFalse(Thread.interrupted());
857              }});
858  
859 <        t.start();
860 <        delay(SHORT_DELAY_MS);
842 <        assertEquals(q.remainingCapacity(), 0);
859 >        await(pleaseTake);
860 >        assertEquals(0, q.remainingCapacity());
861          assertEquals(capacity - 1, q.take());
862 <        delay(SHORT_DELAY_MS);
862 >
863 >        await(pleaseInterrupt);
864 >        assertThreadStaysAlive(t);
865          t.interrupt();
866 <        t.join();
867 <        assertEquals(q.remainingCapacity(), 0);
866 >        awaitTermination(t);
867 >        assertEquals(0, q.remainingCapacity());
868      }
869  
870      /**
# Line 868 | Line 888 | public class LinkedBlockingDequeTest ext
888              }});
889  
890          await(pleaseInterrupt);
891 +        assertThreadStaysAlive(t);
892          t.interrupt();
893          awaitTermination(t);
894      }
# Line 883 | Line 904 | public class LinkedBlockingDequeTest ext
904      }
905  
906      /**
907 <     * takeFirst blocks interruptibly when empty
907 >     * takeFirst() blocks interruptibly when empty
908       */
909 <    public void testTakeFirstFromEmpty() throws InterruptedException {
910 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
911 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
912 <            public void realRun() throws InterruptedException {
913 <                q.takeFirst();
914 <            }};
909 >    public void testTakeFirstFromEmptyBlocksInterruptibly() {
910 >        final BlockingDeque q = new LinkedBlockingDeque();
911 >        final CountDownLatch threadStarted = new CountDownLatch(1);
912 >        Thread t = newStartedThread(new CheckedRunnable() {
913 >            public void realRun() {
914 >                threadStarted.countDown();
915 >                try {
916 >                    q.takeFirst();
917 >                    shouldThrow();
918 >                } catch (InterruptedException success) {}
919 >                assertFalse(Thread.interrupted());
920 >            }});
921  
922 <        t.start();
923 <        delay(SHORT_DELAY_MS);
922 >        await(threadStarted);
923 >        assertThreadStaysAlive(t);
924          t.interrupt();
925 <        t.join();
925 >        awaitTermination(t);
926 >    }
927 >
928 >    /**
929 >     * takeFirst() throws InterruptedException immediately if interrupted
930 >     * before waiting
931 >     */
932 >    public void testTakeFirstFromEmptyAfterInterrupt() {
933 >        final BlockingDeque q = new LinkedBlockingDeque();
934 >        Thread t = newStartedThread(new CheckedRunnable() {
935 >            public void realRun() {
936 >                Thread.currentThread().interrupt();
937 >                try {
938 >                    q.takeFirst();
939 >                    shouldThrow();
940 >                } catch (InterruptedException success) {}
941 >                assertFalse(Thread.interrupted());
942 >            }});
943 >
944 >        awaitTermination(t);
945      }
946  
947      /**
948 <     * TakeFirst removes existing elements until empty, then blocks interruptibly
948 >     * takeLast() blocks interruptibly when empty
949 >     */
950 >    public void testTakeLastFromEmptyBlocksInterruptibly() {
951 >        final BlockingDeque q = new LinkedBlockingDeque();
952 >        final CountDownLatch threadStarted = new CountDownLatch(1);
953 >        Thread t = newStartedThread(new CheckedRunnable() {
954 >            public void realRun() {
955 >                threadStarted.countDown();
956 >                try {
957 >                    q.takeLast();
958 >                    shouldThrow();
959 >                } catch (InterruptedException success) {}
960 >                assertFalse(Thread.interrupted());
961 >            }});
962 >
963 >        await(threadStarted);
964 >        assertThreadStaysAlive(t);
965 >        t.interrupt();
966 >        awaitTermination(t);
967 >    }
968 >
969 >    /**
970 >     * takeLast() throws InterruptedException immediately if interrupted
971 >     * before waiting
972 >     */
973 >    public void testTakeLastFromEmptyAfterInterrupt() {
974 >        final BlockingDeque q = new LinkedBlockingDeque();
975 >        Thread t = newStartedThread(new CheckedRunnable() {
976 >            public void realRun() {
977 >                Thread.currentThread().interrupt();
978 >                try {
979 >                    q.takeLast();
980 >                    shouldThrow();
981 >                } catch (InterruptedException success) {}
982 >                assertFalse(Thread.interrupted());
983 >            }});
984 >
985 >        awaitTermination(t);
986 >    }
987 >
988 >    /**
989 >     * takeFirst removes existing elements until empty, then blocks interruptibly
990       */
991      public void testBlockingTakeFirst() throws InterruptedException {
992          final LinkedBlockingDeque q = populatedDeque(SIZE);
993 <        Thread t = new Thread(new CheckedRunnable() {
993 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
994 >        Thread t = newStartedThread(new CheckedRunnable() {
995              public void realRun() throws InterruptedException {
996 <                for (int i = 0; i < SIZE; ++i)
996 >                for (int i = 0; i < SIZE; ++i) {
997                      assertEquals(i, q.takeFirst());
998 +                }
999 +
1000 +                Thread.currentThread().interrupt();
1001 +                try {
1002 +                    q.takeFirst();
1003 +                    shouldThrow();
1004 +                } catch (InterruptedException success) {}
1005 +                assertFalse(Thread.interrupted());
1006 +
1007 +                pleaseInterrupt.countDown();
1008                  try {
1009                      q.takeFirst();
1010                      shouldThrow();
1011                  } catch (InterruptedException success) {}
1012 +                assertFalse(Thread.interrupted());
1013              }});
1014  
1015 <        t.start();
1016 <        delay(SHORT_DELAY_MS);
1015 >        await(pleaseInterrupt);
1016 >        assertThreadStaysAlive(t);
1017          t.interrupt();
1018 <        t.join();
1018 >        awaitTermination(t);
1019      }
1020  
922
1021      /**
1022       * timed pollFirst with zero timeout succeeds when non-empty, else times out
1023       */
# Line 937 | Line 1035 | public class LinkedBlockingDequeTest ext
1035      public void testTimedPollFirst() throws InterruptedException {
1036          LinkedBlockingDeque q = populatedDeque(SIZE);
1037          for (int i = 0; i < SIZE; ++i) {
1038 <            assertEquals(i, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
1039 <        }
1040 <        assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
1038 >            long startTime = System.nanoTime();
1039 >            assertEquals(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
1040 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1041 >        }
1042 >        long startTime = System.nanoTime();
1043 >        assertNull(q.pollFirst(timeoutMillis(), MILLISECONDS));
1044 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1045 >        checkEmpty(q);
1046      }
1047  
1048      /**
# Line 947 | Line 1050 | public class LinkedBlockingDequeTest ext
1050       * returning timeout status
1051       */
1052      public void testInterruptedTimedPollFirst() throws InterruptedException {
1053 <        Thread t = new Thread(new CheckedRunnable() {
1053 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1054 >        Thread t = newStartedThread(new CheckedRunnable() {
1055              public void realRun() throws InterruptedException {
1056                  LinkedBlockingDeque q = populatedDeque(SIZE);
1057                  for (int i = 0; i < SIZE; ++i) {
1058 <                    assertEquals(i, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
1058 >                    assertEquals(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
1059                  }
1060 +
1061 +                Thread.currentThread().interrupt();
1062                  try {
1063                      q.pollFirst(SMALL_DELAY_MS, MILLISECONDS);
1064                      shouldThrow();
1065                  } catch (InterruptedException success) {}
1066 +                assertFalse(Thread.interrupted());
1067 +
1068 +                pleaseInterrupt.countDown();
1069 +                try {
1070 +                    q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
1071 +                    shouldThrow();
1072 +                } catch (InterruptedException success) {}
1073 +                assertFalse(Thread.interrupted());
1074              }});
1075  
1076 <        t.start();
1077 <        delay(SHORT_DELAY_MS);
1076 >        await(pleaseInterrupt);
1077 >        assertThreadStaysAlive(t);
1078          t.interrupt();
1079 <        t.join();
1079 >        awaitTermination(t);
1080      }
1081  
1082      /**
# Line 971 | Line 1085 | public class LinkedBlockingDequeTest ext
1085       */
1086      public void testTimedPollFirstWithOfferFirst() throws InterruptedException {
1087          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1088 <        Thread t = new Thread(new CheckedRunnable() {
1088 >        final CheckedBarrier barrier = new CheckedBarrier(2);
1089 >        Thread t = newStartedThread(new CheckedRunnable() {
1090              public void realRun() throws InterruptedException {
1091 <                assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
1091 >                long startTime = System.nanoTime();
1092 >                assertNull(q.pollFirst(timeoutMillis(), MILLISECONDS));
1093 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1094 >
1095 >                barrier.await();
1096 >
1097                  assertSame(zero, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
1098 +
1099 +                Thread.currentThread().interrupt();
1100                  try {
1101                      q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
1102                      shouldThrow();
1103                  } catch (InterruptedException success) {}
1104 +
1105 +                barrier.await();
1106 +                try {
1107 +                    q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
1108 +                    shouldThrow();
1109 +                } catch (InterruptedException success) {}
1110 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1111              }});
1112  
1113 <        t.start();
1114 <        delay(SMALL_DELAY_MS);
1115 <        assertTrue(q.offerFirst(zero, SHORT_DELAY_MS, MILLISECONDS));
1113 >        barrier.await();
1114 >        long startTime = System.nanoTime();
1115 >        assertTrue(q.offerFirst(zero, LONG_DELAY_MS, MILLISECONDS));
1116 >        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1117 >        barrier.await();
1118 >        assertThreadStaysAlive(t);
1119          t.interrupt();
1120 <        t.join();
1120 >        awaitTermination(t);
1121      }
1122  
1123      /**
1124       * putLast(null) throws NPE
1125       */
1126      public void testPutLastNull() throws InterruptedException {
1127 +        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1128          try {
996            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1129              q.putLast(null);
1130              shouldThrow();
1131          } catch (NullPointerException success) {}
# Line 1005 | Line 1137 | public class LinkedBlockingDequeTest ext
1137      public void testPutLast() throws InterruptedException {
1138          LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1139          for (int i = 0; i < SIZE; ++i) {
1140 <            Integer I = new Integer(i);
1141 <            q.putLast(I);
1142 <            assertTrue(q.contains(I));
1140 >            Integer x = new Integer(i);
1141 >            q.putLast(x);
1142 >            assertTrue(q.contains(x));
1143          }
1144          assertEquals(0, q.remainingCapacity());
1145      }
# Line 1017 | Line 1149 | public class LinkedBlockingDequeTest ext
1149       */
1150      public void testBlockingPutLast() throws InterruptedException {
1151          final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1152 <        Thread t = new Thread(new CheckedRunnable() {
1152 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1153 >        Thread t = newStartedThread(new CheckedRunnable() {
1154              public void realRun() throws InterruptedException {
1155                  for (int i = 0; i < SIZE; ++i)
1156                      q.putLast(i);
1157                  assertEquals(SIZE, q.size());
1158                  assertEquals(0, q.remainingCapacity());
1159 +
1160 +                Thread.currentThread().interrupt();
1161 +                try {
1162 +                    q.putLast(99);
1163 +                    shouldThrow();
1164 +                } catch (InterruptedException success) {}
1165 +                assertFalse(Thread.interrupted());
1166 +
1167 +                pleaseInterrupt.countDown();
1168                  try {
1169                      q.putLast(99);
1170                      shouldThrow();
1171                  } catch (InterruptedException success) {}
1172 +                assertFalse(Thread.interrupted());
1173              }});
1174  
1175 <        t.start();
1176 <        delay(SHORT_DELAY_MS);
1175 >        await(pleaseInterrupt);
1176 >        assertThreadStaysAlive(t);
1177          t.interrupt();
1178 <        t.join();
1178 >        awaitTermination(t);
1179          assertEquals(SIZE, q.size());
1180          assertEquals(0, q.remainingCapacity());
1181      }
1182  
1183      /**
1184 <     * putLast blocks waiting for take when full
1184 >     * putLast blocks interruptibly waiting for take when full
1185       */
1186      public void testPutLastWithTake() throws InterruptedException {
1187          final int capacity = 2;
1188          final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
1189 <        Thread t = new Thread(new CheckedRunnable() {
1189 >        final CountDownLatch pleaseTake = new CountDownLatch(1);
1190 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1191 >        Thread t = newStartedThread(new CheckedRunnable() {
1192              public void realRun() throws InterruptedException {
1193 <                for (int i = 0; i < capacity + 1; i++)
1193 >                for (int i = 0; i < capacity; i++)
1194                      q.putLast(i);
1195 +                pleaseTake.countDown();
1196 +                q.putLast(86);
1197 +
1198 +                pleaseInterrupt.countDown();
1199                  try {
1200                      q.putLast(99);
1201                      shouldThrow();
1202                  } catch (InterruptedException success) {}
1203 +                assertFalse(Thread.interrupted());
1204              }});
1205  
1206 <        t.start();
1207 <        delay(SHORT_DELAY_MS);
1058 <        assertEquals(q.remainingCapacity(), 0);
1206 >        await(pleaseTake);
1207 >        assertEquals(0, q.remainingCapacity());
1208          assertEquals(0, q.take());
1209 <        delay(SHORT_DELAY_MS);
1209 >
1210 >        await(pleaseInterrupt);
1211 >        assertThreadStaysAlive(t);
1212          t.interrupt();
1213 <        t.join();
1214 <        assertEquals(q.remainingCapacity(), 0);
1213 >        awaitTermination(t);
1214 >        assertEquals(0, q.remainingCapacity());
1215      }
1216  
1217      /**
# Line 1084 | Line 1235 | public class LinkedBlockingDequeTest ext
1235              }});
1236  
1237          await(pleaseInterrupt);
1238 +        assertThreadStaysAlive(t);
1239          t.interrupt();
1240          awaitTermination(t);
1241      }
# Line 1099 | Line 1251 | public class LinkedBlockingDequeTest ext
1251      }
1252  
1253      /**
1254 <     * 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
1254 >     * takeLast removes existing elements until empty, then blocks interruptibly
1255       */
1256      public void testBlockingTakeLast() throws InterruptedException {
1257          final LinkedBlockingDeque q = populatedDeque(SIZE);
1258 <        Thread t = new Thread(new CheckedRunnable() {
1258 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1259 >        Thread t = newStartedThread(new CheckedRunnable() {
1260              public void realRun() throws InterruptedException {
1261 <                for (int i = 0; i < SIZE; ++i)
1262 <                    assertEquals(SIZE - 1 - i, q.takeLast());
1261 >                for (int i = 0; i < SIZE; ++i) {
1262 >                    assertEquals(SIZE-i-1, q.takeLast());
1263 >                }
1264 >
1265 >                Thread.currentThread().interrupt();
1266                  try {
1267                      q.takeLast();
1268                      shouldThrow();
1269                  } catch (InterruptedException success) {}
1270 +                assertFalse(Thread.interrupted());
1271 +
1272 +                pleaseInterrupt.countDown();
1273 +                try {
1274 +                    q.takeLast();
1275 +                    shouldThrow();
1276 +                } catch (InterruptedException success) {}
1277 +                assertFalse(Thread.interrupted());
1278              }});
1279  
1280 <        t.start();
1281 <        delay(SHORT_DELAY_MS);
1280 >        await(pleaseInterrupt);
1281 >        assertThreadStaysAlive(t);
1282          t.interrupt();
1283 <        t.join();
1283 >        awaitTermination(t);
1284      }
1285  
1286      /**
# Line 1152 | Line 1300 | public class LinkedBlockingDequeTest ext
1300      public void testTimedPollLast() throws InterruptedException {
1301          LinkedBlockingDeque q = populatedDeque(SIZE);
1302          for (int i = 0; i < SIZE; ++i) {
1303 <            assertEquals(SIZE-i-1, q.pollLast(SHORT_DELAY_MS, MILLISECONDS));
1304 <        }
1305 <        assertNull(q.pollLast(SHORT_DELAY_MS, MILLISECONDS));
1303 >            long startTime = System.nanoTime();
1304 >            assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1305 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1306 >        }
1307 >        long startTime = System.nanoTime();
1308 >        assertNull(q.pollLast(timeoutMillis(), MILLISECONDS));
1309 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1310 >        checkEmpty(q);
1311      }
1312  
1313      /**
# Line 1162 | Line 1315 | public class LinkedBlockingDequeTest ext
1315       * returning timeout status
1316       */
1317      public void testInterruptedTimedPollLast() throws InterruptedException {
1318 <        Thread t = new Thread(new CheckedRunnable() {
1318 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1319 >        Thread t = newStartedThread(new CheckedRunnable() {
1320              public void realRun() throws InterruptedException {
1321                  LinkedBlockingDeque q = populatedDeque(SIZE);
1322                  for (int i = 0; i < SIZE; ++i) {
1323 <                    assertEquals(SIZE-i-1, q.pollLast(SHORT_DELAY_MS, MILLISECONDS));
1323 >                    assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1324                  }
1325 +
1326 +                Thread.currentThread().interrupt();
1327 +                try {
1328 +                    q.pollLast(LONG_DELAY_MS, MILLISECONDS);
1329 +                    shouldThrow();
1330 +                } catch (InterruptedException success) {}
1331 +                assertFalse(Thread.interrupted());
1332 +
1333 +                pleaseInterrupt.countDown();
1334                  try {
1335 <                    q.pollLast(SMALL_DELAY_MS, MILLISECONDS);
1335 >                    q.pollLast(LONG_DELAY_MS, MILLISECONDS);
1336                      shouldThrow();
1337                  } catch (InterruptedException success) {}
1338 +                assertFalse(Thread.interrupted());
1339              }});
1340  
1341 <        t.start();
1342 <        delay(SHORT_DELAY_MS);
1341 >        await(pleaseInterrupt);
1342 >        assertThreadStaysAlive(t);
1343          t.interrupt();
1344 <        t.join();
1344 >        awaitTermination(t);
1345      }
1346  
1347      /**
# Line 1186 | Line 1350 | public class LinkedBlockingDequeTest ext
1350       */
1351      public void testTimedPollWithOfferLast() throws InterruptedException {
1352          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1353 <        Thread t = new Thread(new CheckedRunnable() {
1353 >        final CheckedBarrier barrier = new CheckedBarrier(2);
1354 >        Thread t = newStartedThread(new CheckedRunnable() {
1355              public void realRun() throws InterruptedException {
1356 <                assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
1356 >                long startTime = System.nanoTime();
1357 >                assertNull(q.poll(timeoutMillis(), MILLISECONDS));
1358 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1359 >
1360 >                barrier.await();
1361 >
1362                  assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
1363 +
1364 +                Thread.currentThread().interrupt();
1365                  try {
1366                      q.poll(LONG_DELAY_MS, MILLISECONDS);
1367                      shouldThrow();
1368                  } catch (InterruptedException success) {}
1369 +                assertFalse(Thread.interrupted());
1370 +
1371 +                barrier.await();
1372 +                try {
1373 +                    q.poll(LONG_DELAY_MS, MILLISECONDS);
1374 +                    shouldThrow();
1375 +                } catch (InterruptedException success) {}
1376 +                assertFalse(Thread.interrupted());
1377              }});
1378  
1379 <        t.start();
1380 <        delay(SMALL_DELAY_MS);
1381 <        assertTrue(q.offerLast(zero, SHORT_DELAY_MS, MILLISECONDS));
1379 >        barrier.await();
1380 >        long startTime = System.nanoTime();
1381 >        assertTrue(q.offerLast(zero, LONG_DELAY_MS, MILLISECONDS));
1382 >        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1383 >
1384 >        barrier.await();
1385 >        assertThreadStaysAlive(t);
1386          t.interrupt();
1387 <        t.join();
1387 >        awaitTermination(t);
1388      }
1389  
1206
1390      /**
1391       * element returns next element, or throws NSEE if empty
1392       */
# Line 1220 | Line 1403 | public class LinkedBlockingDequeTest ext
1403      }
1404  
1405      /**
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    /**
1406       * contains(x) reports true when elements added but not yet removed
1407       */
1408      public void testContains() {
# Line 1311 | Line 1473 | public class LinkedBlockingDequeTest ext
1473              assertTrue(q.removeAll(p));
1474              assertEquals(SIZE-i, q.size());
1475              for (int j = 0; j < i; ++j) {
1476 <                Integer I = (Integer)(p.remove());
1477 <                assertFalse(q.contains(I));
1476 >                Integer x = (Integer)(p.remove());
1477 >                assertFalse(q.contains(x));
1478              }
1479          }
1480      }
# Line 1320 | Line 1482 | public class LinkedBlockingDequeTest ext
1482      /**
1483       * toArray contains all elements in FIFO order
1484       */
1485 <    public void testToArray() throws InterruptedException{
1485 >    public void testToArray() throws InterruptedException {
1486          LinkedBlockingDeque q = populatedDeque(SIZE);
1487          Object[] o = q.toArray();
1488          for (int i = 0; i < o.length; i++)
# Line 1340 | Line 1502 | public class LinkedBlockingDequeTest ext
1502      }
1503  
1504      /**
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    /**
1505       * toArray(incompatible array type) throws ArrayStoreException
1506       */
1507      public void testToArray1_BadArg() {
# Line 1361 | Line 1512 | public class LinkedBlockingDequeTest ext
1512          } catch (ArrayStoreException success) {}
1513      }
1514  
1364
1515      /**
1516       * iterator iterates through all elements
1517       */
1518      public void testIterator() throws InterruptedException {
1519          LinkedBlockingDeque q = populatedDeque(SIZE);
1520          Iterator it = q.iterator();
1521 <        while (it.hasNext()) {
1521 >        int i;
1522 >        for (i = 0; it.hasNext(); i++)
1523 >            assertTrue(q.contains(it.next()));
1524 >        assertEquals(i, SIZE);
1525 >        assertIteratorExhausted(it);
1526 >
1527 >        it = q.iterator();
1528 >        for (i = 0; it.hasNext(); i++)
1529              assertEquals(it.next(), q.take());
1530 <        }
1530 >        assertEquals(i, SIZE);
1531 >        assertIteratorExhausted(it);
1532 >    }
1533 >
1534 >    /**
1535 >     * iterator of empty collection has no elements
1536 >     */
1537 >    public void testEmptyIterator() {
1538 >        Deque c = new LinkedBlockingDeque();
1539 >        assertIteratorExhausted(c.iterator());
1540 >        assertIteratorExhausted(c.descendingIterator());
1541      }
1542  
1543      /**
# Line 1392 | Line 1559 | public class LinkedBlockingDequeTest ext
1559          assertFalse(it.hasNext());
1560      }
1561  
1395
1562      /**
1563       * iterator ordering is FIFO
1564       */
# Line 1424 | Line 1590 | public class LinkedBlockingDequeTest ext
1590          assertEquals(0, q.size());
1591      }
1592  
1427
1593      /**
1594       * Descending iterator iterates through all elements
1595       */
# Line 1487 | Line 1652 | public class LinkedBlockingDequeTest ext
1652          }
1653      }
1654  
1490
1655      /**
1656       * toString contains toStrings of elements
1657       */
# Line 1495 | Line 1659 | public class LinkedBlockingDequeTest ext
1659          LinkedBlockingDeque q = populatedDeque(SIZE);
1660          String s = q.toString();
1661          for (int i = 0; i < SIZE; ++i) {
1662 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
1662 >            assertTrue(s.contains(String.valueOf(i)));
1663          }
1664      }
1665  
1502
1666      /**
1667       * offer transfers elements across Executor tasks
1668       */
# Line 1508 | Line 1671 | public class LinkedBlockingDequeTest ext
1671          q.add(one);
1672          q.add(two);
1673          ExecutorService executor = Executors.newFixedThreadPool(2);
1674 +        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
1675          executor.execute(new CheckedRunnable() {
1676              public void realRun() throws InterruptedException {
1677                  assertFalse(q.offer(three));
1678 <                assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
1678 >                threadsStarted.await();
1679 >                assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
1680                  assertEquals(0, q.remainingCapacity());
1681              }});
1682  
1683          executor.execute(new CheckedRunnable() {
1684              public void realRun() throws InterruptedException {
1685 <                delay(SMALL_DELAY_MS);
1685 >                threadsStarted.await();
1686                  assertSame(one, q.take());
1687              }});
1688  
# Line 1525 | Line 1690 | public class LinkedBlockingDequeTest ext
1690      }
1691  
1692      /**
1693 <     * poll retrieves elements across Executor threads
1693 >     * timed poll retrieves elements across Executor threads
1694       */
1695      public void testPollInExecutor() {
1696          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1697 +        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
1698          ExecutorService executor = Executors.newFixedThreadPool(2);
1699          executor.execute(new CheckedRunnable() {
1700              public void realRun() throws InterruptedException {
1701                  assertNull(q.poll());
1702 <                assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
1703 <                assertTrue(q.isEmpty());
1702 >                threadsStarted.await();
1703 >                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
1704 >                checkEmpty(q);
1705              }});
1706  
1707          executor.execute(new CheckedRunnable() {
1708              public void realRun() throws InterruptedException {
1709 <                delay(SMALL_DELAY_MS);
1709 >                threadsStarted.await();
1710                  q.put(one);
1711              }});
1712  
# Line 1550 | Line 1717 | public class LinkedBlockingDequeTest ext
1717       * A deserialized serialized deque has same elements in same order
1718       */
1719      public void testSerialization() throws Exception {
1720 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1721 <
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 <    }
1567 <
1568 <    /**
1569 <     * drainTo(null) throws NPE
1570 <     */
1571 <    public void testDrainToNull() {
1572 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1573 <        try {
1574 <            q.drainTo(null);
1575 <            shouldThrow();
1576 <        } catch (NullPointerException success) {}
1577 <    }
1720 >        Queue x = populatedDeque(SIZE);
1721 >        Queue y = serialClone(x);
1722  
1723 <    /**
1724 <     * drainTo(this) throws IAE
1725 <     */
1726 <    public void testDrainToSelf() {
1727 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1728 <        try {
1729 <            q.drainTo(q);
1730 <            shouldThrow();
1731 <        } catch (IllegalArgumentException success) {}
1723 >        assertNotSame(y, x);
1724 >        assertEquals(x.size(), y.size());
1725 >        assertEquals(x.toString(), y.toString());
1726 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
1727 >        while (!x.isEmpty()) {
1728 >            assertFalse(y.isEmpty());
1729 >            assertEquals(x.remove(), y.remove());
1730 >        }
1731 >        assertTrue(y.isEmpty());
1732      }
1733  
1734      /**
# Line 1594 | Line 1738 | public class LinkedBlockingDequeTest ext
1738          LinkedBlockingDeque q = populatedDeque(SIZE);
1739          ArrayList l = new ArrayList();
1740          q.drainTo(l);
1741 <        assertEquals(q.size(), 0);
1742 <        assertEquals(l.size(), SIZE);
1741 >        assertEquals(0, q.size());
1742 >        assertEquals(SIZE, l.size());
1743          for (int i = 0; i < SIZE; ++i)
1744              assertEquals(l.get(i), new Integer(i));
1745          q.add(zero);
# Line 1605 | Line 1749 | public class LinkedBlockingDequeTest ext
1749          assertTrue(q.contains(one));
1750          l.clear();
1751          q.drainTo(l);
1752 <        assertEquals(q.size(), 0);
1753 <        assertEquals(l.size(), 2);
1752 >        assertEquals(0, q.size());
1753 >        assertEquals(2, l.size());
1754          for (int i = 0; i < 2; ++i)
1755              assertEquals(l.get(i), new Integer(i));
1756      }
# Line 1632 | Line 1776 | public class LinkedBlockingDequeTest ext
1776      }
1777  
1778      /**
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    /**
1779       * drainTo(c, n) empties first min(n, size) elements of queue into c
1780       */
1781      public void testDrainToN() {
# Line 1664 | Line 1786 | public class LinkedBlockingDequeTest ext
1786              ArrayList l = new ArrayList();
1787              q.drainTo(l, i);
1788              int k = (i < SIZE) ? i : SIZE;
1789 <            assertEquals(l.size(), k);
1790 <            assertEquals(q.size(), SIZE-k);
1789 >            assertEquals(k, l.size());
1790 >            assertEquals(SIZE-k, q.size());
1791              for (int j = 0; j < k; ++j)
1792                  assertEquals(l.get(j), new Integer(j));
1793 <            while (q.poll() != null) ;
1793 >            do {} while (q.poll() != null);
1794 >        }
1795 >    }
1796 >
1797 >    /**
1798 >     * remove(null), contains(null) always return false
1799 >     */
1800 >    public void testNeverContainsNull() {
1801 >        Deque<?>[] qs = {
1802 >            new LinkedBlockingDeque<Object>(),
1803 >            populatedDeque(2),
1804 >        };
1805 >
1806 >        for (Deque<?> q : qs) {
1807 >            assertFalse(q.contains(null));
1808 >            assertFalse(q.remove(null));
1809 >            assertFalse(q.removeFirstOccurrence(null));
1810 >            assertFalse(q.removeLastOccurrence(null));
1811          }
1812      }
1813  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines