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.60 by jsr166, Sat May 23 00:53:08 2015 UTC vs.
Revision 1.73 by jsr166, Sun May 14 01:30:34 2017 UTC

# Line 41 | Line 41 | public class LinkedBlockingQueueTest ext
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 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 <                for (int i = 0; i < SIZE; ++i) {
452 <                    long t0 = System.nanoTime();
451 >                long startTime = System.nanoTime();
452 >                for (int i = 0; i < SIZE; i++)
453                      assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
454 <                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
455 <                }
456 <                long t0 = System.nanoTime();
457 <                aboutToWait.countDown();
454 >
455 >                Thread.currentThread().interrupt();
456 >                try {
457 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
458 >                    shouldThrow();
459 >                } catch (InterruptedException success) {}
460 >                assertFalse(Thread.interrupted());
461 >
462 >                pleaseInterrupt.countDown();
463                  try {
464 <                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
464 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
465                      shouldThrow();
466 <                } catch (InterruptedException success) {
467 <                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
468 <                }
466 >                } catch (InterruptedException success) {}
467 >                assertFalse(Thread.interrupted());
468 >
469 >                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
470              }});
471  
472 <        aboutToWait.await();
473 <        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
472 >        await(pleaseInterrupt);
473 >        assertThreadBlocks(t, Thread.State.TIMED_WAITING);
474          t.interrupt();
475 <        awaitTermination(t, MEDIUM_DELAY_MS);
475 >        awaitTermination(t);
476          checkEmpty(q);
477      }
478  
# Line 722 | Line 736 | public class LinkedBlockingQueueTest ext
736          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
737          q.add(one);
738          q.add(two);
725        ExecutorService executor = Executors.newFixedThreadPool(2);
739          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
740 <        executor.execute(new CheckedRunnable() {
741 <            public void realRun() throws InterruptedException {
742 <                assertFalse(q.offer(three));
743 <                threadsStarted.await();
744 <                assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
745 <                assertEquals(0, q.remainingCapacity());
746 <            }});
747 <
748 <        executor.execute(new CheckedRunnable() {
749 <            public void realRun() throws InterruptedException {
750 <                threadsStarted.await();
751 <                assertSame(one, q.take());
752 <            }});
753 <
754 <        joinPool(executor);
740 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
741 >        try (PoolCleaner cleaner = cleaner(executor)) {
742 >            executor.execute(new CheckedRunnable() {
743 >                public void realRun() throws InterruptedException {
744 >                    assertFalse(q.offer(three));
745 >                    threadsStarted.await();
746 >                    assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
747 >                    assertEquals(0, q.remainingCapacity());
748 >                }});
749 >
750 >            executor.execute(new CheckedRunnable() {
751 >                public void realRun() throws InterruptedException {
752 >                    threadsStarted.await();
753 >                    assertSame(one, q.take());
754 >                }});
755 >        }
756      }
757  
758      /**
# Line 747 | Line 761 | public class LinkedBlockingQueueTest ext
761      public void testPollInExecutor() {
762          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
763          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
764 <        ExecutorService executor = Executors.newFixedThreadPool(2);
765 <        executor.execute(new CheckedRunnable() {
766 <            public void realRun() throws InterruptedException {
767 <                assertNull(q.poll());
768 <                threadsStarted.await();
769 <                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
770 <                checkEmpty(q);
771 <            }});
772 <
773 <        executor.execute(new CheckedRunnable() {
774 <            public void realRun() throws InterruptedException {
775 <                threadsStarted.await();
776 <                q.put(one);
777 <            }});
778 <
779 <        joinPool(executor);
764 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
765 >        try (PoolCleaner cleaner = cleaner(executor)) {
766 >            executor.execute(new CheckedRunnable() {
767 >                public void realRun() throws InterruptedException {
768 >                    assertNull(q.poll());
769 >                    threadsStarted.await();
770 >                    assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
771 >                    checkEmpty(q);
772 >                }});
773 >
774 >            executor.execute(new CheckedRunnable() {
775 >                public void realRun() throws InterruptedException {
776 >                    threadsStarted.await();
777 >                    q.put(one);
778 >                }});
779 >        }
780      }
781  
782      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines