ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LinkedBlockingQueueTest.java
(Generate patch)

Comparing jsr166/src/test/tck/LinkedBlockingQueueTest.java (file contents):
Revision 1.50 by jsr166, Tue Feb 21 01:54:04 2012 UTC vs.
Revision 1.62 by jsr166, Tue Oct 6 00:03:55 2015 UTC

# Line 6 | Line 6
6   * Pat Fisher, Mike Judd.
7   */
8  
9 < import junit.framework.*;
10 < import java.util.Arrays;
9 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
10 >
11   import java.util.ArrayList;
12 + import java.util.Arrays;
13   import java.util.Collection;
14   import java.util.Iterator;
15   import java.util.NoSuchElementException;
16   import java.util.Queue;
17   import java.util.concurrent.BlockingQueue;
18   import java.util.concurrent.CountDownLatch;
18 import java.util.concurrent.LinkedBlockingQueue;
19   import java.util.concurrent.Executors;
20   import java.util.concurrent.ExecutorService;
21 < import static java.util.concurrent.TimeUnit.MILLISECONDS;
21 > import java.util.concurrent.LinkedBlockingQueue;
22 >
23 > import junit.framework.Test;
24  
25   public class LinkedBlockingQueueTest extends JSR166TestCase {
26  
# Line 35 | Line 37 | public class LinkedBlockingQueueTest ext
37      }
38  
39      public static void main(String[] args) {
40 <        junit.textui.TestRunner.run(suite());
40 >        main(suite(), args);
41      }
42  
43      public static Test suite() {
# Line 45 | Line 47 | public class LinkedBlockingQueueTest ext
47      }
48  
49      /**
50 <     * Creates a queue of given size containing consecutive
50 >     * Returns a new queue of given size containing consecutive
51       * Integers 0 ... n.
52       */
53      private LinkedBlockingQueue<Integer> populatedQueue(int n) {
# Line 106 | Line 108 | public class LinkedBlockingQueueTest ext
108       */
109      public void testConstructor5() {
110          Integer[] ints = new Integer[SIZE];
111 <        for (int i = 0; i < SIZE-1; ++i)
111 >        for (int i = 0; i < SIZE - 1; ++i)
112              ints[i] = new Integer(i);
113          Collection<Integer> elements = Arrays.asList(ints);
114          try {
# Line 146 | Line 148 | public class LinkedBlockingQueueTest ext
148       * remainingCapacity decreases on add, increases on remove
149       */
150      public void testRemainingCapacity() {
151 <        LinkedBlockingQueue q = populatedQueue(SIZE);
151 >        BlockingQueue q = populatedQueue(SIZE);
152          for (int i = 0; i < SIZE; ++i) {
153              assertEquals(i, q.remainingCapacity());
154 <            assertEquals(SIZE-i, q.size());
155 <            q.remove();
154 >            assertEquals(SIZE, q.size() + q.remainingCapacity());
155 >            assertEquals(i, q.remove());
156          }
157          for (int i = 0; i < SIZE; ++i) {
158 <            assertEquals(SIZE-i, q.remainingCapacity());
159 <            assertEquals(i, q.size());
160 <            q.add(new Integer(i));
158 >            assertEquals(SIZE - i, q.remainingCapacity());
159 >            assertEquals(SIZE, q.size() + q.remainingCapacity());
160 >            assertTrue(q.add(i));
161          }
162      }
163  
# Line 200 | Line 202 | public class LinkedBlockingQueueTest ext
202      public void testAddAll3() {
203          LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
204          Integer[] ints = new Integer[SIZE];
205 <        for (int i = 0; i < SIZE-1; ++i)
205 >        for (int i = 0; i < SIZE - 1; ++i)
206              ints[i] = new Integer(i);
207          Collection<Integer> elements = Arrays.asList(ints);
208          try {
# Line 245 | Line 247 | public class LinkedBlockingQueueTest ext
247      public void testPut() throws InterruptedException {
248          LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
249          for (int i = 0; i < SIZE; ++i) {
250 <            Integer I = new Integer(i);
251 <            q.put(I);
252 <            assertTrue(q.contains(I));
250 >            Integer x = new Integer(i);
251 >            q.put(x);
252 >            assertTrue(q.contains(x));
253          }
254          assertEquals(0, q.remainingCapacity());
255      }
# Line 438 | Line 440 | public class LinkedBlockingQueueTest ext
440          final CountDownLatch aboutToWait = new CountDownLatch(1);
441          Thread t = newStartedThread(new CheckedRunnable() {
442              public void realRun() throws InterruptedException {
443 +                long startTime = System.nanoTime();
444                  for (int i = 0; i < SIZE; ++i) {
442                    long t0 = System.nanoTime();
445                      assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
444                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
446                  }
446                long t0 = System.nanoTime();
447                  aboutToWait.countDown();
448                  try {
449 <                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
449 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
450                      shouldThrow();
451                  } catch (InterruptedException success) {
452 <                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
452 >                    assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
453                  }
454              }});
455  
456 <        aboutToWait.await();
457 <        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
456 >        await(aboutToWait);
457 >        waitForThreadToEnterWaitState(t, LONG_DELAY_MS);
458          t.interrupt();
459 <        awaitTermination(t, MEDIUM_DELAY_MS);
459 >        awaitTermination(t);
460          checkEmpty(q);
461      }
462  
# Line 513 | Line 513 | public class LinkedBlockingQueueTest ext
513          assertTrue(q.remove(new Integer(1)));
514          assertTrue(q.remove(new Integer(2)));
515          assertTrue(q.add(new Integer(3)));
516 <        assertTrue(q.take() != null);
516 >        assertNotNull(q.take());
517      }
518  
519      /**
# Line 572 | Line 572 | public class LinkedBlockingQueueTest ext
572                  assertTrue(changed);
573  
574              assertTrue(q.containsAll(p));
575 <            assertEquals(SIZE-i, q.size());
575 >            assertEquals(SIZE - i, q.size());
576              p.remove();
577          }
578      }
# Line 585 | Line 585 | public class LinkedBlockingQueueTest ext
585              LinkedBlockingQueue q = populatedQueue(SIZE);
586              LinkedBlockingQueue p = populatedQueue(i);
587              assertTrue(q.removeAll(p));
588 <            assertEquals(SIZE-i, q.size());
588 >            assertEquals(SIZE - i, q.size());
589              for (int j = 0; j < i; ++j) {
590 <                Integer I = (Integer)(p.remove());
591 <                assertFalse(q.contains(I));
590 >                Integer x = (Integer)(p.remove());
591 >                assertFalse(q.contains(x));
592              }
593          }
594      }
# Line 632 | Line 632 | public class LinkedBlockingQueueTest ext
632      public void testIterator() throws InterruptedException {
633          LinkedBlockingQueue q = populatedQueue(SIZE);
634          Iterator it = q.iterator();
635 <        while (it.hasNext()) {
635 >        int i;
636 >        for (i = 0; it.hasNext(); i++)
637 >            assertTrue(q.contains(it.next()));
638 >        assertEquals(i, SIZE);
639 >        assertIteratorExhausted(it);
640 >
641 >        it = q.iterator();
642 >        for (i = 0; it.hasNext(); i++)
643              assertEquals(it.next(), q.take());
644 <        }
644 >        assertEquals(i, SIZE);
645 >        assertIteratorExhausted(it);
646 >    }
647 >
648 >    /**
649 >     * iterator of empty collection has no elements
650 >     */
651 >    public void testEmptyIterator() {
652 >        assertIteratorExhausted(new LinkedBlockingQueue().iterator());
653      }
654  
655      /**
# Line 705 | Line 720 | public class LinkedBlockingQueueTest ext
720          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
721          q.add(one);
722          q.add(two);
708        ExecutorService executor = Executors.newFixedThreadPool(2);
723          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
724 <        executor.execute(new CheckedRunnable() {
725 <            public void realRun() throws InterruptedException {
726 <                assertFalse(q.offer(three));
727 <                threadsStarted.await();
728 <                assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
729 <                assertEquals(0, q.remainingCapacity());
730 <            }});
731 <
732 <        executor.execute(new CheckedRunnable() {
733 <            public void realRun() throws InterruptedException {
734 <                threadsStarted.await();
735 <                assertSame(one, q.take());
736 <            }});
737 <
738 <        joinPool(executor);
724 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
725 >        try (PoolCleaner cleaner = cleaner(executor)) {
726 >            executor.execute(new CheckedRunnable() {
727 >                public void realRun() throws InterruptedException {
728 >                    assertFalse(q.offer(three));
729 >                    threadsStarted.await();
730 >                    assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
731 >                    assertEquals(0, q.remainingCapacity());
732 >                }});
733 >
734 >            executor.execute(new CheckedRunnable() {
735 >                public void realRun() throws InterruptedException {
736 >                    threadsStarted.await();
737 >                    assertSame(one, q.take());
738 >                }});
739 >        }
740      }
741  
742      /**
# Line 730 | Line 745 | public class LinkedBlockingQueueTest ext
745      public void testPollInExecutor() {
746          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
747          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
748 <        ExecutorService executor = Executors.newFixedThreadPool(2);
749 <        executor.execute(new CheckedRunnable() {
750 <            public void realRun() throws InterruptedException {
751 <                assertNull(q.poll());
752 <                threadsStarted.await();
753 <                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
754 <                checkEmpty(q);
755 <            }});
756 <
757 <        executor.execute(new CheckedRunnable() {
758 <            public void realRun() throws InterruptedException {
759 <                threadsStarted.await();
760 <                q.put(one);
761 <            }});
762 <
763 <        joinPool(executor);
748 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
749 >        try (PoolCleaner cleaner = cleaner(executor)) {
750 >            executor.execute(new CheckedRunnable() {
751 >                public void realRun() throws InterruptedException {
752 >                    assertNull(q.poll());
753 >                    threadsStarted.await();
754 >                    assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
755 >                    checkEmpty(q);
756 >                }});
757 >
758 >            executor.execute(new CheckedRunnable() {
759 >                public void realRun() throws InterruptedException {
760 >                    threadsStarted.await();
761 >                    q.put(one);
762 >                }});
763 >        }
764      }
765  
766      /**
# Line 755 | Line 770 | public class LinkedBlockingQueueTest ext
770          Queue x = populatedQueue(SIZE);
771          Queue y = serialClone(x);
772  
773 <        assertTrue(x != y);
773 >        assertNotSame(x, y);
774          assertEquals(x.size(), y.size());
775          assertEquals(x.toString(), y.toString());
776          assertTrue(Arrays.equals(x.toArray(), y.toArray()));
# Line 797 | Line 812 | public class LinkedBlockingQueueTest ext
812          final LinkedBlockingQueue q = populatedQueue(SIZE);
813          Thread t = new Thread(new CheckedRunnable() {
814              public void realRun() throws InterruptedException {
815 <                q.put(new Integer(SIZE+1));
815 >                q.put(new Integer(SIZE + 1));
816              }});
817  
818          t.start();
# Line 822 | Line 837 | public class LinkedBlockingQueueTest ext
837              q.drainTo(l, i);
838              int k = (i < SIZE) ? i : SIZE;
839              assertEquals(k, l.size());
840 <            assertEquals(SIZE-k, q.size());
840 >            assertEquals(SIZE - k, q.size());
841              for (int j = 0; j < k; ++j)
842                  assertEquals(l.get(j), new Integer(j));
843 <            while (q.poll() != null) ;
843 >            do {} while (q.poll() != null);
844 >        }
845 >    }
846 >
847 >    /**
848 >     * remove(null), contains(null) always return false
849 >     */
850 >    public void testNeverContainsNull() {
851 >        Collection<?>[] qs = {
852 >            new LinkedBlockingQueue<Object>(),
853 >            populatedQueue(2),
854 >        };
855 >
856 >        for (Collection<?> q : qs) {
857 >            assertFalse(q.contains(null));
858 >            assertFalse(q.remove(null));
859          }
860      }
861  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines