--- jsr166/src/test/tck/LinkedBlockingQueueTest.java 2014/12/31 20:17:39 1.56 +++ jsr166/src/test/tck/LinkedBlockingQueueTest.java 2016/10/17 01:31:30 1.65 @@ -37,18 +37,26 @@ 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())); } /** * Returns a new queue of given size containing consecutive - * Integers 0 ... n. + * Integers 0 ... n - 1. */ private LinkedBlockingQueue populatedQueue(int n) { LinkedBlockingQueue q = @@ -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; } @@ -108,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 { @@ -148,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)); } } @@ -202,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 { @@ -440,25 +449,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); + waitForThreadToEnterWaitState(t); t.interrupt(); - awaitTermination(t, MEDIUM_DELAY_MS); + awaitTermination(t); checkEmpty(q); } @@ -574,7 +581,7 @@ public class LinkedBlockingQueueTest ext assertTrue(changed); assertTrue(q.containsAll(p)); - assertEquals(SIZE-i, q.size()); + assertEquals(SIZE - i, q.size()); p.remove(); } } @@ -587,7 +594,7 @@ 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 x = (Integer)(p.remove()); assertFalse(q.contains(x)); @@ -634,9 +641,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()); } /** @@ -707,23 +729,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()); + }}); + } } /** @@ -732,22 +754,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); + }}); + } } /** @@ -799,7 +821,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(); @@ -824,7 +846,7 @@ public class LinkedBlockingQueueTest ext q.drainTo(l, i); int k = (i < SIZE) ? i : SIZE; assertEquals(k, l.size()); - assertEquals(SIZE-k, q.size()); + assertEquals(SIZE - k, q.size()); for (int j = 0; j < k; ++j) assertEquals(l.get(j), new Integer(j)); do {} while (q.poll() != null);