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.46 by jsr166, Tue May 31 16:16:24 2011 UTC vs.
Revision 1.74 by jsr166, Sun May 14 04:02:06 2017 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 30 | Line 32 | public class LinkedBlockingQueueTest ext
32  
33      public static class Bounded extends BlockingQueueTest {
34          protected BlockingQueue emptyCollection() {
35 <            return new LinkedBlockingQueue(20);
35 >            return new LinkedBlockingQueue(SIZE);
36          }
37      }
38  
39      public static void main(String[] args) {
40 <        junit.textui.TestRunner.run(suite());
40 >        main(suite(), args);
41      }
42  
43      public static Test suite() {
44 +        class Implementation implements CollectionImplementation {
45 +            public Class<?> klazz() { return LinkedBlockingQueue.class; }
46 +            public Collection emptyCollection() { return new LinkedBlockingQueue(); }
47 +            public Object makeElement(int i) { return i; }
48 +            public boolean isConcurrent() { return true; }
49 +            public boolean permitsNulls() { return false; }
50 +        }
51          return newTestSuite(LinkedBlockingQueueTest.class,
52                              new Unbounded().testSuite(),
53 <                            new Bounded().testSuite());
53 >                            new Bounded().testSuite(),
54 >                            CollectionTest.testSuite(new Implementation()));
55      }
56  
57      /**
58 <     * Create a queue of given size containing consecutive
59 <     * Integers 0 ... n.
58 >     * Returns a new queue of given size containing consecutive
59 >     * Integers 0 ... n - 1.
60       */
61 <    private LinkedBlockingQueue<Integer> populatedQueue(int n) {
61 >    private static LinkedBlockingQueue<Integer> populatedQueue(int n) {
62          LinkedBlockingQueue<Integer> q =
63              new LinkedBlockingQueue<Integer>(n);
64          assertTrue(q.isEmpty());
# Line 57 | Line 67 | public class LinkedBlockingQueueTest ext
67          assertFalse(q.isEmpty());
68          assertEquals(0, q.remainingCapacity());
69          assertEquals(n, q.size());
70 +        assertEquals((Integer) 0, q.peek());
71          return q;
72      }
73  
# Line 106 | Line 117 | public class LinkedBlockingQueueTest ext
117       */
118      public void testConstructor5() {
119          Integer[] ints = new Integer[SIZE];
120 <        for (int i = 0; i < SIZE-1; ++i)
120 >        for (int i = 0; i < SIZE - 1; ++i)
121              ints[i] = new Integer(i);
122          Collection<Integer> elements = Arrays.asList(ints);
123          try {
# Line 146 | Line 157 | public class LinkedBlockingQueueTest ext
157       * remainingCapacity decreases on add, increases on remove
158       */
159      public void testRemainingCapacity() {
160 <        LinkedBlockingQueue q = populatedQueue(SIZE);
160 >        BlockingQueue q = populatedQueue(SIZE);
161          for (int i = 0; i < SIZE; ++i) {
162              assertEquals(i, q.remainingCapacity());
163 <            assertEquals(SIZE-i, q.size());
164 <            q.remove();
163 >            assertEquals(SIZE, q.size() + q.remainingCapacity());
164 >            assertEquals(i, q.remove());
165          }
166          for (int i = 0; i < SIZE; ++i) {
167 <            assertEquals(SIZE-i, q.remainingCapacity());
168 <            assertEquals(i, q.size());
169 <            q.add(new Integer(i));
167 >            assertEquals(SIZE - i, q.remainingCapacity());
168 >            assertEquals(SIZE, q.size() + q.remainingCapacity());
169 >            assertTrue(q.add(i));
170          }
171      }
172  
# Line 200 | Line 211 | public class LinkedBlockingQueueTest ext
211      public void testAddAll3() {
212          LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
213          Integer[] ints = new Integer[SIZE];
214 <        for (int i = 0; i < SIZE-1; ++i)
214 >        for (int i = 0; i < SIZE - 1; ++i)
215              ints[i] = new Integer(i);
216          Collection<Integer> elements = Arrays.asList(ints);
217          try {
# Line 245 | Line 256 | public class LinkedBlockingQueueTest ext
256      public void testPut() throws InterruptedException {
257          LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
258          for (int i = 0; i < SIZE; ++i) {
259 <            Integer I = new Integer(i);
260 <            q.put(I);
261 <            assertTrue(q.contains(I));
259 >            Integer x = new Integer(i);
260 >            q.put(x);
261 >            assertTrue(q.contains(x));
262          }
263          assertEquals(0, q.remainingCapacity());
264      }
# Line 281 | Line 292 | public class LinkedBlockingQueueTest ext
292              }});
293  
294          await(pleaseInterrupt);
295 <        assertThreadStaysAlive(t);
295 >        assertThreadBlocks(t, Thread.State.WAITING);
296          t.interrupt();
297          awaitTermination(t);
298          assertEquals(SIZE, q.size());
# Line 303 | Line 314 | public class LinkedBlockingQueueTest ext
314                  pleaseTake.countDown();
315                  q.put(86);
316  
317 +                Thread.currentThread().interrupt();
318 +                try {
319 +                    q.put(99);
320 +                    shouldThrow();
321 +                } catch (InterruptedException success) {}
322 +                assertFalse(Thread.interrupted());
323 +
324                  pleaseInterrupt.countDown();
325                  try {
326                      q.put(99);
# Line 312 | Line 330 | public class LinkedBlockingQueueTest ext
330              }});
331  
332          await(pleaseTake);
333 <        assertEquals(q.remainingCapacity(), 0);
333 >        assertEquals(0, q.remainingCapacity());
334          assertEquals(0, q.take());
335  
336          await(pleaseInterrupt);
337 <        assertThreadStaysAlive(t);
337 >        assertThreadBlocks(t, Thread.State.WAITING);
338          t.interrupt();
339          awaitTermination(t);
340 <        assertEquals(q.remainingCapacity(), 0);
340 >        assertEquals(0, q.remainingCapacity());
341      }
342  
343      /**
# Line 340 | Line 358 | public class LinkedBlockingQueueTest ext
358                      q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
359                      shouldThrow();
360                  } catch (InterruptedException success) {}
361 +                assertFalse(Thread.interrupted());
362              }});
363  
364          await(pleaseInterrupt);
365 <        assertThreadStaysAlive(t);
365 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
366          t.interrupt();
367          awaitTermination(t);
368      }
# Line 366 | Line 385 | public class LinkedBlockingQueueTest ext
385          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
386          Thread t = newStartedThread(new CheckedRunnable() {
387              public void realRun() throws InterruptedException {
388 <                for (int i = 0; i < SIZE; ++i) {
370 <                    assertEquals(i, q.take());
371 <                }
388 >                for (int i = 0; i < SIZE; i++) assertEquals(i, q.take());
389  
390                  Thread.currentThread().interrupt();
391                  try {
# Line 386 | Line 403 | public class LinkedBlockingQueueTest ext
403              }});
404  
405          await(pleaseInterrupt);
406 <        assertThreadStaysAlive(t);
406 >        assertThreadBlocks(t, Thread.State.WAITING);
407          t.interrupt();
408          awaitTermination(t);
409      }
# Line 435 | Line 452 | public class LinkedBlockingQueueTest ext
452       */
453      public void testInterruptedTimedPoll() throws InterruptedException {
454          final BlockingQueue<Integer> q = populatedQueue(SIZE);
455 <        final CountDownLatch aboutToWait = new CountDownLatch(1);
455 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
456          Thread t = newStartedThread(new CheckedRunnable() {
457              public void realRun() throws InterruptedException {
458 <                for (int i = 0; i < SIZE; ++i) {
459 <                    long t0 = System.nanoTime();
458 >                long startTime = System.nanoTime();
459 >                for (int i = 0; i < SIZE; i++)
460                      assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
461 <                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
462 <                }
463 <                long t0 = System.nanoTime();
464 <                aboutToWait.countDown();
461 >
462 >                Thread.currentThread().interrupt();
463 >                try {
464 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
465 >                    shouldThrow();
466 >                } catch (InterruptedException success) {}
467 >                assertFalse(Thread.interrupted());
468 >
469 >                pleaseInterrupt.countDown();
470                  try {
471 <                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
471 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
472                      shouldThrow();
473 <                } catch (InterruptedException success) {
474 <                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
475 <                }
473 >                } catch (InterruptedException success) {}
474 >                assertFalse(Thread.interrupted());
475 >
476 >                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
477              }});
478  
479 <        aboutToWait.await();
480 <        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
479 >        await(pleaseInterrupt);
480 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
481          t.interrupt();
482 <        awaitTermination(t, MEDIUM_DELAY_MS);
482 >        awaitTermination(t);
483          checkEmpty(q);
484      }
485  
# Line 504 | Line 527 | public class LinkedBlockingQueueTest ext
527      }
528  
529      /**
507     * remove(x) removes x and returns true if present
508     */
509    public void testRemoveElement() {
510        LinkedBlockingQueue q = populatedQueue(SIZE);
511        for (int i = 1; i < SIZE; i+=2) {
512            assertTrue(q.contains(i));
513            assertTrue(q.remove(i));
514            assertFalse(q.contains(i));
515            assertTrue(q.contains(i-1));
516        }
517        for (int i = 0; i < SIZE; i+=2) {
518            assertTrue(q.contains(i));
519            assertTrue(q.remove(i));
520            assertFalse(q.contains(i));
521            assertFalse(q.remove(i+1));
522            assertFalse(q.contains(i+1));
523        }
524        assertTrue(q.isEmpty());
525    }
526
527    /**
530       * An add following remove(x) succeeds
531       */
532      public void testRemoveElementAndAdd() throws InterruptedException {
# Line 534 | Line 536 | public class LinkedBlockingQueueTest ext
536          assertTrue(q.remove(new Integer(1)));
537          assertTrue(q.remove(new Integer(2)));
538          assertTrue(q.add(new Integer(3)));
539 <        assertTrue(q.take() != null);
539 >        assertNotNull(q.take());
540      }
541  
542      /**
# Line 593 | Line 595 | public class LinkedBlockingQueueTest ext
595                  assertTrue(changed);
596  
597              assertTrue(q.containsAll(p));
598 <            assertEquals(SIZE-i, q.size());
598 >            assertEquals(SIZE - i, q.size());
599              p.remove();
600          }
601      }
# Line 606 | Line 608 | public class LinkedBlockingQueueTest ext
608              LinkedBlockingQueue q = populatedQueue(SIZE);
609              LinkedBlockingQueue p = populatedQueue(i);
610              assertTrue(q.removeAll(p));
611 <            assertEquals(SIZE-i, q.size());
611 >            assertEquals(SIZE - i, q.size());
612              for (int j = 0; j < i; ++j) {
613 <                Integer I = (Integer)(p.remove());
614 <                assertFalse(q.contains(I));
613 >                Integer x = (Integer)(p.remove());
614 >                assertFalse(q.contains(x));
615              }
616          }
617      }
# Line 653 | Line 655 | public class LinkedBlockingQueueTest ext
655      public void testIterator() throws InterruptedException {
656          LinkedBlockingQueue q = populatedQueue(SIZE);
657          Iterator it = q.iterator();
658 <        while (it.hasNext()) {
658 >        int i;
659 >        for (i = 0; it.hasNext(); i++)
660 >            assertTrue(q.contains(it.next()));
661 >        assertEquals(i, SIZE);
662 >        assertIteratorExhausted(it);
663 >
664 >        it = q.iterator();
665 >        for (i = 0; it.hasNext(); i++)
666              assertEquals(it.next(), q.take());
667 <        }
667 >        assertEquals(i, SIZE);
668 >        assertIteratorExhausted(it);
669 >    }
670 >
671 >    /**
672 >     * iterator of empty collection has no elements
673 >     */
674 >    public void testEmptyIterator() {
675 >        assertIteratorExhausted(new LinkedBlockingQueue().iterator());
676      }
677  
678      /**
# Line 726 | Line 743 | public class LinkedBlockingQueueTest ext
743          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
744          q.add(one);
745          q.add(two);
729        ExecutorService executor = Executors.newFixedThreadPool(2);
746          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
747 <        executor.execute(new CheckedRunnable() {
748 <            public void realRun() throws InterruptedException {
749 <                assertFalse(q.offer(three));
750 <                threadsStarted.await();
751 <                assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
752 <                assertEquals(0, q.remainingCapacity());
753 <            }});
754 <
755 <        executor.execute(new CheckedRunnable() {
756 <            public void realRun() throws InterruptedException {
757 <                threadsStarted.await();
758 <                assertSame(one, q.take());
759 <            }});
760 <
761 <        joinPool(executor);
747 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
748 >        try (PoolCleaner cleaner = cleaner(executor)) {
749 >            executor.execute(new CheckedRunnable() {
750 >                public void realRun() throws InterruptedException {
751 >                    assertFalse(q.offer(three));
752 >                    threadsStarted.await();
753 >                    assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
754 >                    assertEquals(0, q.remainingCapacity());
755 >                }});
756 >
757 >            executor.execute(new CheckedRunnable() {
758 >                public void realRun() throws InterruptedException {
759 >                    threadsStarted.await();
760 >                    assertSame(one, q.take());
761 >                }});
762 >        }
763      }
764  
765      /**
# Line 751 | Line 768 | public class LinkedBlockingQueueTest ext
768      public void testPollInExecutor() {
769          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
770          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
771 <        ExecutorService executor = Executors.newFixedThreadPool(2);
772 <        executor.execute(new CheckedRunnable() {
773 <            public void realRun() throws InterruptedException {
774 <                assertNull(q.poll());
775 <                threadsStarted.await();
776 <                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
777 <                checkEmpty(q);
778 <            }});
779 <
780 <        executor.execute(new CheckedRunnable() {
781 <            public void realRun() throws InterruptedException {
782 <                threadsStarted.await();
783 <                q.put(one);
784 <            }});
785 <
786 <        joinPool(executor);
771 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
772 >        try (PoolCleaner cleaner = cleaner(executor)) {
773 >            executor.execute(new CheckedRunnable() {
774 >                public void realRun() throws InterruptedException {
775 >                    assertNull(q.poll());
776 >                    threadsStarted.await();
777 >                    assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
778 >                    checkEmpty(q);
779 >                }});
780 >
781 >            executor.execute(new CheckedRunnable() {
782 >                public void realRun() throws InterruptedException {
783 >                    threadsStarted.await();
784 >                    q.put(one);
785 >                }});
786 >        }
787      }
788  
789      /**
# Line 776 | Line 793 | public class LinkedBlockingQueueTest ext
793          Queue x = populatedQueue(SIZE);
794          Queue y = serialClone(x);
795  
796 <        assertTrue(x != y);
796 >        assertNotSame(x, y);
797          assertEquals(x.size(), y.size());
798          assertEquals(x.toString(), y.toString());
799          assertTrue(Arrays.equals(x.toArray(), y.toArray()));
# Line 794 | Line 811 | public class LinkedBlockingQueueTest ext
811          LinkedBlockingQueue q = populatedQueue(SIZE);
812          ArrayList l = new ArrayList();
813          q.drainTo(l);
814 <        assertEquals(q.size(), 0);
815 <        assertEquals(l.size(), SIZE);
814 >        assertEquals(0, q.size());
815 >        assertEquals(SIZE, l.size());
816          for (int i = 0; i < SIZE; ++i)
817              assertEquals(l.get(i), new Integer(i));
818          q.add(zero);
# Line 805 | Line 822 | public class LinkedBlockingQueueTest ext
822          assertTrue(q.contains(one));
823          l.clear();
824          q.drainTo(l);
825 <        assertEquals(q.size(), 0);
826 <        assertEquals(l.size(), 2);
825 >        assertEquals(0, q.size());
826 >        assertEquals(2, l.size());
827          for (int i = 0; i < 2; ++i)
828              assertEquals(l.get(i), new Integer(i));
829      }
# Line 818 | Line 835 | public class LinkedBlockingQueueTest ext
835          final LinkedBlockingQueue q = populatedQueue(SIZE);
836          Thread t = new Thread(new CheckedRunnable() {
837              public void realRun() throws InterruptedException {
838 <                q.put(new Integer(SIZE+1));
838 >                q.put(new Integer(SIZE + 1));
839              }});
840  
841          t.start();
# Line 842 | Line 859 | public class LinkedBlockingQueueTest ext
859              ArrayList l = new ArrayList();
860              q.drainTo(l, i);
861              int k = (i < SIZE) ? i : SIZE;
862 <            assertEquals(l.size(), k);
863 <            assertEquals(q.size(), SIZE-k);
862 >            assertEquals(k, l.size());
863 >            assertEquals(SIZE - k, q.size());
864              for (int j = 0; j < k; ++j)
865                  assertEquals(l.get(j), new Integer(j));
866 <            while (q.poll() != null) ;
866 >            do {} while (q.poll() != null);
867 >        }
868 >    }
869 >
870 >    /**
871 >     * remove(null), contains(null) always return false
872 >     */
873 >    public void testNeverContainsNull() {
874 >        Collection<?>[] qs = {
875 >            new LinkedBlockingQueue<Object>(),
876 >            populatedQueue(2),
877 >        };
878 >
879 >        for (Collection<?> q : qs) {
880 >            assertFalse(q.contains(null));
881 >            assertFalse(q.remove(null));
882          }
883      }
884  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines