--- jsr166/src/test/tck/LinkedBlockingQueueTest.java 2015/05/23 00:53:08 1.60 +++ jsr166/src/test/tck/LinkedBlockingQueueTest.java 2017/05/13 22:49:01 1.70 @@ -41,16 +41,24 @@ public class LinkedBlockingQueueTest ext } public static Test suite() { + class Implementation implements CollectionImplementation { + public Class klazz() { return LinkedBlockingQueue.class; } + public Collection emptyCollection() { return new LinkedBlockingQueue(); } + public Object makeElement(int i) { return i; } + public boolean isConcurrent() { return true; } + public boolean permitsNulls() { return false; } + } return newTestSuite(LinkedBlockingQueueTest.class, new Unbounded().testSuite(), - new Bounded().testSuite()); + new Bounded().testSuite(), + CollectionTest.testSuite(new Implementation())); } /** * Returns a new queue of given size containing consecutive - * Integers 0 ... n. + * Integers 0 ... n - 1. */ - private LinkedBlockingQueue populatedQueue(int n) { + private static LinkedBlockingQueue populatedQueue(int n) { LinkedBlockingQueue q = new LinkedBlockingQueue(n); assertTrue(q.isEmpty()); @@ -59,6 +67,7 @@ public class LinkedBlockingQueueTest ext assertFalse(q.isEmpty()); assertEquals(0, q.remainingCapacity()); assertEquals(n, q.size()); + assertEquals((Integer) 0, q.peek()); return q; } @@ -283,7 +292,7 @@ public class LinkedBlockingQueueTest ext }}); await(pleaseInterrupt); - assertThreadStaysAlive(t); + assertThreadBlocks(t, Thread.State.WAITING); t.interrupt(); awaitTermination(t); assertEquals(SIZE, q.size()); @@ -318,7 +327,7 @@ public class LinkedBlockingQueueTest ext assertEquals(0, q.take()); await(pleaseInterrupt); - assertThreadStaysAlive(t); + assertThreadBlocks(t, Thread.State.WAITING); t.interrupt(); awaitTermination(t); assertEquals(0, q.remainingCapacity()); @@ -345,7 +354,7 @@ public class LinkedBlockingQueueTest ext }}); await(pleaseInterrupt); - assertThreadStaysAlive(t); + assertThreadBlocks(t, Thread.State.TIMED_WAITING); t.interrupt(); awaitTermination(t); } @@ -368,9 +377,7 @@ public class LinkedBlockingQueueTest ext final CountDownLatch pleaseInterrupt = new CountDownLatch(1); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { - for (int i = 0; i < SIZE; ++i) { - assertEquals(i, q.take()); - } + for (int i = 0; i < SIZE; i++) assertEquals(i, q.take()); Thread.currentThread().interrupt(); try { @@ -388,7 +395,7 @@ public class LinkedBlockingQueueTest ext }}); await(pleaseInterrupt); - assertThreadStaysAlive(t); + assertThreadBlocks(t, Thread.State.WAITING); t.interrupt(); awaitTermination(t); } @@ -440,25 +447,23 @@ public class LinkedBlockingQueueTest ext final CountDownLatch aboutToWait = new CountDownLatch(1); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { + long startTime = System.nanoTime(); for (int i = 0; i < SIZE; ++i) { - long t0 = System.nanoTime(); assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); - assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); } - long t0 = System.nanoTime(); aboutToWait.countDown(); try { - q.poll(MEDIUM_DELAY_MS, MILLISECONDS); + q.poll(LONG_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (InterruptedException success) { - assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); } }}); - aboutToWait.await(); - waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); + await(aboutToWait); + assertThreadBlocks(t, Thread.State.TIMED_WAITING); t.interrupt(); - awaitTermination(t, MEDIUM_DELAY_MS); + awaitTermination(t); checkEmpty(q); } @@ -722,23 +727,23 @@ public class LinkedBlockingQueueTest ext final LinkedBlockingQueue q = new LinkedBlockingQueue(2); q.add(one); q.add(two); - ExecutorService executor = Executors.newFixedThreadPool(2); final CheckedBarrier threadsStarted = new CheckedBarrier(2); - executor.execute(new CheckedRunnable() { - public void realRun() throws InterruptedException { - assertFalse(q.offer(three)); - threadsStarted.await(); - assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); - assertEquals(0, q.remainingCapacity()); - }}); - - executor.execute(new CheckedRunnable() { - public void realRun() throws InterruptedException { - threadsStarted.await(); - assertSame(one, q.take()); - }}); - - joinPool(executor); + final ExecutorService executor = Executors.newFixedThreadPool(2); + try (PoolCleaner cleaner = cleaner(executor)) { + executor.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { + assertFalse(q.offer(three)); + threadsStarted.await(); + assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); + assertEquals(0, q.remainingCapacity()); + }}); + + executor.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { + threadsStarted.await(); + assertSame(one, q.take()); + }}); + } } /** @@ -747,22 +752,22 @@ public class LinkedBlockingQueueTest ext public void testPollInExecutor() { final LinkedBlockingQueue q = new LinkedBlockingQueue(2); final CheckedBarrier threadsStarted = new CheckedBarrier(2); - ExecutorService executor = Executors.newFixedThreadPool(2); - executor.execute(new CheckedRunnable() { - public void realRun() throws InterruptedException { - assertNull(q.poll()); - threadsStarted.await(); - assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); - checkEmpty(q); - }}); - - executor.execute(new CheckedRunnable() { - public void realRun() throws InterruptedException { - threadsStarted.await(); - q.put(one); - }}); - - joinPool(executor); + final ExecutorService executor = Executors.newFixedThreadPool(2); + try (PoolCleaner cleaner = cleaner(executor)) { + executor.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { + assertNull(q.poll()); + threadsStarted.await(); + assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); + checkEmpty(q); + }}); + + executor.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { + threadsStarted.await(); + q.put(one); + }}); + } } /**