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.76 by jsr166, Fri Aug 4 03:30:21 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 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) {
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 316 | Line 334 | public class LinkedBlockingQueueTest ext
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(0, q.remainingCapacity());
# Line 332 | Line 350 | public class LinkedBlockingQueueTest ext
350              public void realRun() throws InterruptedException {
351                  q.put(new Object());
352                  q.put(new Object());
353 +
354                  long startTime = System.nanoTime();
355                  assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS));
356                  assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
357 +
358 +                Thread.currentThread().interrupt();
359 +                try {
360 +                    q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
361 +                    shouldThrow();
362 +                } catch (InterruptedException success) {}
363 +                assertFalse(Thread.interrupted());
364 +
365                  pleaseInterrupt.countDown();
366                  try {
367                      q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
368                      shouldThrow();
369                  } catch (InterruptedException success) {}
370 +                assertFalse(Thread.interrupted());
371              }});
372  
373          await(pleaseInterrupt);
374 <        assertThreadStaysAlive(t);
374 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
375          t.interrupt();
376          awaitTermination(t);
377      }
# Line 366 | Line 394 | public class LinkedBlockingQueueTest ext
394          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
395          Thread t = newStartedThread(new CheckedRunnable() {
396              public void realRun() throws InterruptedException {
397 <                for (int i = 0; i < SIZE; ++i) {
370 <                    assertEquals(i, q.take());
371 <                }
397 >                for (int i = 0; i < SIZE; i++) assertEquals(i, q.take());
398  
399                  Thread.currentThread().interrupt();
400                  try {
# Line 386 | Line 412 | public class LinkedBlockingQueueTest ext
412              }});
413  
414          await(pleaseInterrupt);
415 <        assertThreadStaysAlive(t);
415 >        assertThreadBlocks(t, Thread.State.WAITING);
416          t.interrupt();
417          awaitTermination(t);
418      }
# Line 435 | Line 461 | public class LinkedBlockingQueueTest ext
461       */
462      public void testInterruptedTimedPoll() throws InterruptedException {
463          final BlockingQueue<Integer> q = populatedQueue(SIZE);
464 <        final CountDownLatch aboutToWait = new CountDownLatch(1);
464 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
465          Thread t = newStartedThread(new CheckedRunnable() {
466              public void realRun() throws InterruptedException {
467 <                for (int i = 0; i < SIZE; ++i) {
468 <                    long t0 = System.nanoTime();
467 >                long startTime = System.nanoTime();
468 >                for (int i = 0; i < SIZE; i++)
469                      assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
470 <                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
471 <                }
472 <                long t0 = System.nanoTime();
473 <                aboutToWait.countDown();
470 >
471 >                Thread.currentThread().interrupt();
472 >                try {
473 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
474 >                    shouldThrow();
475 >                } catch (InterruptedException success) {}
476 >                assertFalse(Thread.interrupted());
477 >
478 >                pleaseInterrupt.countDown();
479                  try {
480 <                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
480 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
481                      shouldThrow();
482 <                } catch (InterruptedException success) {
483 <                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
484 <                }
482 >                } catch (InterruptedException success) {}
483 >                assertFalse(Thread.interrupted());
484 >
485 >                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
486              }});
487  
488 <        aboutToWait.await();
489 <        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
488 >        await(pleaseInterrupt);
489 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
490          t.interrupt();
491 <        awaitTermination(t, MEDIUM_DELAY_MS);
491 >        awaitTermination(t);
492          checkEmpty(q);
493      }
494  
# Line 513 | Line 545 | public class LinkedBlockingQueueTest ext
545          assertTrue(q.remove(new Integer(1)));
546          assertTrue(q.remove(new Integer(2)));
547          assertTrue(q.add(new Integer(3)));
548 <        assertTrue(q.take() != null);
548 >        assertNotNull(q.take());
549      }
550  
551      /**
# Line 572 | Line 604 | public class LinkedBlockingQueueTest ext
604                  assertTrue(changed);
605  
606              assertTrue(q.containsAll(p));
607 <            assertEquals(SIZE-i, q.size());
607 >            assertEquals(SIZE - i, q.size());
608              p.remove();
609          }
610      }
# Line 585 | Line 617 | public class LinkedBlockingQueueTest ext
617              LinkedBlockingQueue q = populatedQueue(SIZE);
618              LinkedBlockingQueue p = populatedQueue(i);
619              assertTrue(q.removeAll(p));
620 <            assertEquals(SIZE-i, q.size());
620 >            assertEquals(SIZE - i, q.size());
621              for (int j = 0; j < i; ++j) {
622 <                Integer I = (Integer)(p.remove());
623 <                assertFalse(q.contains(I));
622 >                Integer x = (Integer)(p.remove());
623 >                assertFalse(q.contains(x));
624              }
625          }
626      }
# Line 632 | Line 664 | public class LinkedBlockingQueueTest ext
664      public void testIterator() throws InterruptedException {
665          LinkedBlockingQueue q = populatedQueue(SIZE);
666          Iterator it = q.iterator();
667 <        while (it.hasNext()) {
667 >        int i;
668 >        for (i = 0; it.hasNext(); i++)
669 >            assertTrue(q.contains(it.next()));
670 >        assertEquals(i, SIZE);
671 >        assertIteratorExhausted(it);
672 >
673 >        it = q.iterator();
674 >        for (i = 0; it.hasNext(); i++)
675              assertEquals(it.next(), q.take());
676 <        }
676 >        assertEquals(i, SIZE);
677 >        assertIteratorExhausted(it);
678 >    }
679 >
680 >    /**
681 >     * iterator of empty collection has no elements
682 >     */
683 >    public void testEmptyIterator() {
684 >        assertIteratorExhausted(new LinkedBlockingQueue().iterator());
685      }
686  
687      /**
# Line 705 | Line 752 | public class LinkedBlockingQueueTest ext
752          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
753          q.add(one);
754          q.add(two);
708        ExecutorService executor = Executors.newFixedThreadPool(2);
755          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
756 <        executor.execute(new CheckedRunnable() {
757 <            public void realRun() throws InterruptedException {
758 <                assertFalse(q.offer(three));
759 <                threadsStarted.await();
760 <                assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
761 <                assertEquals(0, q.remainingCapacity());
762 <            }});
763 <
764 <        executor.execute(new CheckedRunnable() {
765 <            public void realRun() throws InterruptedException {
766 <                threadsStarted.await();
767 <                assertSame(one, q.take());
768 <            }});
769 <
770 <        joinPool(executor);
756 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
757 >        try (PoolCleaner cleaner = cleaner(executor)) {
758 >            executor.execute(new CheckedRunnable() {
759 >                public void realRun() throws InterruptedException {
760 >                    assertFalse(q.offer(three));
761 >                    threadsStarted.await();
762 >                    assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
763 >                    assertEquals(0, q.remainingCapacity());
764 >                }});
765 >
766 >            executor.execute(new CheckedRunnable() {
767 >                public void realRun() throws InterruptedException {
768 >                    threadsStarted.await();
769 >                    assertSame(one, q.take());
770 >                }});
771 >        }
772      }
773  
774      /**
# Line 730 | Line 777 | public class LinkedBlockingQueueTest ext
777      public void testPollInExecutor() {
778          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
779          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
780 <        ExecutorService executor = Executors.newFixedThreadPool(2);
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 <        joinPool(executor);
780 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
781 >        try (PoolCleaner cleaner = cleaner(executor)) {
782 >            executor.execute(new CheckedRunnable() {
783 >                public void realRun() throws InterruptedException {
784 >                    assertNull(q.poll());
785 >                    threadsStarted.await();
786 >                    assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
787 >                    checkEmpty(q);
788 >                }});
789 >
790 >            executor.execute(new CheckedRunnable() {
791 >                public void realRun() throws InterruptedException {
792 >                    threadsStarted.await();
793 >                    q.put(one);
794 >                }});
795 >        }
796      }
797  
798      /**
799 <     * A deserialized serialized queue has same elements in same order
799 >     * A deserialized/reserialized queue has same elements in same order
800       */
801      public void testSerialization() throws Exception {
802          Queue x = populatedQueue(SIZE);
803          Queue y = serialClone(x);
804  
805 <        assertTrue(x != y);
805 >        assertNotSame(x, y);
806          assertEquals(x.size(), y.size());
807          assertEquals(x.toString(), y.toString());
808          assertTrue(Arrays.equals(x.toArray(), y.toArray()));
# Line 797 | Line 844 | public class LinkedBlockingQueueTest ext
844          final LinkedBlockingQueue q = populatedQueue(SIZE);
845          Thread t = new Thread(new CheckedRunnable() {
846              public void realRun() throws InterruptedException {
847 <                q.put(new Integer(SIZE+1));
847 >                q.put(new Integer(SIZE + 1));
848              }});
849  
850          t.start();
# Line 821 | Line 868 | public class LinkedBlockingQueueTest ext
868              ArrayList l = new ArrayList();
869              q.drainTo(l, i);
870              int k = (i < SIZE) ? i : SIZE;
871 <            assertEquals(l.size(), k);
872 <            assertEquals(q.size(), SIZE-k);
871 >            assertEquals(k, l.size());
872 >            assertEquals(SIZE - k, q.size());
873              for (int j = 0; j < k; ++j)
874                  assertEquals(l.get(j), new Integer(j));
875 <            while (q.poll() != null) ;
875 >            do {} while (q.poll() != null);
876 >        }
877 >    }
878 >
879 >    /**
880 >     * remove(null), contains(null) always return false
881 >     */
882 >    public void testNeverContainsNull() {
883 >        Collection<?>[] qs = {
884 >            new LinkedBlockingQueue<Object>(),
885 >            populatedQueue(2),
886 >        };
887 >
888 >        for (Collection<?> q : qs) {
889 >            assertFalse(q.contains(null));
890 >            assertFalse(q.remove(null));
891          }
892      }
893  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines