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.56 by jsr166, Wed Dec 31 20:17:39 2014 UTC vs.
Revision 1.72 by jsr166, Sun May 14 00:56:43 2017 UTC

# Line 37 | 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       * Returns a new queue of given size containing consecutive
59 <     * Integers 0 ... n.
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 59 | 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 108 | 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 148 | 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 202 | 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 283 | 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 318 | Line 327 | public class LinkedBlockingQueueTest ext
327          assertEquals(0, q.take());
328  
329          await(pleaseInterrupt);
330 <        assertThreadStaysAlive(t);
330 >        assertThreadBlocks(t, Thread.State.WAITING);
331          t.interrupt();
332          awaitTermination(t);
333          assertEquals(0, q.remainingCapacity());
# Line 342 | Line 351 | public class LinkedBlockingQueueTest ext
351                      q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
352                      shouldThrow();
353                  } catch (InterruptedException success) {}
354 +                assertFalse(Thread.interrupted());
355              }});
356  
357          await(pleaseInterrupt);
358 <        assertThreadStaysAlive(t);
358 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
359          t.interrupt();
360          awaitTermination(t);
361      }
# Line 368 | Line 378 | public class LinkedBlockingQueueTest ext
378          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
379          Thread t = newStartedThread(new CheckedRunnable() {
380              public void realRun() throws InterruptedException {
381 <                for (int i = 0; i < SIZE; ++i) {
372 <                    assertEquals(i, q.take());
373 <                }
381 >                for (int i = 0; i < SIZE; i++) assertEquals(i, q.take());
382  
383                  Thread.currentThread().interrupt();
384                  try {
# Line 388 | Line 396 | public class LinkedBlockingQueueTest ext
396              }});
397  
398          await(pleaseInterrupt);
399 <        assertThreadStaysAlive(t);
399 >        assertThreadBlocks(t, Thread.State.WAITING);
400          t.interrupt();
401          awaitTermination(t);
402      }
# Line 437 | Line 445 | public class LinkedBlockingQueueTest ext
445       */
446      public void testInterruptedTimedPoll() throws InterruptedException {
447          final BlockingQueue<Integer> q = populatedQueue(SIZE);
448 <        final CountDownLatch aboutToWait = new CountDownLatch(1);
448 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
449          Thread t = newStartedThread(new CheckedRunnable() {
450              public void realRun() throws InterruptedException {
451 +                long startTime = System.nanoTime();
452                  for (int i = 0; i < SIZE; ++i) {
444                    long t0 = System.nanoTime();
453                      assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
446                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
454                  }
455 <                long t0 = System.nanoTime();
456 <                aboutToWait.countDown();
455 >
456 >                pleaseInterrupt.countDown();
457                  try {
458 <                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
458 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
459                      shouldThrow();
460 <                } catch (InterruptedException success) {
461 <                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
462 <                }
460 >                } catch (InterruptedException success) {}
461 >                assertFalse(Thread.interrupted());
462 >
463 >                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
464              }});
465  
466 <        aboutToWait.await();
467 <        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
466 >        await(pleaseInterrupt);
467 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
468          t.interrupt();
469 <        awaitTermination(t, MEDIUM_DELAY_MS);
469 >        awaitTermination(t);
470          checkEmpty(q);
471      }
472  
# Line 574 | Line 582 | public class LinkedBlockingQueueTest ext
582                  assertTrue(changed);
583  
584              assertTrue(q.containsAll(p));
585 <            assertEquals(SIZE-i, q.size());
585 >            assertEquals(SIZE - i, q.size());
586              p.remove();
587          }
588      }
# Line 587 | Line 595 | public class LinkedBlockingQueueTest ext
595              LinkedBlockingQueue q = populatedQueue(SIZE);
596              LinkedBlockingQueue p = populatedQueue(i);
597              assertTrue(q.removeAll(p));
598 <            assertEquals(SIZE-i, q.size());
598 >            assertEquals(SIZE - i, q.size());
599              for (int j = 0; j < i; ++j) {
600                  Integer x = (Integer)(p.remove());
601                  assertFalse(q.contains(x));
# Line 634 | Line 642 | public class LinkedBlockingQueueTest ext
642      public void testIterator() throws InterruptedException {
643          LinkedBlockingQueue q = populatedQueue(SIZE);
644          Iterator it = q.iterator();
645 <        while (it.hasNext()) {
645 >        int i;
646 >        for (i = 0; it.hasNext(); i++)
647 >            assertTrue(q.contains(it.next()));
648 >        assertEquals(i, SIZE);
649 >        assertIteratorExhausted(it);
650 >
651 >        it = q.iterator();
652 >        for (i = 0; it.hasNext(); i++)
653              assertEquals(it.next(), q.take());
654 <        }
654 >        assertEquals(i, SIZE);
655 >        assertIteratorExhausted(it);
656 >    }
657 >
658 >    /**
659 >     * iterator of empty collection has no elements
660 >     */
661 >    public void testEmptyIterator() {
662 >        assertIteratorExhausted(new LinkedBlockingQueue().iterator());
663      }
664  
665      /**
# Line 707 | Line 730 | public class LinkedBlockingQueueTest ext
730          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
731          q.add(one);
732          q.add(two);
710        ExecutorService executor = Executors.newFixedThreadPool(2);
733          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
734 <        executor.execute(new CheckedRunnable() {
735 <            public void realRun() throws InterruptedException {
736 <                assertFalse(q.offer(three));
737 <                threadsStarted.await();
738 <                assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
739 <                assertEquals(0, q.remainingCapacity());
740 <            }});
741 <
742 <        executor.execute(new CheckedRunnable() {
743 <            public void realRun() throws InterruptedException {
744 <                threadsStarted.await();
745 <                assertSame(one, q.take());
746 <            }});
747 <
748 <        joinPool(executor);
734 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
735 >        try (PoolCleaner cleaner = cleaner(executor)) {
736 >            executor.execute(new CheckedRunnable() {
737 >                public void realRun() throws InterruptedException {
738 >                    assertFalse(q.offer(three));
739 >                    threadsStarted.await();
740 >                    assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
741 >                    assertEquals(0, q.remainingCapacity());
742 >                }});
743 >
744 >            executor.execute(new CheckedRunnable() {
745 >                public void realRun() throws InterruptedException {
746 >                    threadsStarted.await();
747 >                    assertSame(one, q.take());
748 >                }});
749 >        }
750      }
751  
752      /**
# Line 732 | Line 755 | public class LinkedBlockingQueueTest ext
755      public void testPollInExecutor() {
756          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
757          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
758 <        ExecutorService executor = Executors.newFixedThreadPool(2);
759 <        executor.execute(new CheckedRunnable() {
760 <            public void realRun() throws InterruptedException {
761 <                assertNull(q.poll());
762 <                threadsStarted.await();
763 <                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
764 <                checkEmpty(q);
765 <            }});
766 <
767 <        executor.execute(new CheckedRunnable() {
768 <            public void realRun() throws InterruptedException {
769 <                threadsStarted.await();
770 <                q.put(one);
771 <            }});
772 <
773 <        joinPool(executor);
758 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
759 >        try (PoolCleaner cleaner = cleaner(executor)) {
760 >            executor.execute(new CheckedRunnable() {
761 >                public void realRun() throws InterruptedException {
762 >                    assertNull(q.poll());
763 >                    threadsStarted.await();
764 >                    assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
765 >                    checkEmpty(q);
766 >                }});
767 >
768 >            executor.execute(new CheckedRunnable() {
769 >                public void realRun() throws InterruptedException {
770 >                    threadsStarted.await();
771 >                    q.put(one);
772 >                }});
773 >        }
774      }
775  
776      /**
# Line 799 | Line 822 | public class LinkedBlockingQueueTest ext
822          final LinkedBlockingQueue q = populatedQueue(SIZE);
823          Thread t = new Thread(new CheckedRunnable() {
824              public void realRun() throws InterruptedException {
825 <                q.put(new Integer(SIZE+1));
825 >                q.put(new Integer(SIZE + 1));
826              }});
827  
828          t.start();
# Line 824 | Line 847 | public class LinkedBlockingQueueTest ext
847              q.drainTo(l, i);
848              int k = (i < SIZE) ? i : SIZE;
849              assertEquals(k, l.size());
850 <            assertEquals(SIZE-k, q.size());
850 >            assertEquals(SIZE - k, q.size());
851              for (int j = 0; j < k; ++j)
852                  assertEquals(l.get(j), new Integer(j));
853              do {} while (q.poll() != null);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines