--- jsr166/src/test/tck/LinkedTransferQueueTest.java 2013/05/30 03:28:55 1.52 +++ jsr166/src/test/tck/LinkedTransferQueueTest.java 2019/08/11 22:29:27 1.82 @@ -5,25 +5,26 @@ * Other contributors include John Vint */ -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.List; import java.util.NoSuchElementException; import java.util.Queue; import java.util.concurrent.BlockingQueue; +import java.util.concurrent.Callable; import java.util.concurrent.CountDownLatch; import java.util.concurrent.Executors; import java.util.concurrent.ExecutorService; import java.util.concurrent.LinkedTransferQueue; -import static java.util.concurrent.TimeUnit.MILLISECONDS; -import static java.util.concurrent.TimeUnit.NANOSECONDS; + +import junit.framework.Test; @SuppressWarnings({"unchecked", "rawtypes"}) public class LinkedTransferQueueTest extends JSR166TestCase { - public static class Generic extends BlockingQueueTest { protected BlockingQueue emptyCollection() { return new LinkedTransferQueue(); @@ -31,12 +32,20 @@ public class LinkedTransferQueueTest 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 LinkedTransferQueue.class; } + public Collection emptyCollection() { return new LinkedTransferQueue(); } + public Object makeElement(int i) { return i; } + public boolean isConcurrent() { return true; } + public boolean permitsNulls() { return false; } + } return newTestSuite(LinkedTransferQueueTest.class, - new Generic().testSuite()); + new Generic().testSuite(), + CollectionTest.testSuite(new Implementation())); } /** @@ -77,7 +86,7 @@ public class LinkedTransferQueueTest ext */ public void testConstructor4() { Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE-1; ++i) + for (int i = 0; i < SIZE - 1; ++i) ints[i] = i; Collection elements = Arrays.asList(ints); try { @@ -114,16 +123,16 @@ public class LinkedTransferQueueTest ext * remainingCapacity() always returns Integer.MAX_VALUE */ public void testRemainingCapacity() { - LinkedTransferQueue q = populatedQueue(SIZE); + BlockingQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); assertEquals(SIZE - i, q.size()); - q.remove(); + assertEquals(i, q.remove()); } for (int i = 0; i < SIZE; ++i) { assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); assertEquals(i, q.size()); - q.add(i); + assertTrue(q.add(i)); } } @@ -131,8 +140,8 @@ public class LinkedTransferQueueTest ext * addAll(this) throws IllegalArgumentException */ public void testAddAllSelf() { + LinkedTransferQueue q = populatedQueue(SIZE); try { - LinkedTransferQueue q = populatedQueue(SIZE); q.addAll(q); shouldThrow(); } catch (IllegalArgumentException success) {} @@ -143,12 +152,11 @@ public class LinkedTransferQueueTest ext * NullPointerException after possibly adding some elements */ public void testAddAll3() { + LinkedTransferQueue q = new LinkedTransferQueue(); + Integer[] ints = new Integer[SIZE]; + for (int i = 0; i < SIZE - 1; ++i) + ints[i] = i; try { - LinkedTransferQueue q = new LinkedTransferQueue(); - Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE - 1; ++i) { - ints[i] = i; - } q.addAll(Arrays.asList(ints)); shouldThrow(); } catch (NullPointerException success) {} @@ -175,7 +183,7 @@ public class LinkedTransferQueueTest ext * all elements successfully put are contained */ public void testPut() { - LinkedTransferQueue q = new LinkedTransferQueue(); + LinkedTransferQueue q = new LinkedTransferQueue<>(); for (int i = 0; i < SIZE; ++i) { assertEquals(i, q.size()); q.put(i); @@ -201,9 +209,7 @@ public class LinkedTransferQueueTest 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 { @@ -221,7 +227,7 @@ public class LinkedTransferQueueTest ext }}); await(pleaseInterrupt); - assertThreadStaysAlive(t); + if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); t.interrupt(); awaitTermination(t); } @@ -255,12 +261,12 @@ public class LinkedTransferQueueTest ext */ public void testTimedPoll() throws InterruptedException { LinkedTransferQueue q = populatedQueue(SIZE); - for (int i = 0; i < SIZE; ++i) { - long startTime = System.nanoTime(); - assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); - assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); - } long startTime = System.nanoTime(); + for (int i = 0; i < SIZE; ++i) + assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); + + startTime = System.nanoTime(); assertNull(q.poll(timeoutMillis(), MILLISECONDS)); assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); checkEmpty(q); @@ -272,28 +278,34 @@ public class LinkedTransferQueueTest ext */ public void testInterruptedTimedPoll() throws InterruptedException { final BlockingQueue q = populatedQueue(SIZE); - final CountDownLatch aboutToWait = new CountDownLatch(1); + final CountDownLatch pleaseInterrupt = new CountDownLatch(1); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { - for (int i = 0; i < SIZE; ++i) { - long t0 = System.nanoTime(); + long startTime = System.nanoTime(); + for (int i = 0; i < SIZE; i++) assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); - assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); - } - long t0 = System.nanoTime(); - aboutToWait.countDown(); + + Thread.currentThread().interrupt(); + try { + q.poll(randomTimeout(), randomTimeUnit()); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + + pleaseInterrupt.countDown(); try { - q.poll(MEDIUM_DELAY_MS, MILLISECONDS); + q.poll(LONG_DELAY_MS, MILLISECONDS); shouldThrow(); - } catch (InterruptedException success) { - assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); - } + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); }}); - aboutToWait.await(); - waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); + await(pleaseInterrupt); + if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); t.interrupt(); - awaitTermination(t, MEDIUM_DELAY_MS); + awaitTermination(t); checkEmpty(q); } @@ -305,19 +317,19 @@ public class LinkedTransferQueueTest ext final BlockingQueue q = populatedQueue(SIZE); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { + long startTime = System.nanoTime(); Thread.currentThread().interrupt(); - 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); - } + for (int i = 0; i < SIZE; ++i) + assertEquals(i, (int) q.poll(randomTimeout(), randomTimeUnit())); try { - q.poll(MEDIUM_DELAY_MS, MILLISECONDS); + q.poll(randomTimeout(), randomTimeUnit()); shouldThrow(); } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); }}); - awaitTermination(t, MEDIUM_DELAY_MS); + awaitTermination(t); checkEmpty(q); } @@ -413,7 +425,7 @@ public class LinkedTransferQueueTest ext */ public void testContainsAll() { LinkedTransferQueue q = populatedQueue(SIZE); - LinkedTransferQueue p = new LinkedTransferQueue(); + LinkedTransferQueue p = new LinkedTransferQueue<>(); for (int i = 0; i < SIZE; ++i) { assertTrue(q.containsAll(p)); assertFalse(p.containsAll(q)); @@ -463,10 +475,11 @@ public class LinkedTransferQueueTest ext */ public void testToArray() { LinkedTransferQueue q = populatedQueue(SIZE); - Object[] o = q.toArray(); - for (int i = 0; i < o.length; i++) { - assertSame(o[i], q.poll()); - } + Object[] a = q.toArray(); + assertSame(Object[].class, a.getClass()); + for (Object o : a) + assertSame(o, q.poll()); + assertTrue(q.isEmpty()); } /** @@ -477,9 +490,9 @@ public class LinkedTransferQueueTest ext Integer[] ints = new Integer[SIZE]; Integer[] array = q.toArray(ints); assertSame(ints, array); - for (int i = 0; i < ints.length; i++) { - assertSame(ints[i], q.poll()); - } + for (Integer o : ints) + assertSame(o, q.poll()); + assertTrue(q.isEmpty()); } /** @@ -499,11 +512,24 @@ public class LinkedTransferQueueTest ext public void testIterator() throws InterruptedException { LinkedTransferQueue q = populatedQueue(SIZE); Iterator it = q.iterator(); - int i = 0; - while (it.hasNext()) { - assertEquals(it.next(), i++); - } + 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 LinkedTransferQueue().iterator()); } /** @@ -529,8 +555,7 @@ public class LinkedTransferQueueTest ext * iterator ordering is FIFO */ public void testIteratorOrdering() { - final LinkedTransferQueue q - = new LinkedTransferQueue(); + final LinkedTransferQueue q = new LinkedTransferQueue<>(); assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); q.add(one); q.add(two); @@ -575,22 +600,24 @@ public class LinkedTransferQueueTest ext public void testOfferInExecutor() { final LinkedTransferQueue q = new LinkedTransferQueue(); final CheckedBarrier threadsStarted = new CheckedBarrier(2); - ExecutorService executor = Executors.newFixedThreadPool(2); - - executor.execute(new CheckedRunnable() { - public void realRun() throws InterruptedException { - threadsStarted.await(); - assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS)); - }}); + final ExecutorService executor = Executors.newFixedThreadPool(2); + try (PoolCleaner cleaner = cleaner(executor)) { - executor.execute(new CheckedRunnable() { - public void realRun() throws InterruptedException { - threadsStarted.await(); - assertSame(one, q.take()); - checkEmpty(q); - }}); + executor.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { + threadsStarted.await(); + long startTime = System.nanoTime(); + assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); + }}); - joinPool(executor); + executor.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { + threadsStarted.await(); + assertSame(one, q.take()); + checkEmpty(q); + }}); + } } /** @@ -599,27 +626,29 @@ public class LinkedTransferQueueTest ext public void testPollInExecutor() { final LinkedTransferQueue q = new LinkedTransferQueue(); 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); - }}); + final ExecutorService executor = Executors.newFixedThreadPool(2); + try (PoolCleaner cleaner = cleaner(executor)) { - executor.execute(new CheckedRunnable() { - public void realRun() throws InterruptedException { - threadsStarted.await(); - q.put(one); - }}); + executor.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { + assertNull(q.poll()); + threadsStarted.await(); + long startTime = System.nanoTime(); + assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); + checkEmpty(q); + }}); - joinPool(executor); + executor.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { + threadsStarted.await(); + q.put(one); + }}); + } } /** - * A deserialized serialized queue has same elements in same order + * A deserialized/reserialized queue has same elements in same order */ public void testSerialization() throws Exception { Queue x = populatedQueue(SIZE); @@ -676,7 +705,7 @@ public class LinkedTransferQueueTest ext assertTrue(l.size() >= SIZE); for (int i = 0; i < SIZE; ++i) assertEquals(i, l.get(i)); - awaitTermination(t, MEDIUM_DELAY_MS); + awaitTermination(t); assertTrue(q.size() + l.size() >= SIZE); } @@ -696,8 +725,7 @@ public class LinkedTransferQueueTest ext assertEquals(SIZE - k, q.size()); for (int j = 0; j < k; ++j) assertEquals(j, l.get(j)); - while (q.poll() != null) - ; + do {} while (q.poll() != null); } } @@ -714,21 +742,25 @@ public class LinkedTransferQueueTest ext Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { threadStarted.countDown(); + long startTime = System.nanoTime(); assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); assertEquals(0, q.getWaitingConsumerCount()); assertFalse(q.hasWaitingConsumer()); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); }}); threadStarted.await(); - waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); - assertEquals(1, q.getWaitingConsumerCount()); - assertTrue(q.hasWaitingConsumer()); + Callable oneConsumer + = new Callable() { public Boolean call() { + return q.hasWaitingConsumer() + && q.getWaitingConsumerCount() == 1; }}; + waitForThreadToEnterWaitState(t, oneConsumer); assertTrue(q.offer(one)); assertEquals(0, q.getWaitingConsumerCount()); assertFalse(q.hasWaitingConsumer()); - awaitTermination(t, MEDIUM_DELAY_MS); + awaitTermination(t); } /** @@ -743,12 +775,11 @@ public class LinkedTransferQueueTest ext } /** - * transfer waits until a poll occurs. The transfered element - * is returned by this associated poll. + * transfer waits until a poll occurs. The transferred element + * is returned by the associated poll. */ public void testTransfer2() throws InterruptedException { - final LinkedTransferQueue q - = new LinkedTransferQueue(); + final LinkedTransferQueue q = new LinkedTransferQueue<>(); final CountDownLatch threadStarted = new CountDownLatch(1); Thread t = newStartedThread(new CheckedRunnable() { @@ -759,24 +790,26 @@ public class LinkedTransferQueueTest ext }}); threadStarted.await(); - waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); - assertEquals(1, q.size()); + Callable oneElement + = new Callable() { public Boolean call() { + return !q.isEmpty() && q.size() == 1; }}; + waitForThreadToEnterWaitState(t, oneElement); + assertSame(five, q.poll()); checkEmpty(q); - awaitTermination(t, MEDIUM_DELAY_MS); + awaitTermination(t); } /** * transfer waits until a poll occurs, and then transfers in fifo order */ public void testTransfer3() throws InterruptedException { - final LinkedTransferQueue q - = new LinkedTransferQueue(); + final LinkedTransferQueue q = new LinkedTransferQueue<>(); Thread first = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { q.transfer(four); - assertTrue(!q.contains(four)); + assertFalse(q.contains(four)); assertEquals(1, q.size()); }}); @@ -819,16 +852,15 @@ public class LinkedTransferQueueTest ext assertEquals(1, q.size()); assertTrue(q.offer(three)); assertSame(four, q.poll()); - awaitTermination(t, MEDIUM_DELAY_MS); + awaitTermination(t); } /** - * transfer waits until a take occurs. The transfered element - * is returned by this associated take. + * transfer waits until a take occurs. The transferred element + * is returned by the associated take. */ public void testTransfer5() throws InterruptedException { - final LinkedTransferQueue q - = new LinkedTransferQueue(); + final LinkedTransferQueue q = new LinkedTransferQueue<>(); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { @@ -842,15 +874,15 @@ public class LinkedTransferQueueTest ext assertEquals(1, q.size()); assertSame(four, q.take()); checkEmpty(q); - awaitTermination(t, MEDIUM_DELAY_MS); + awaitTermination(t); } /** * tryTransfer(null) throws NullPointerException */ public void testTryTransfer1() { + final LinkedTransferQueue q = new LinkedTransferQueue(); try { - final LinkedTransferQueue q = new LinkedTransferQueue(); q.tryTransfer(null); shouldThrow(); } catch (NullPointerException success) {} @@ -884,9 +916,11 @@ public class LinkedTransferQueueTest ext assertTrue(q.tryTransfer(hotPotato)); }}); - assertSame(hotPotato, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); + long startTime = System.nanoTime(); + assertSame(hotPotato, q.poll(LONG_DELAY_MS, MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); checkEmpty(q); - awaitTermination(t, MEDIUM_DELAY_MS); + awaitTermination(t); } /** @@ -908,7 +942,7 @@ public class LinkedTransferQueueTest ext assertSame(q.take(), hotPotato); checkEmpty(q); - awaitTermination(t, MEDIUM_DELAY_MS); + awaitTermination(t); } /** @@ -921,9 +955,10 @@ public class LinkedTransferQueueTest ext Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { + long startTime = System.nanoTime(); Thread.currentThread().interrupt(); try { - q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); + q.tryTransfer(new Object(), randomTimeout(), randomTimeUnit()); shouldThrow(); } catch (InterruptedException success) {} assertFalse(Thread.interrupted()); @@ -934,10 +969,12 @@ public class LinkedTransferQueueTest ext shouldThrow(); } catch (InterruptedException success) {} assertFalse(Thread.interrupted()); + + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); }}); await(pleaseInterrupt); - assertThreadStaysAlive(t); + if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); t.interrupt(); awaitTermination(t); checkEmpty(q); @@ -951,10 +988,10 @@ public class LinkedTransferQueueTest ext Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { - long t0 = System.nanoTime(); + long startTime = System.nanoTime(); assertFalse(q.tryTransfer(new Object(), timeoutMillis(), MILLISECONDS)); - assertTrue(millisElapsedSince(t0) >= timeoutMillis()); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); checkEmpty(q); }}); @@ -972,7 +1009,9 @@ public class LinkedTransferQueueTest ext Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { - assertTrue(q.tryTransfer(five, MEDIUM_DELAY_MS, MILLISECONDS)); + long startTime = System.nanoTime(); + assertTrue(q.tryTransfer(five, LONG_DELAY_MS, MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); checkEmpty(q); }}); @@ -982,7 +1021,7 @@ public class LinkedTransferQueueTest ext assertSame(four, q.poll()); assertSame(five, q.poll()); checkEmpty(q); - awaitTermination(t, MEDIUM_DELAY_MS); + awaitTermination(t); } /** @@ -993,9 +1032,9 @@ public class LinkedTransferQueueTest ext final LinkedTransferQueue q = new LinkedTransferQueue(); assertTrue(q.offer(four)); assertEquals(1, q.size()); - long t0 = System.nanoTime(); + long startTime = System.nanoTime(); assertFalse(q.tryTransfer(five, timeoutMillis(), MILLISECONDS)); - assertTrue(millisElapsedSince(t0) >= timeoutMillis()); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); assertEquals(1, q.size()); assertSame(four, q.poll()); assertNull(q.poll()); @@ -1003,7 +1042,7 @@ public class LinkedTransferQueueTest ext } private LinkedTransferQueue populatedQueue(int n) { - LinkedTransferQueue q = new LinkedTransferQueue(); + LinkedTransferQueue q = new LinkedTransferQueue<>(); checkEmpty(q); for (int i = 0; i < n; i++) { assertEquals(i, q.size()); @@ -1013,4 +1052,19 @@ public class LinkedTransferQueueTest ext assertFalse(q.isEmpty()); return q; } + + /** + * remove(null), contains(null) always return false + */ + public void testNeverContainsNull() { + Collection[] qs = { + new LinkedTransferQueue(), + populatedQueue(2), + }; + + for (Collection q : qs) { + assertFalse(q.contains(null)); + assertFalse(q.remove(null)); + } + } }