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.81 by jsr166, Thu Sep 5 21:11:13 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(), LONGER_DELAY_MS, MILLISECONDS);
367                      shouldThrow();
368                  } catch (InterruptedException success) {}
369 +                assertFalse(Thread.interrupted());
370              }});
371  
372          await(pleaseInterrupt);
373 <        assertThreadStaysAlive(t);
373 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
374          t.interrupt();
375          awaitTermination(t);
376      }
# Line 366 | Line 393 | public class LinkedBlockingQueueTest ext
393          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
394          Thread t = newStartedThread(new CheckedRunnable() {
395              public void realRun() throws InterruptedException {
396 <                for (int i = 0; i < SIZE; ++i) {
370 <                    assertEquals(i, q.take());
371 <                }
396 >                for (int i = 0; i < SIZE; i++) assertEquals(i, q.take());
397  
398                  Thread.currentThread().interrupt();
399                  try {
# Line 386 | Line 411 | public class LinkedBlockingQueueTest ext
411              }});
412  
413          await(pleaseInterrupt);
414 <        assertThreadStaysAlive(t);
414 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
415          t.interrupt();
416          awaitTermination(t);
417      }
# Line 435 | Line 460 | public class LinkedBlockingQueueTest ext
460       */
461      public void testInterruptedTimedPoll() throws InterruptedException {
462          final BlockingQueue<Integer> q = populatedQueue(SIZE);
463 <        final CountDownLatch aboutToWait = new CountDownLatch(1);
463 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
464          Thread t = newStartedThread(new CheckedRunnable() {
465              public void realRun() throws InterruptedException {
466 <                for (int i = 0; i < SIZE; ++i) {
442 <                    long t0 = System.nanoTime();
466 >                for (int i = 0; i < SIZE; i++)
467                      assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
468 <                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
469 <                }
446 <                long t0 = System.nanoTime();
447 <                aboutToWait.countDown();
468 >
469 >                Thread.currentThread().interrupt();
470                  try {
471 <                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
471 >                    q.poll(randomTimeout(), randomTimeUnit());
472                      shouldThrow();
473 <                } catch (InterruptedException success) {
474 <                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
475 <                }
473 >                } catch (InterruptedException success) {}
474 >                assertFalse(Thread.interrupted());
475 >
476 >                pleaseInterrupt.countDown();
477 >                try {
478 >                    q.poll(LONGER_DELAY_MS, MILLISECONDS);
479 >                    shouldThrow();
480 >                } catch (InterruptedException success) {}
481 >                assertFalse(Thread.interrupted());
482              }});
483  
484 <        aboutToWait.await();
485 <        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
484 >        await(pleaseInterrupt);
485 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
486          t.interrupt();
487 <        awaitTermination(t, MEDIUM_DELAY_MS);
487 >        awaitTermination(t);
488          checkEmpty(q);
489      }
490  
# Line 513 | Line 541 | public class LinkedBlockingQueueTest ext
541          assertTrue(q.remove(new Integer(1)));
542          assertTrue(q.remove(new Integer(2)));
543          assertTrue(q.add(new Integer(3)));
544 <        assertTrue(q.take() != null);
544 >        assertNotNull(q.take());
545      }
546  
547      /**
# Line 572 | Line 600 | public class LinkedBlockingQueueTest ext
600                  assertTrue(changed);
601  
602              assertTrue(q.containsAll(p));
603 <            assertEquals(SIZE-i, q.size());
603 >            assertEquals(SIZE - i, q.size());
604              p.remove();
605          }
606      }
# Line 585 | Line 613 | public class LinkedBlockingQueueTest ext
613              LinkedBlockingQueue q = populatedQueue(SIZE);
614              LinkedBlockingQueue p = populatedQueue(i);
615              assertTrue(q.removeAll(p));
616 <            assertEquals(SIZE-i, q.size());
616 >            assertEquals(SIZE - i, q.size());
617              for (int j = 0; j < i; ++j) {
618 <                Integer I = (Integer)(p.remove());
619 <                assertFalse(q.contains(I));
618 >                Integer x = (Integer)(p.remove());
619 >                assertFalse(q.contains(x));
620              }
621          }
622      }
# Line 598 | Line 626 | public class LinkedBlockingQueueTest ext
626       */
627      public void testToArray() {
628          LinkedBlockingQueue q = populatedQueue(SIZE);
629 <        Object[] o = q.toArray();
630 <        for (int i = 0; i < o.length; i++)
631 <            assertSame(o[i], q.poll());
629 >        Object[] a = q.toArray();
630 >        assertSame(Object[].class, a.getClass());
631 >        for (Object o : a)
632 >            assertSame(o, q.poll());
633 >        assertTrue(q.isEmpty());
634      }
635  
636      /**
# Line 611 | Line 641 | public class LinkedBlockingQueueTest ext
641          Integer[] ints = new Integer[SIZE];
642          Integer[] array = q.toArray(ints);
643          assertSame(ints, array);
644 <        for (int i = 0; i < ints.length; i++)
645 <            assertSame(ints[i], q.poll());
644 >        for (Integer o : ints)
645 >            assertSame(o, q.poll());
646 >        assertTrue(q.isEmpty());
647      }
648  
649      /**
# Line 632 | Line 663 | public class LinkedBlockingQueueTest ext
663      public void testIterator() throws InterruptedException {
664          LinkedBlockingQueue q = populatedQueue(SIZE);
665          Iterator it = q.iterator();
666 <        while (it.hasNext()) {
666 >        int i;
667 >        for (i = 0; it.hasNext(); i++)
668 >            assertTrue(q.contains(it.next()));
669 >        assertEquals(i, SIZE);
670 >        assertIteratorExhausted(it);
671 >
672 >        it = q.iterator();
673 >        for (i = 0; it.hasNext(); i++)
674              assertEquals(it.next(), q.take());
675 <        }
675 >        assertEquals(i, SIZE);
676 >        assertIteratorExhausted(it);
677 >    }
678 >
679 >    /**
680 >     * iterator of empty collection has no elements
681 >     */
682 >    public void testEmptyIterator() {
683 >        assertIteratorExhausted(new LinkedBlockingQueue().iterator());
684      }
685  
686      /**
# Line 705 | Line 751 | public class LinkedBlockingQueueTest ext
751          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
752          q.add(one);
753          q.add(two);
708        ExecutorService executor = Executors.newFixedThreadPool(2);
754          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
755 <        executor.execute(new CheckedRunnable() {
756 <            public void realRun() throws InterruptedException {
757 <                assertFalse(q.offer(three));
758 <                threadsStarted.await();
759 <                assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
760 <                assertEquals(0, q.remainingCapacity());
761 <            }});
762 <
763 <        executor.execute(new CheckedRunnable() {
764 <            public void realRun() throws InterruptedException {
765 <                threadsStarted.await();
766 <                assertSame(one, q.take());
767 <            }});
768 <
769 <        joinPool(executor);
755 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
756 >        try (PoolCleaner cleaner = cleaner(executor)) {
757 >            executor.execute(new CheckedRunnable() {
758 >                public void realRun() throws InterruptedException {
759 >                    assertFalse(q.offer(three));
760 >                    threadsStarted.await();
761 >                    assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
762 >                    assertEquals(0, q.remainingCapacity());
763 >                }});
764 >
765 >            executor.execute(new CheckedRunnable() {
766 >                public void realRun() throws InterruptedException {
767 >                    threadsStarted.await();
768 >                    assertSame(one, q.take());
769 >                }});
770 >        }
771      }
772  
773      /**
# Line 730 | Line 776 | public class LinkedBlockingQueueTest ext
776      public void testPollInExecutor() {
777          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
778          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
779 <        ExecutorService executor = Executors.newFixedThreadPool(2);
780 <        executor.execute(new CheckedRunnable() {
781 <            public void realRun() throws InterruptedException {
782 <                assertNull(q.poll());
783 <                threadsStarted.await();
784 <                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
785 <                checkEmpty(q);
786 <            }});
787 <
788 <        executor.execute(new CheckedRunnable() {
789 <            public void realRun() throws InterruptedException {
790 <                threadsStarted.await();
791 <                q.put(one);
792 <            }});
793 <
794 <        joinPool(executor);
779 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
780 >        try (PoolCleaner cleaner = cleaner(executor)) {
781 >            executor.execute(new CheckedRunnable() {
782 >                public void realRun() throws InterruptedException {
783 >                    assertNull(q.poll());
784 >                    threadsStarted.await();
785 >                    assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
786 >                    checkEmpty(q);
787 >                }});
788 >
789 >            executor.execute(new CheckedRunnable() {
790 >                public void realRun() throws InterruptedException {
791 >                    threadsStarted.await();
792 >                    q.put(one);
793 >                }});
794 >        }
795      }
796  
797      /**
798 <     * A deserialized serialized queue has same elements in same order
798 >     * A deserialized/reserialized queue has same elements in same order
799       */
800      public void testSerialization() throws Exception {
801          Queue x = populatedQueue(SIZE);
802          Queue y = serialClone(x);
803  
804 <        assertTrue(x != y);
804 >        assertNotSame(x, y);
805          assertEquals(x.size(), y.size());
806          assertEquals(x.toString(), y.toString());
807          assertTrue(Arrays.equals(x.toArray(), y.toArray()));
# Line 797 | Line 843 | public class LinkedBlockingQueueTest ext
843          final LinkedBlockingQueue q = populatedQueue(SIZE);
844          Thread t = new Thread(new CheckedRunnable() {
845              public void realRun() throws InterruptedException {
846 <                q.put(new Integer(SIZE+1));
846 >                q.put(new Integer(SIZE + 1));
847              }});
848  
849          t.start();
# Line 821 | Line 867 | public class LinkedBlockingQueueTest ext
867              ArrayList l = new ArrayList();
868              q.drainTo(l, i);
869              int k = (i < SIZE) ? i : SIZE;
870 <            assertEquals(l.size(), k);
871 <            assertEquals(q.size(), SIZE-k);
870 >            assertEquals(k, l.size());
871 >            assertEquals(SIZE - k, q.size());
872              for (int j = 0; j < k; ++j)
873                  assertEquals(l.get(j), new Integer(j));
874 <            while (q.poll() != null) ;
874 >            do {} while (q.poll() != null);
875 >        }
876 >    }
877 >
878 >    /**
879 >     * remove(null), contains(null) always return false
880 >     */
881 >    public void testNeverContainsNull() {
882 >        Collection<?>[] qs = {
883 >            new LinkedBlockingQueue<Object>(),
884 >            populatedQueue(2),
885 >        };
886 >
887 >        for (Collection<?> q : qs) {
888 >            assertFalse(q.contains(null));
889 >            assertFalse(q.remove(null));
890          }
891      }
892  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines