--- jsr166/src/test/tck/LinkedBlockingQueueTest.java 2011/07/15 18:49:31 1.47 +++ jsr166/src/test/tck/LinkedBlockingQueueTest.java 2017/05/13 22:49:01 1.70 @@ -6,19 +6,21 @@ * Pat Fisher, Mike Judd. */ -import junit.framework.*; -import java.util.Arrays; +import static java.util.concurrent.TimeUnit.MILLISECONDS; + import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Queue; import java.util.concurrent.BlockingQueue; import java.util.concurrent.CountDownLatch; -import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.Executors; import java.util.concurrent.ExecutorService; -import static java.util.concurrent.TimeUnit.MILLISECONDS; +import java.util.concurrent.LinkedBlockingQueue; + +import junit.framework.Test; public class LinkedBlockingQueueTest extends JSR166TestCase { @@ -35,20 +37,28 @@ public class LinkedBlockingQueueTest ext } public static void main(String[] args) { - junit.textui.TestRunner.run(suite()); + main(suite(), args); } 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())); } /** - * Create a queue of given size containing consecutive - * Integers 0 ... n. + * Returns a new queue of given size containing consecutive + * Integers 0 ... n - 1. */ - private LinkedBlockingQueue populatedQueue(int n) { + private static LinkedBlockingQueue populatedQueue(int n) { LinkedBlockingQueue q = new LinkedBlockingQueue(n); assertTrue(q.isEmpty()); @@ -57,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; } @@ -106,7 +117,7 @@ public class LinkedBlockingQueueTest ext */ public void testConstructor5() { Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE-1; ++i) + for (int i = 0; i < SIZE - 1; ++i) ints[i] = new Integer(i); Collection elements = Arrays.asList(ints); try { @@ -146,16 +157,16 @@ public class LinkedBlockingQueueTest ext * remainingCapacity decreases on add, increases on remove */ public void testRemainingCapacity() { - LinkedBlockingQueue q = populatedQueue(SIZE); + BlockingQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { assertEquals(i, q.remainingCapacity()); - assertEquals(SIZE-i, q.size()); - q.remove(); + assertEquals(SIZE, q.size() + q.remainingCapacity()); + assertEquals(i, q.remove()); } for (int i = 0; i < SIZE; ++i) { - assertEquals(SIZE-i, q.remainingCapacity()); - assertEquals(i, q.size()); - q.add(new Integer(i)); + assertEquals(SIZE - i, q.remainingCapacity()); + assertEquals(SIZE, q.size() + q.remainingCapacity()); + assertTrue(q.add(i)); } } @@ -200,7 +211,7 @@ public class LinkedBlockingQueueTest ext public void testAddAll3() { LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE-1; ++i) + for (int i = 0; i < SIZE - 1; ++i) ints[i] = new Integer(i); Collection elements = Arrays.asList(ints); try { @@ -245,9 +256,9 @@ public class LinkedBlockingQueueTest ext public void testPut() throws InterruptedException { LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - Integer I = new Integer(i); - q.put(I); - assertTrue(q.contains(I)); + Integer x = new Integer(i); + q.put(x); + assertTrue(q.contains(x)); } assertEquals(0, q.remainingCapacity()); } @@ -281,7 +292,7 @@ public class LinkedBlockingQueueTest ext }}); await(pleaseInterrupt); - assertThreadStaysAlive(t); + assertThreadBlocks(t, Thread.State.WAITING); t.interrupt(); awaitTermination(t); assertEquals(SIZE, q.size()); @@ -312,14 +323,14 @@ public class LinkedBlockingQueueTest ext }}); await(pleaseTake); - assertEquals(q.remainingCapacity(), 0); + assertEquals(0, q.remainingCapacity()); assertEquals(0, q.take()); await(pleaseInterrupt); - assertThreadStaysAlive(t); + assertThreadBlocks(t, Thread.State.WAITING); t.interrupt(); awaitTermination(t); - assertEquals(q.remainingCapacity(), 0); + assertEquals(0, q.remainingCapacity()); } /** @@ -343,7 +354,7 @@ public class LinkedBlockingQueueTest ext }}); await(pleaseInterrupt); - assertThreadStaysAlive(t); + assertThreadBlocks(t, Thread.State.TIMED_WAITING); t.interrupt(); awaitTermination(t); } @@ -366,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 { @@ -386,7 +395,7 @@ public class LinkedBlockingQueueTest ext }}); await(pleaseInterrupt); - assertThreadStaysAlive(t); + assertThreadBlocks(t, Thread.State.WAITING); t.interrupt(); awaitTermination(t); } @@ -438,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); } @@ -513,7 +520,7 @@ public class LinkedBlockingQueueTest ext assertTrue(q.remove(new Integer(1))); assertTrue(q.remove(new Integer(2))); assertTrue(q.add(new Integer(3))); - assertTrue(q.take() != null); + assertNotNull(q.take()); } /** @@ -572,7 +579,7 @@ public class LinkedBlockingQueueTest ext assertTrue(changed); assertTrue(q.containsAll(p)); - assertEquals(SIZE-i, q.size()); + assertEquals(SIZE - i, q.size()); p.remove(); } } @@ -585,10 +592,10 @@ public class LinkedBlockingQueueTest ext LinkedBlockingQueue q = populatedQueue(SIZE); LinkedBlockingQueue p = populatedQueue(i); assertTrue(q.removeAll(p)); - assertEquals(SIZE-i, q.size()); + assertEquals(SIZE - i, q.size()); for (int j = 0; j < i; ++j) { - Integer I = (Integer)(p.remove()); - assertFalse(q.contains(I)); + Integer x = (Integer)(p.remove()); + assertFalse(q.contains(x)); } } } @@ -632,9 +639,24 @@ public class LinkedBlockingQueueTest ext public void testIterator() throws InterruptedException { LinkedBlockingQueue q = populatedQueue(SIZE); Iterator it = q.iterator(); - while (it.hasNext()) { + int i; + for (i = 0; it.hasNext(); i++) + assertTrue(q.contains(it.next())); + assertEquals(i, SIZE); + assertIteratorExhausted(it); + + it = q.iterator(); + for (i = 0; it.hasNext(); i++) assertEquals(it.next(), q.take()); - } + assertEquals(i, SIZE); + assertIteratorExhausted(it); + } + + /** + * iterator of empty collection has no elements + */ + public void testEmptyIterator() { + assertIteratorExhausted(new LinkedBlockingQueue().iterator()); } /** @@ -705,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()); + }}); + } } /** @@ -730,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); + }}); + } } /** @@ -755,7 +777,7 @@ public class LinkedBlockingQueueTest ext Queue x = populatedQueue(SIZE); Queue y = serialClone(x); - assertTrue(x != y); + assertNotSame(x, y); assertEquals(x.size(), y.size()); assertEquals(x.toString(), y.toString()); assertTrue(Arrays.equals(x.toArray(), y.toArray())); @@ -773,8 +795,8 @@ public class LinkedBlockingQueueTest ext LinkedBlockingQueue q = populatedQueue(SIZE); ArrayList l = new ArrayList(); q.drainTo(l); - assertEquals(q.size(), 0); - assertEquals(l.size(), SIZE); + assertEquals(0, q.size()); + assertEquals(SIZE, l.size()); for (int i = 0; i < SIZE; ++i) assertEquals(l.get(i), new Integer(i)); q.add(zero); @@ -784,8 +806,8 @@ public class LinkedBlockingQueueTest ext assertTrue(q.contains(one)); l.clear(); q.drainTo(l); - assertEquals(q.size(), 0); - assertEquals(l.size(), 2); + assertEquals(0, q.size()); + assertEquals(2, l.size()); for (int i = 0; i < 2; ++i) assertEquals(l.get(i), new Integer(i)); } @@ -797,7 +819,7 @@ public class LinkedBlockingQueueTest ext final LinkedBlockingQueue q = populatedQueue(SIZE); Thread t = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { - q.put(new Integer(SIZE+1)); + q.put(new Integer(SIZE + 1)); }}); t.start(); @@ -821,11 +843,26 @@ public class LinkedBlockingQueueTest ext ArrayList l = new ArrayList(); q.drainTo(l, i); int k = (i < SIZE) ? i : SIZE; - assertEquals(l.size(), k); - assertEquals(q.size(), SIZE-k); + assertEquals(k, l.size()); + assertEquals(SIZE - k, q.size()); for (int j = 0; j < k; ++j) assertEquals(l.get(j), new Integer(j)); - while (q.poll() != null) ; + do {} while (q.poll() != null); + } + } + + /** + * remove(null), contains(null) always return false + */ + public void testNeverContainsNull() { + Collection[] qs = { + new LinkedBlockingQueue(), + populatedQueue(2), + }; + + for (Collection q : qs) { + assertFalse(q.contains(null)); + assertFalse(q.remove(null)); } }