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.48 by jsr166, Sat Nov 26 05:19:17 2011 UTC vs.
Revision 1.79 by jsr166, Sun Aug 11 22:29:27 2019 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() {
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) {
62 <        LinkedBlockingQueue<Integer> q =
53 <            new LinkedBlockingQueue<Integer>(n);
61 >    private static LinkedBlockingQueue<Integer> populatedQueue(int n) {
62 >        LinkedBlockingQueue<Integer> q = new LinkedBlockingQueue<>(n);
63          assertTrue(q.isEmpty());
64          for (int i = 0; i < n; i++)
65              assertTrue(q.offer(new Integer(i)));
66          assertFalse(q.isEmpty());
67          assertEquals(0, q.remainingCapacity());
68          assertEquals(n, q.size());
69 +        assertEquals((Integer) 0, q.peek());
70          return q;
71      }
72  
# Line 106 | Line 116 | public class LinkedBlockingQueueTest ext
116       */
117      public void testConstructor5() {
118          Integer[] ints = new Integer[SIZE];
119 <        for (int i = 0; i < SIZE-1; ++i)
119 >        for (int i = 0; i < SIZE - 1; ++i)
120              ints[i] = new Integer(i);
121          Collection<Integer> elements = Arrays.asList(ints);
122          try {
# Line 146 | Line 156 | public class LinkedBlockingQueueTest ext
156       * remainingCapacity decreases on add, increases on remove
157       */
158      public void testRemainingCapacity() {
159 <        LinkedBlockingQueue q = populatedQueue(SIZE);
159 >        BlockingQueue q = populatedQueue(SIZE);
160          for (int i = 0; i < SIZE; ++i) {
161              assertEquals(i, q.remainingCapacity());
162 <            assertEquals(SIZE-i, q.size());
163 <            q.remove();
162 >            assertEquals(SIZE, q.size() + q.remainingCapacity());
163 >            assertEquals(i, q.remove());
164          }
165          for (int i = 0; i < SIZE; ++i) {
166 <            assertEquals(SIZE-i, q.remainingCapacity());
167 <            assertEquals(i, q.size());
168 <            q.add(new Integer(i));
166 >            assertEquals(SIZE - i, q.remainingCapacity());
167 >            assertEquals(SIZE, q.size() + q.remainingCapacity());
168 >            assertTrue(q.add(i));
169          }
170      }
171  
# Line 200 | Line 210 | public class LinkedBlockingQueueTest ext
210      public void testAddAll3() {
211          LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
212          Integer[] ints = new Integer[SIZE];
213 <        for (int i = 0; i < SIZE-1; ++i)
213 >        for (int i = 0; i < SIZE - 1; ++i)
214              ints[i] = new Integer(i);
215          Collection<Integer> elements = Arrays.asList(ints);
216          try {
# Line 245 | Line 255 | public class LinkedBlockingQueueTest ext
255      public void testPut() throws InterruptedException {
256          LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
257          for (int i = 0; i < SIZE; ++i) {
258 <            Integer I = new Integer(i);
259 <            q.put(I);
260 <            assertTrue(q.contains(I));
258 >            Integer x = new Integer(i);
259 >            q.put(x);
260 >            assertTrue(q.contains(x));
261          }
262          assertEquals(0, q.remainingCapacity());
263      }
# Line 281 | Line 291 | public class LinkedBlockingQueueTest ext
291              }});
292  
293          await(pleaseInterrupt);
294 <        assertThreadStaysAlive(t);
294 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
295          t.interrupt();
296          awaitTermination(t);
297          assertEquals(SIZE, q.size());
# Line 303 | Line 313 | public class LinkedBlockingQueueTest ext
313                  pleaseTake.countDown();
314                  q.put(86);
315  
316 +                Thread.currentThread().interrupt();
317 +                try {
318 +                    q.put(99);
319 +                    shouldThrow();
320 +                } catch (InterruptedException success) {}
321 +                assertFalse(Thread.interrupted());
322 +
323                  pleaseInterrupt.countDown();
324                  try {
325                      q.put(99);
# Line 316 | Line 333 | public class LinkedBlockingQueueTest ext
333          assertEquals(0, q.take());
334  
335          await(pleaseInterrupt);
336 <        assertThreadStaysAlive(t);
336 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
337          t.interrupt();
338          awaitTermination(t);
339          assertEquals(0, q.remainingCapacity());
# Line 333 | Line 350 | public class LinkedBlockingQueueTest ext
350                  q.put(new Object());
351                  q.put(new Object());
352                  long startTime = System.nanoTime();
353 +
354                  assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS));
355                  assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
356 +
357 +                Thread.currentThread().interrupt();
358 +                try {
359 +                    q.offer(new Object(), randomTimeout(), randomTimeUnit());
360 +                    shouldThrow();
361 +                } catch (InterruptedException success) {}
362 +                assertFalse(Thread.interrupted());
363 +
364                  pleaseInterrupt.countDown();
365                  try {
366 <                    q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
366 >                    q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
367                      shouldThrow();
368                  } catch (InterruptedException success) {}
369 +                assertFalse(Thread.interrupted());
370 +
371 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
372              }});
373  
374          await(pleaseInterrupt);
375 <        assertThreadStaysAlive(t);
375 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
376          t.interrupt();
377          awaitTermination(t);
378      }
# Line 366 | Line 395 | public class LinkedBlockingQueueTest ext
395          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
396          Thread t = newStartedThread(new CheckedRunnable() {
397              public void realRun() throws InterruptedException {
398 <                for (int i = 0; i < SIZE; ++i) {
370 <                    assertEquals(i, q.take());
371 <                }
398 >                for (int i = 0; i < SIZE; i++) assertEquals(i, q.take());
399  
400                  Thread.currentThread().interrupt();
401                  try {
# Line 386 | Line 413 | public class LinkedBlockingQueueTest ext
413              }});
414  
415          await(pleaseInterrupt);
416 <        assertThreadStaysAlive(t);
416 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
417          t.interrupt();
418          awaitTermination(t);
419      }
# Line 435 | Line 462 | public class LinkedBlockingQueueTest ext
462       */
463      public void testInterruptedTimedPoll() throws InterruptedException {
464          final BlockingQueue<Integer> q = populatedQueue(SIZE);
465 <        final CountDownLatch aboutToWait = new CountDownLatch(1);
465 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
466          Thread t = newStartedThread(new CheckedRunnable() {
467              public void realRun() throws InterruptedException {
468 <                for (int i = 0; i < SIZE; ++i) {
469 <                    long t0 = System.nanoTime();
468 >                long startTime = System.nanoTime();
469 >                for (int i = 0; i < SIZE; i++)
470                      assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
471 <                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
472 <                }
473 <                long t0 = System.nanoTime();
474 <                aboutToWait.countDown();
471 >
472 >                Thread.currentThread().interrupt();
473 >                try {
474 >                    q.poll(randomTimeout(), randomTimeUnit());
475 >                    shouldThrow();
476 >                } catch (InterruptedException success) {}
477 >                assertFalse(Thread.interrupted());
478 >
479 >                pleaseInterrupt.countDown();
480                  try {
481 <                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
481 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
482                      shouldThrow();
483 <                } catch (InterruptedException success) {
484 <                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
485 <                }
483 >                } catch (InterruptedException success) {}
484 >                assertFalse(Thread.interrupted());
485 >
486 >                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
487              }});
488  
489 <        aboutToWait.await();
490 <        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
489 >        await(pleaseInterrupt);
490 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
491          t.interrupt();
492 <        awaitTermination(t, MEDIUM_DELAY_MS);
492 >        awaitTermination(t);
493          checkEmpty(q);
494      }
495  
# Line 513 | Line 546 | public class LinkedBlockingQueueTest ext
546          assertTrue(q.remove(new Integer(1)));
547          assertTrue(q.remove(new Integer(2)));
548          assertTrue(q.add(new Integer(3)));
549 <        assertTrue(q.take() != null);
549 >        assertNotNull(q.take());
550      }
551  
552      /**
# Line 572 | Line 605 | public class LinkedBlockingQueueTest ext
605                  assertTrue(changed);
606  
607              assertTrue(q.containsAll(p));
608 <            assertEquals(SIZE-i, q.size());
608 >            assertEquals(SIZE - i, q.size());
609              p.remove();
610          }
611      }
# Line 585 | Line 618 | public class LinkedBlockingQueueTest ext
618              LinkedBlockingQueue q = populatedQueue(SIZE);
619              LinkedBlockingQueue p = populatedQueue(i);
620              assertTrue(q.removeAll(p));
621 <            assertEquals(SIZE-i, q.size());
621 >            assertEquals(SIZE - i, q.size());
622              for (int j = 0; j < i; ++j) {
623 <                Integer I = (Integer)(p.remove());
624 <                assertFalse(q.contains(I));
623 >                Integer x = (Integer)(p.remove());
624 >                assertFalse(q.contains(x));
625              }
626          }
627      }
# Line 598 | Line 631 | public class LinkedBlockingQueueTest ext
631       */
632      public void testToArray() {
633          LinkedBlockingQueue q = populatedQueue(SIZE);
634 <        Object[] o = q.toArray();
635 <        for (int i = 0; i < o.length; i++)
636 <            assertSame(o[i], q.poll());
634 >        Object[] a = q.toArray();
635 >        assertSame(Object[].class, a.getClass());
636 >        for (Object o : a)
637 >            assertSame(o, q.poll());
638 >        assertTrue(q.isEmpty());
639      }
640  
641      /**
# Line 611 | Line 646 | public class LinkedBlockingQueueTest ext
646          Integer[] ints = new Integer[SIZE];
647          Integer[] array = q.toArray(ints);
648          assertSame(ints, array);
649 <        for (int i = 0; i < ints.length; i++)
650 <            assertSame(ints[i], q.poll());
649 >        for (Integer o : ints)
650 >            assertSame(o, q.poll());
651 >        assertTrue(q.isEmpty());
652      }
653  
654      /**
# Line 632 | Line 668 | public class LinkedBlockingQueueTest ext
668      public void testIterator() throws InterruptedException {
669          LinkedBlockingQueue q = populatedQueue(SIZE);
670          Iterator it = q.iterator();
671 <        while (it.hasNext()) {
671 >        int i;
672 >        for (i = 0; it.hasNext(); i++)
673 >            assertTrue(q.contains(it.next()));
674 >        assertEquals(i, SIZE);
675 >        assertIteratorExhausted(it);
676 >
677 >        it = q.iterator();
678 >        for (i = 0; it.hasNext(); i++)
679              assertEquals(it.next(), q.take());
680 <        }
680 >        assertEquals(i, SIZE);
681 >        assertIteratorExhausted(it);
682 >    }
683 >
684 >    /**
685 >     * iterator of empty collection has no elements
686 >     */
687 >    public void testEmptyIterator() {
688 >        assertIteratorExhausted(new LinkedBlockingQueue().iterator());
689      }
690  
691      /**
# Line 705 | Line 756 | public class LinkedBlockingQueueTest ext
756          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
757          q.add(one);
758          q.add(two);
708        ExecutorService executor = Executors.newFixedThreadPool(2);
759          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
760 <        executor.execute(new CheckedRunnable() {
761 <            public void realRun() throws InterruptedException {
762 <                assertFalse(q.offer(three));
763 <                threadsStarted.await();
764 <                assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
765 <                assertEquals(0, q.remainingCapacity());
766 <            }});
767 <
768 <        executor.execute(new CheckedRunnable() {
769 <            public void realRun() throws InterruptedException {
770 <                threadsStarted.await();
771 <                assertSame(one, q.take());
772 <            }});
773 <
774 <        joinPool(executor);
760 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
761 >        try (PoolCleaner cleaner = cleaner(executor)) {
762 >            executor.execute(new CheckedRunnable() {
763 >                public void realRun() throws InterruptedException {
764 >                    assertFalse(q.offer(three));
765 >                    threadsStarted.await();
766 >                    assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
767 >                    assertEquals(0, q.remainingCapacity());
768 >                }});
769 >
770 >            executor.execute(new CheckedRunnable() {
771 >                public void realRun() throws InterruptedException {
772 >                    threadsStarted.await();
773 >                    assertSame(one, q.take());
774 >                }});
775 >        }
776      }
777  
778      /**
# Line 730 | Line 781 | public class LinkedBlockingQueueTest ext
781      public void testPollInExecutor() {
782          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
783          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
784 <        ExecutorService executor = Executors.newFixedThreadPool(2);
785 <        executor.execute(new CheckedRunnable() {
786 <            public void realRun() throws InterruptedException {
787 <                assertNull(q.poll());
788 <                threadsStarted.await();
789 <                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
790 <                checkEmpty(q);
791 <            }});
792 <
793 <        executor.execute(new CheckedRunnable() {
794 <            public void realRun() throws InterruptedException {
795 <                threadsStarted.await();
796 <                q.put(one);
797 <            }});
798 <
799 <        joinPool(executor);
784 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
785 >        try (PoolCleaner cleaner = cleaner(executor)) {
786 >            executor.execute(new CheckedRunnable() {
787 >                public void realRun() throws InterruptedException {
788 >                    assertNull(q.poll());
789 >                    threadsStarted.await();
790 >                    assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
791 >                    checkEmpty(q);
792 >                }});
793 >
794 >            executor.execute(new CheckedRunnable() {
795 >                public void realRun() throws InterruptedException {
796 >                    threadsStarted.await();
797 >                    q.put(one);
798 >                }});
799 >        }
800      }
801  
802      /**
803 <     * A deserialized serialized queue has same elements in same order
803 >     * A deserialized/reserialized queue has same elements in same order
804       */
805      public void testSerialization() throws Exception {
806          Queue x = populatedQueue(SIZE);
807          Queue y = serialClone(x);
808  
809 <        assertTrue(x != y);
809 >        assertNotSame(x, y);
810          assertEquals(x.size(), y.size());
811          assertEquals(x.toString(), y.toString());
812          assertTrue(Arrays.equals(x.toArray(), y.toArray()));
# Line 797 | Line 848 | public class LinkedBlockingQueueTest ext
848          final LinkedBlockingQueue q = populatedQueue(SIZE);
849          Thread t = new Thread(new CheckedRunnable() {
850              public void realRun() throws InterruptedException {
851 <                q.put(new Integer(SIZE+1));
851 >                q.put(new Integer(SIZE + 1));
852              }});
853  
854          t.start();
# Line 821 | Line 872 | public class LinkedBlockingQueueTest ext
872              ArrayList l = new ArrayList();
873              q.drainTo(l, i);
874              int k = (i < SIZE) ? i : SIZE;
875 <            assertEquals(l.size(), k);
876 <            assertEquals(q.size(), SIZE-k);
875 >            assertEquals(k, l.size());
876 >            assertEquals(SIZE - k, q.size());
877              for (int j = 0; j < k; ++j)
878                  assertEquals(l.get(j), new Integer(j));
879 <            while (q.poll() != null) ;
879 >            do {} while (q.poll() != null);
880 >        }
881 >    }
882 >
883 >    /**
884 >     * remove(null), contains(null) always return false
885 >     */
886 >    public void testNeverContainsNull() {
887 >        Collection<?>[] qs = {
888 >            new LinkedBlockingQueue<Object>(),
889 >            populatedQueue(2),
890 >        };
891 >
892 >        for (Collection<?> q : qs) {
893 >            assertFalse(q.contains(null));
894 >            assertFalse(q.remove(null));
895          }
896      }
897  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines