--- jsr166/src/test/tck/LinkedTransferQueueTest.java 2010/11/03 16:46:34 1.37 +++ jsr166/src/test/tck/LinkedTransferQueueTest.java 2015/10/18 04:48:32 1.66 @@ -1,29 +1,36 @@ /* * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at - * http://creativecommons.org/licenses/publicdomain + * http://creativecommons.org/publicdomain/zero/1.0/ * Other contributors include John Vint */ -import java.io.BufferedInputStream; -import java.io.BufferedOutputStream; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; +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.concurrent.*; -import static java.util.concurrent.TimeUnit.MILLISECONDS; -import static java.util.concurrent.TimeUnit.NANOSECONDS; +import java.util.Queue; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Executors; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.LinkedTransferQueue; + import junit.framework.Test; -import junit.framework.TestSuite; @SuppressWarnings({"unchecked", "rawtypes"}) public class LinkedTransferQueueTest extends JSR166TestCase { + static 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; } + } public static class Generic extends BlockingQueueTest { protected BlockingQueue emptyCollection() { @@ -32,39 +39,13 @@ public class LinkedTransferQueueTest ext } public static void main(String[] args) { - junit.textui.TestRunner.run(suite()); + main(suite(), args); } public static Test suite() { return newTestSuite(LinkedTransferQueueTest.class, - new Generic().testSuite()); - } - - void checkEmpty(BlockingQueue q) { - try { - assertTrue(q.isEmpty()); - assertEquals(0, q.size()); - assertNull(q.peek()); - assertNull(q.poll()); - assertNull(q.poll(0, MILLISECONDS)); - assertEquals(q.toString(), "[]"); - assertTrue(Arrays.equals(q.toArray(), new Object[0])); - assertFalse(q.iterator().hasNext()); - try { - q.element(); - shouldThrow(); - } catch (NoSuchElementException success) {} - try { - q.iterator().next(); - shouldThrow(); - } catch (NoSuchElementException success) {} - try { - q.remove(); - shouldThrow(); - } catch (NoSuchElementException success) {} - } catch (InterruptedException ie) { - threadUnexpectedException(ie); - } + new Generic().testSuite(), + CollectionTest.testSuite(new Implementation())); } /** @@ -92,9 +73,9 @@ public class LinkedTransferQueueTest ext * NullPointerException */ public void testConstructor3() { + Collection elements = Arrays.asList(new Integer[SIZE]); try { - Integer[] ints = new Integer[SIZE]; - new LinkedTransferQueue(Arrays.asList(ints)); + new LinkedTransferQueue(elements); shouldThrow(); } catch (NullPointerException success) {} } @@ -104,12 +85,12 @@ public class LinkedTransferQueueTest ext * throws NullPointerException */ public void testConstructor4() { + Integer[] ints = new Integer[SIZE]; + for (int i = 0; i < SIZE - 1; ++i) + ints[i] = i; + Collection elements = Arrays.asList(ints); try { - Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE - 1; ++i) { - ints[i] = i; - } - new LinkedTransferQueue(Arrays.asList(ints)); + new LinkedTransferQueue(elements); shouldThrow(); } catch (NullPointerException success) {} } @@ -142,86 +123,40 @@ 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)); } } /** - * offer(null) throws NullPointerException - */ - public void testOfferNull() { - try { - LinkedTransferQueue q = new LinkedTransferQueue(); - q.offer(null); - shouldThrow(); - } catch (NullPointerException success) {} - } - - /** - * add(null) throws NullPointerException - */ - public void testAddNull() { - try { - LinkedTransferQueue q = new LinkedTransferQueue(); - q.add(null); - shouldThrow(); - } catch (NullPointerException success) {} - } - - /** - * addAll(null) throws NullPointerException - */ - public void testAddAll1() { - try { - LinkedTransferQueue q = new LinkedTransferQueue(); - q.addAll(null); - shouldThrow(); - } catch (NullPointerException success) {} - } - - /** * addAll(this) throws IllegalArgumentException */ public void testAddAllSelf() { + LinkedTransferQueue q = populatedQueue(SIZE); try { - LinkedTransferQueue q = populatedQueue(SIZE); q.addAll(q); shouldThrow(); } catch (IllegalArgumentException success) {} } /** - * addAll of a collection with null elements throws NullPointerException - */ - public void testAddAll2() { - try { - LinkedTransferQueue q = new LinkedTransferQueue(); - Integer[] ints = new Integer[SIZE]; - q.addAll(Arrays.asList(ints)); - shouldThrow(); - } catch (NullPointerException success) {} - } - - /** * addAll of a collection with any null elements throws * 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) {} @@ -245,23 +180,12 @@ public class LinkedTransferQueueTest ext } /** - * put(null) throws NullPointerException - */ - public void testPutNull() throws InterruptedException { - try { - LinkedTransferQueue q = new LinkedTransferQueue(); - q.put(null); - shouldThrow(); - } catch (NullPointerException success) {} - } - - /** * all elements successfully put are contained */ public void testPut() { LinkedTransferQueue q = new LinkedTransferQueue(); for (int i = 0; i < SIZE; ++i) { - assertEquals(q.size(), i); + assertEquals(i, q.size()); q.put(i); assertTrue(q.contains(i)); } @@ -281,25 +205,33 @@ public class LinkedTransferQueueTest ext * take removes existing elements until empty, then blocks interruptibly */ public void testBlockingTake() throws InterruptedException { - final BlockingQueue q = populatedQueue(SIZE); - final CountDownLatch aboutToWait = new CountDownLatch(1); + final BlockingQueue q = populatedQueue(SIZE); + 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, (int) q.take()); + assertEquals(i, q.take()); } - aboutToWait.countDown(); + + Thread.currentThread().interrupt(); + try { + q.take(); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + + pleaseInterrupt.countDown(); try { q.take(); shouldThrow(); } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); }}); - aboutToWait.await(); - waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); + await(pleaseInterrupt); + assertThreadStaysAlive(t); t.interrupt(); - awaitTermination(t, MEDIUM_DELAY_MS); - checkEmpty(q); + awaitTermination(t); } /** @@ -331,14 +263,14 @@ public class LinkedTransferQueueTest ext */ public void testTimedPoll() throws InterruptedException { LinkedTransferQueue q = populatedQueue(SIZE); - for (int i = 0; i < SIZE; ++i) { - long t0 = System.nanoTime(); - assertEquals(i, (int) q.poll(SMALL_DELAY_MS, MILLISECONDS)); - assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); - } - long t0 = System.nanoTime(); - assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); - assertTrue(millisElapsedSince(t0) >= SHORT_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); } @@ -351,25 +283,21 @@ public class LinkedTransferQueueTest ext final CountDownLatch aboutToWait = 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(); 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) {} + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); }}); aboutToWait.await(); - waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); + waitForThreadToEnterWaitState(t); t.interrupt(); - awaitTermination(t, MEDIUM_DELAY_MS); + awaitTermination(t); checkEmpty(q); } @@ -381,19 +309,18 @@ 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(); + for (int i = 0; i < SIZE; ++i) assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); - assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); - } try { - q.poll(MEDIUM_DELAY_MS, MILLISECONDS); + q.poll(LONG_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (InterruptedException success) {} + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); }}); - awaitTermination(t, MEDIUM_DELAY_MS); + awaitTermination(t); checkEmpty(q); } @@ -444,21 +371,6 @@ public class LinkedTransferQueueTest ext } /** - * remove(x) removes x and returns true if present - */ - public void testRemoveElement() throws InterruptedException { - LinkedTransferQueue q = populatedQueue(SIZE); - for (int i = 1; i < SIZE; i += 2) { - assertTrue(q.remove(i)); - } - for (int i = 0; i < SIZE; i += 2) { - assertTrue(q.remove(i)); - assertFalse(q.remove(i + 1)); - } - checkEmpty(q); - } - - /** * An add following remove(x) succeeds */ public void testRemoveElementAndAdd() throws InterruptedException { @@ -550,40 +462,30 @@ public class LinkedTransferQueueTest ext } /** - * toArray() contains all elements + * toArray() contains all elements in FIFO order */ - public void testToArray() throws InterruptedException { + public void testToArray() { LinkedTransferQueue q = populatedQueue(SIZE); Object[] o = q.toArray(); for (int i = 0; i < o.length; i++) { - assertEquals(o[i], q.take()); + assertSame(o[i], q.poll()); } } /** - * toArray(a) contains all elements + * toArray(a) contains all elements in FIFO order */ - public void testToArray2() throws InterruptedException { + public void testToArray2() { LinkedTransferQueue q = populatedQueue(SIZE); Integer[] ints = new Integer[SIZE]; - ints = q.toArray(ints); + Integer[] array = q.toArray(ints); + assertSame(ints, array); for (int i = 0; i < ints.length; i++) { - assertEquals(ints[i], q.take()); + assertSame(ints[i], q.poll()); } } /** - * toArray(null) throws NullPointerException - */ - public void testToArray_NullArg() { - LinkedTransferQueue q = populatedQueue(SIZE); - try { - q.toArray(null); - shouldThrow(); - } catch (NullPointerException success) {} - } - - /** * toArray(incompatible array type) throws ArrayStoreException */ public void testToArray1_BadArg() { @@ -600,11 +502,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()); } /** @@ -666,7 +581,7 @@ public class LinkedTransferQueueTest ext LinkedTransferQueue q = populatedQueue(SIZE); String s = q.toString(); for (int i = 0; i < SIZE; ++i) { - assertTrue(s.indexOf(String.valueOf(i)) >= 0); + assertTrue(s.contains(String.valueOf(i))); } } @@ -675,25 +590,25 @@ public class LinkedTransferQueueTest ext */ public void testOfferInExecutor() { final LinkedTransferQueue q = new LinkedTransferQueue(); - final CountDownLatch threadsStarted = new CountDownLatch(2); - ExecutorService executor = Executors.newFixedThreadPool(2); - - executor.execute(new CheckedRunnable() { - public void realRun() throws InterruptedException { - threadsStarted.countDown(); - threadsStarted.await(); - assertTrue(q.offer(one, MEDIUM_DELAY_MS, MILLISECONDS)); - }}); + final CheckedBarrier threadsStarted = new CheckedBarrier(2); + final ExecutorService executor = Executors.newFixedThreadPool(2); + try (PoolCleaner cleaner = cleaner(executor)) { - executor.execute(new CheckedRunnable() { - public void realRun() throws InterruptedException { - threadsStarted.countDown(); - 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); + }}); + } } /** @@ -701,74 +616,44 @@ public class LinkedTransferQueueTest ext */ public void testPollInExecutor() { final LinkedTransferQueue q = new LinkedTransferQueue(); - final CountDownLatch threadsStarted = new CountDownLatch(2); - ExecutorService executor = Executors.newFixedThreadPool(2); - - executor.execute(new CheckedRunnable() { - public void realRun() throws InterruptedException { - assertNull(q.poll()); - threadsStarted.countDown(); - threadsStarted.await(); - assertSame(one, q.poll(SMALL_DELAY_MS, MILLISECONDS)); - checkEmpty(q); - }}); + final CheckedBarrier threadsStarted = new CheckedBarrier(2); + final ExecutorService executor = Executors.newFixedThreadPool(2); + try (PoolCleaner cleaner = cleaner(executor)) { - executor.execute(new CheckedRunnable() { - public void realRun() throws InterruptedException { - threadsStarted.countDown(); - 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 */ public void testSerialization() throws Exception { - LinkedTransferQueue q = populatedQueue(SIZE); - - ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); - ObjectOutputStream out - = new ObjectOutputStream(new BufferedOutputStream(bout)); - out.writeObject(q); - out.close(); + Queue x = populatedQueue(SIZE); + Queue y = serialClone(x); - ByteArrayInputStream bin - = new ByteArrayInputStream(bout.toByteArray()); - ObjectInputStream in - = new ObjectInputStream(new BufferedInputStream(bin)); - LinkedTransferQueue r = (LinkedTransferQueue) in.readObject(); - - assertEquals(q.size(), r.size()); - assertEquals(q.toString(), r.toString()); - assertTrue(Arrays.equals(q.toArray(), r.toArray())); - while (!q.isEmpty()) { - assertEquals(q.remove(), r.remove()); + assertNotSame(y, x); + assertEquals(x.size(), y.size()); + assertEquals(x.toString(), y.toString()); + assertTrue(Arrays.equals(x.toArray(), y.toArray())); + while (!x.isEmpty()) { + assertFalse(y.isEmpty()); + assertEquals(x.remove(), y.remove()); } - } - - /** - * drainTo(null) throws NullPointerException - */ - public void testDrainToNull() { - LinkedTransferQueue q = populatedQueue(SIZE); - try { - q.drainTo(null); - shouldThrow(); - } catch (NullPointerException success) {} - } - - /** - * drainTo(this) throws IllegalArgumentException - */ - public void testDrainToSelf() { - LinkedTransferQueue q = populatedQueue(SIZE); - try { - q.drainTo(q); - shouldThrow(); - } catch (IllegalArgumentException success) {} + assertTrue(y.isEmpty()); } /** @@ -778,10 +663,10 @@ public class LinkedTransferQueueTest ext LinkedTransferQueue 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), i); + assertEquals(i, l.get(i)); } q.add(zero); q.add(one); @@ -790,10 +675,10 @@ public class LinkedTransferQueueTest 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), i); + assertEquals(i, l.get(i)); } } @@ -809,36 +694,13 @@ public class LinkedTransferQueueTest ext ArrayList l = new ArrayList(); q.drainTo(l); assertTrue(l.size() >= SIZE); - for (int i = 0; i < SIZE; ++i) { - assertEquals(l.get(i), i); - } - awaitTermination(t, MEDIUM_DELAY_MS); + for (int i = 0; i < SIZE; ++i) + assertEquals(i, l.get(i)); + awaitTermination(t); assertTrue(q.size() + l.size() >= SIZE); } /** - * drainTo(null, n) throws NullPointerException - */ - public void testDrainToNullN() { - LinkedTransferQueue q = populatedQueue(SIZE); - try { - q.drainTo(null, SIZE); - shouldThrow(); - } catch (NullPointerException success) {} - } - - /** - * drainTo(this, n) throws IllegalArgumentException - */ - public void testDrainToSelfN() { - LinkedTransferQueue q = populatedQueue(SIZE); - try { - q.drainTo(q, SIZE); - shouldThrow(); - } catch (IllegalArgumentException success) {} - } - - /** * drainTo(c, n) empties first min(n, size) elements of queue into c */ public void testDrainToN() { @@ -850,13 +712,11 @@ public class LinkedTransferQueueTest ext ArrayList l = new ArrayList(); q.drainTo(l, i); int k = (i < SIZE) ? i : SIZE; - assertEquals(l.size(), k); - assertEquals(q.size(), SIZE - k); - for (int j = 0; j < k; ++j) { - assertEquals(l.get(j), j); - } - while (q.poll() != null) - ; + assertEquals(k, l.size()); + assertEquals(SIZE - k, q.size()); + for (int j = 0; j < k; ++j) + assertEquals(j, l.get(j)); + do {} while (q.poll() != null); } } @@ -866,28 +726,30 @@ public class LinkedTransferQueueTest ext */ public void testWaitingConsumer() throws InterruptedException { final LinkedTransferQueue q = new LinkedTransferQueue(); - assertEquals(q.getWaitingConsumerCount(), 0); + assertEquals(0, q.getWaitingConsumerCount()); assertFalse(q.hasWaitingConsumer()); final CountDownLatch threadStarted = new CountDownLatch(1); 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(q.getWaitingConsumerCount(), 0); + assertEquals(0, q.getWaitingConsumerCount()); assertFalse(q.hasWaitingConsumer()); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); }}); threadStarted.await(); - waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); - assertEquals(q.getWaitingConsumerCount(), 1); + waitForThreadToEnterWaitState(t); + assertEquals(1, q.getWaitingConsumerCount()); assertTrue(q.hasWaitingConsumer()); assertTrue(q.offer(one)); - assertEquals(q.getWaitingConsumerCount(), 0); + assertEquals(0, q.getWaitingConsumerCount()); assertFalse(q.hasWaitingConsumer()); - awaitTermination(t, MEDIUM_DELAY_MS); + awaitTermination(t); } /** @@ -918,11 +780,11 @@ public class LinkedTransferQueueTest ext }}); threadStarted.await(); - waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); + waitForThreadToEnterWaitState(t); assertEquals(1, q.size()); assertSame(five, q.poll()); checkEmpty(q); - awaitTermination(t, MEDIUM_DELAY_MS); + awaitTermination(t); } /** @@ -978,7 +840,7 @@ public class LinkedTransferQueueTest ext assertEquals(1, q.size()); assertTrue(q.offer(three)); assertSame(four, q.poll()); - awaitTermination(t, MEDIUM_DELAY_MS); + awaitTermination(t); } /** @@ -1001,15 +863,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) {} @@ -1043,9 +905,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); } /** @@ -1067,32 +931,40 @@ public class LinkedTransferQueueTest ext assertSame(q.take(), hotPotato); checkEmpty(q); - awaitTermination(t, MEDIUM_DELAY_MS); + awaitTermination(t); } /** - * tryTransfer waits the amount given if interrupted, and - * throws interrupted exception + * tryTransfer blocks interruptibly if no takers */ public void testTryTransfer5() throws InterruptedException { final LinkedTransferQueue q = new LinkedTransferQueue(); - final CountDownLatch threadStarted = new CountDownLatch(1); + final CountDownLatch pleaseInterrupt = new CountDownLatch(1); + assertTrue(q.isEmpty()); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { - long t0 = System.nanoTime(); - threadStarted.countDown(); + long startTime = System.nanoTime(); + Thread.currentThread().interrupt(); try { q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (InterruptedException success) {} - assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); + assertFalse(Thread.interrupted()); + + pleaseInterrupt.countDown(); + try { + q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); }}); - threadStarted.await(); - Thread.sleep(SHORT_DELAY_MS); + await(pleaseInterrupt); + assertThreadStaysAlive(t); t.interrupt(); - awaitTermination(t, MEDIUM_DELAY_MS); + awaitTermination(t); checkEmpty(q); } @@ -1104,14 +976,14 @@ 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(), - SHORT_DELAY_MS, MILLISECONDS)); - assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); + timeoutMillis(), MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); checkEmpty(q); }}); - awaitTermination(t, MEDIUM_DELAY_MS); + awaitTermination(t); checkEmpty(q); } @@ -1125,7 +997,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); }}); @@ -1135,20 +1009,20 @@ public class LinkedTransferQueueTest ext assertSame(four, q.poll()); assertSame(five, q.poll()); checkEmpty(q); - awaitTermination(t, MEDIUM_DELAY_MS); + awaitTermination(t); } /** - * tryTransfer attempts to enqueue into the q and fails returning - * false not enqueueing and the successive poll is null + * tryTransfer attempts to enqueue into the queue and fails + * returning false not enqueueing and the successive poll is null */ public void testTryTransfer8() throws InterruptedException { final LinkedTransferQueue q = new LinkedTransferQueue(); assertTrue(q.offer(four)); assertEquals(1, q.size()); - long t0 = System.nanoTime(); - assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS)); - assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); + long startTime = System.nanoTime(); + assertFalse(q.tryTransfer(five, timeoutMillis(), MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); assertEquals(1, q.size()); assertSame(four, q.poll()); assertNull(q.poll()); @@ -1166,4 +1040,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)); + } + } }