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.57 by jsr166, Sat Jan 17 22:55:06 2015 UTC vs.
Revision 1.74 by jsr166, Sun May 14 04:02:06 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 305 | 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 318 | 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 342 | 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 368 | 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) {
372 <                    assertEquals(i, q.take());
373 <                }
388 >                for (int i = 0; i < SIZE; i++) assertEquals(i, q.take());
389  
390                  Thread.currentThread().interrupt();
391                  try {
# Line 388 | 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 437 | 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 574 | 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 587 | 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 x = (Integer)(p.remove());
614                  assertFalse(q.contains(x));
# Line 722 | Line 743 | public class LinkedBlockingQueueTest ext
743          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
744          q.add(one);
745          q.add(two);
725        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 747 | 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 814 | 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 839 | Line 860 | public class LinkedBlockingQueueTest ext
860              q.drainTo(l, i);
861              int k = (i < SIZE) ? i : SIZE;
862              assertEquals(k, l.size());
863 <            assertEquals(SIZE-k, q.size());
863 >            assertEquals(SIZE - k, q.size());
864              for (int j = 0; j < k; ++j)
865                  assertEquals(l.get(j), new Integer(j));
866              do {} while (q.poll() != null);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines