--- jsr166/src/test/tck/LinkedTransferQueueTest.java 2009/07/31 23:02:49 1.1 +++ jsr166/src/test/tck/LinkedTransferQueueTest.java 2015/04/25 04:55:31 1.59 @@ -1,227 +1,174 @@ - /* * 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.ConcurrentModificationException; +import java.util.Collection; import java.util.Iterator; +import java.util.List; import java.util.NoSuchElementException; -import java.util.concurrent.*; +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 { + public static class Generic extends BlockingQueueTest { + protected BlockingQueue emptyCollection() { + return new LinkedTransferQueue(); + } + } + public static void main(String[] args) { - junit.textui.TestRunner.run(suite()); + main(suite(), args); } public static Test suite() { - return new TestSuite(LinkedTransferQueueTest.class); + return newTestSuite(LinkedTransferQueueTest.class, + new Generic().testSuite()); } - /* - *Constructor builds new queue with size being zero and empty being true + /** + * Constructor builds new queue with size being zero and empty + * being true */ public void testConstructor1() { assertEquals(0, new LinkedTransferQueue().size()); assertTrue(new LinkedTransferQueue().isEmpty()); } - /* - * Initizialing constructor with null collection throws NPE + /** + * Initializing constructor with null collection throws + * NullPointerException */ public void testConstructor2() { try { new LinkedTransferQueue(null); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** - * Initializing from Collection of null elements throws NPE + * Initializing from Collection of null elements throws + * NullPointerException */ public void testConstructor3() { + Collection elements = Arrays.asList(new Integer[SIZE]); try { - Integer[] ints = new Integer[SIZE]; - LinkedTransferQueue q = new LinkedTransferQueue(Arrays.asList(ints)); + new LinkedTransferQueue(elements); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } - /* + + /** * Initializing constructor with a collection containing some null elements - * throws NPE + * 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] = new Integer(i); - } - LinkedTransferQueue q = new LinkedTransferQueue(Arrays.asList(ints)); + new LinkedTransferQueue(elements); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } - /* + /** * Queue contains all elements of the collection it is initialized by */ public void testConstructor5() { - try { - Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE; ++i) { - ints[i] = new Integer(i); - } - LinkedTransferQueue q = new LinkedTransferQueue(Arrays.asList(ints)); - for (int i = 0; i < SIZE; ++i) { - assertEquals(ints[i], q.poll()); - } - } finally { + Integer[] ints = new Integer[SIZE]; + for (int i = 0; i < SIZE; ++i) { + ints[i] = i; + } + List intList = Arrays.asList(ints); + LinkedTransferQueue q + = new LinkedTransferQueue(intList); + assertEquals(q.size(), intList.size()); + assertEquals(q.toString(), intList.toString()); + assertTrue(Arrays.equals(q.toArray(), + intList.toArray())); + assertTrue(Arrays.equals(q.toArray(new Object[0]), + intList.toArray(new Object[0]))); + assertTrue(Arrays.equals(q.toArray(new Object[SIZE]), + intList.toArray(new Object[SIZE]))); + for (int i = 0; i < SIZE; ++i) { + assertEquals(ints[i], q.poll()); } } /** - * Remaining capacity never decrease nor increase on add or remove + * remainingCapacity() always returns Integer.MAX_VALUE */ public void testRemainingCapacity() { - LinkedTransferQueue q = populatedQueue(SIZE); - int remainingCapacity = q.remainingCapacity(); + BlockingQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(remainingCapacity, q.remainingCapacity()); + 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(remainingCapacity, q.remainingCapacity()); + assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); assertEquals(i, q.size()); - q.add(new Integer(i)); - } - } - - /** - * offer(null) throws NPE - */ - public void testOfferNull() { - try { - LinkedTransferQueue q = new LinkedTransferQueue(); - q.offer(null); - shouldThrow(); - } catch (NullPointerException success) { + assertTrue(q.add(i)); } } /** - * add(null) throws NPE - */ - public void testAddNull() { - try { - LinkedTransferQueue q = new LinkedTransferQueue(); - q.add(null); - shouldThrow(); - } catch (NullPointerException success) { - } - } - - /** - * addAll(null) throws NPE - */ - public void testAddAll1() { - try { - LinkedTransferQueue q = new LinkedTransferQueue(); - q.addAll(null); - shouldThrow(); - } catch (NullPointerException success) { - } - } - - /** - * addAll(this) throws IAE + * addAll(this) throws IllegalArgumentException */ public void testAddAllSelf() { try { LinkedTransferQueue q = populatedQueue(SIZE); q.addAll(q); shouldThrow(); - } catch (IllegalArgumentException success) { - } - } - - /** - * addAll of a collection with null elements throws NPE - */ - public void testAddAll2() { - try { - LinkedTransferQueue q = new LinkedTransferQueue(); - Integer[] ints = new Integer[SIZE]; - q.addAll(Arrays.asList(ints)); - shouldThrow(); - } catch (NullPointerException success) { - } + } catch (IllegalArgumentException success) {} } /** - * addAll of a collection with any null elements throws NPE after - * possibly adding some elements + * addAll of a collection with any null elements throws + * NullPointerException after possibly adding some elements */ public void testAddAll3() { try { LinkedTransferQueue q = new LinkedTransferQueue(); Integer[] ints = new Integer[SIZE]; for (int i = 0; i < SIZE - 1; ++i) { - ints[i] = new Integer(i); + ints[i] = i; } q.addAll(Arrays.asList(ints)); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** * Queue contains all elements, in traversal order, of successful addAll */ public void testAddAll5() { - try { - Integer[] empty = new Integer[0]; - Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE; ++i) { - ints[i] = new Integer(i); - } - LinkedTransferQueue q = new LinkedTransferQueue(); - assertFalse(q.addAll(Arrays.asList(empty))); - assertTrue(q.addAll(Arrays.asList(ints))); - for (int i = 0; i < SIZE; ++i) { - assertEquals(ints[i], q.poll()); - } - } finally { + Integer[] empty = new Integer[0]; + Integer[] ints = new Integer[SIZE]; + for (int i = 0; i < SIZE; ++i) { + ints[i] = i; } - } - - /** - * put(null) throws NPE - */ - public void testPutNull() { - try { - LinkedTransferQueue q = new LinkedTransferQueue(); - q.put(null); - shouldThrow(); - } catch (NullPointerException success) { - } catch (Exception ie) { - unexpectedException(); + LinkedTransferQueue q = new LinkedTransferQueue(); + assertFalse(q.addAll(Arrays.asList(empty))); + assertTrue(q.addAll(Arrays.asList(ints))); + for (int i = 0; i < SIZE; ++i) { + assertEquals(ints[i], q.poll()); } } @@ -229,305 +176,256 @@ public class LinkedTransferQueueTest ext * all elements successfully put are contained */ public void testPut() { - try { - LinkedTransferQueue q = new LinkedTransferQueue(); - for (int i = 0; i < SIZE; ++i) { - Integer I = new Integer(i); - q.put(I); - assertTrue(q.contains(I)); - } - } catch (Exception ie) { - unexpectedException(); + LinkedTransferQueue q = new LinkedTransferQueue(); + for (int i = 0; i < SIZE; ++i) { + assertEquals(i, q.size()); + q.put(i); + assertTrue(q.contains(i)); } } /** * take retrieves elements in FIFO order */ - public void testTake() { - try { - LinkedTransferQueue q = populatedQueue(SIZE); - for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer) q.take()).intValue()); - } - } catch (InterruptedException e) { - unexpectedException(); + public void testTake() throws InterruptedException { + LinkedTransferQueue q = populatedQueue(SIZE); + for (int i = 0; i < SIZE; ++i) { + assertEquals(i, (int) q.take()); } } /** - * take blocks interruptibly when empty + * take removes existing elements until empty, then blocks interruptibly */ - public void testTakeFromEmpty() { - final LinkedTransferQueue q = new LinkedTransferQueue(); - Thread t = new Thread(new Runnable() { + public void testBlockingTake() throws InterruptedException { + 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, q.take()); + } - public void run() { + Thread.currentThread().interrupt(); try { q.take(); - threadShouldThrow(); - } catch (InterruptedException success) { - } - } - }); - try { - t.start(); - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); - } catch (Exception e) { - unexpectedException(); - } - } - /* - * Take removes existing elements until empty, then blocks interruptibly - */ - - public void testBlockingTake() { - Thread t = new Thread(new Runnable() { + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); - public void run() { + pleaseInterrupt.countDown(); try { - LinkedTransferQueue q = populatedQueue(SIZE); - for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer) q.take()).intValue()); - } q.take(); - threadShouldThrow(); - } catch (InterruptedException success) { - } - } - }); - t.start(); - try { - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); - } catch (InterruptedException ie) { - unexpectedException(); - } + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + }}); + + await(pleaseInterrupt); + assertThreadStaysAlive(t); + t.interrupt(); + awaitTermination(t); } /** * poll succeeds unless empty */ - public void testPoll() { - LinkedTransferQueue q = populatedQueue(SIZE); + public void testPoll() throws InterruptedException { + LinkedTransferQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer) q.poll()).intValue()); + assertEquals(i, (int) q.poll()); } assertNull(q.poll()); + checkEmpty(q); } /** - * timed pool with zero timeout succeeds when non-empty, else times out + * timed poll with zero timeout succeeds when non-empty, else times out */ - public void testTimedPoll0() { - try { - LinkedTransferQueue q = populatedQueue(SIZE); - for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer) q.poll(0, TimeUnit.MILLISECONDS)).intValue()); - } - assertNull(q.poll(0, TimeUnit.MILLISECONDS)); - } catch (InterruptedException e) { - unexpectedException(); + public void testTimedPoll0() throws InterruptedException { + LinkedTransferQueue q = populatedQueue(SIZE); + for (int i = 0; i < SIZE; ++i) { + assertEquals(i, (int) q.poll(0, MILLISECONDS)); } + assertNull(q.poll(0, MILLISECONDS)); + checkEmpty(q); } /** - * timed pool with nonzero timeout succeeds when non-empty, else times out + * timed poll with nonzero timeout succeeds when non-empty, else times out */ - public void testTimedPoll() { - try { - LinkedTransferQueue q = populatedQueue(SIZE); - for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer) q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); - } - assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); - } catch (InterruptedException e) { - unexpectedException(); - } + 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(); + assertNull(q.poll(timeoutMillis(), MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); + checkEmpty(q); } /** * Interrupted timed poll throws InterruptedException instead of * returning timeout status */ - public void testInterruptedTimedPoll() { - Thread t = new Thread(new Runnable() { - - public void run() { + public void testInterruptedTimedPoll() throws InterruptedException { + final BlockingQueue q = populatedQueue(SIZE); + 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(); + assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); + assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); + } + long t0 = System.nanoTime(); + aboutToWait.countDown(); try { - LinkedTransferQueue q = populatedQueue(SIZE); - for (int i = 0; i < SIZE; ++i) { - threadAssertEquals(i, ((Integer) q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); - } - threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); + q.poll(MEDIUM_DELAY_MS, MILLISECONDS); + shouldThrow(); } catch (InterruptedException success) { + assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); } - } - }); - t.start(); - try { - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); - } catch (InterruptedException ie) { - unexpectedException(); - } + }}); + + aboutToWait.await(); + waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); + t.interrupt(); + awaitTermination(t, MEDIUM_DELAY_MS); + checkEmpty(q); } /** - * timed poll before a delayed offer fails; after offer succeeds; - * on interruption throws - */ - public void testTimedPollWithOffer() { - final LinkedTransferQueue q = new LinkedTransferQueue(); - Thread t = new Thread(new Runnable() { - - public void run() { - try { - threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); - q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); - q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); - threadShouldThrow(); - } catch (InterruptedException success) { + * timed poll after thread interrupted throws InterruptedException + * instead of returning timeout status + */ + public void testTimedPollAfterInterrupt() throws InterruptedException { + final BlockingQueue q = populatedQueue(SIZE); + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + 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); } - } - }); - try { - t.start(); - Thread.sleep(SMALL_DELAY_MS); - assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); - t.interrupt(); - t.join(); - } catch (Exception e) { - unexpectedException(); - } + try { + q.poll(MEDIUM_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + }}); + + awaitTermination(t, MEDIUM_DELAY_MS); + checkEmpty(q); } /** * peek returns next element, or null if empty */ - public void testPeek() { - LinkedTransferQueue q = populatedQueue(SIZE); + public void testPeek() throws InterruptedException { + LinkedTransferQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer) q.peek()).intValue()); - q.poll(); + assertEquals(i, (int) q.peek()); + assertEquals(i, (int) q.poll()); assertTrue(q.peek() == null || - i != ((Integer) q.peek()).intValue()); + i != (int) q.peek()); } assertNull(q.peek()); + checkEmpty(q); } /** - * element returns next element, or throws NSEE if empty + * element returns next element, or throws NoSuchElementException if empty */ - public void testElement() { - LinkedTransferQueue q = populatedQueue(SIZE); + public void testElement() throws InterruptedException { + LinkedTransferQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer) q.element()).intValue()); - q.poll(); + assertEquals(i, (int) q.element()); + assertEquals(i, (int) q.poll()); } try { q.element(); shouldThrow(); - } catch (NoSuchElementException success) { - } + } catch (NoSuchElementException success) {} + checkEmpty(q); } /** - * remove removes next element, or throws NSEE if empty + * remove removes next element, or throws NoSuchElementException if empty */ - public void testRemove() { - LinkedTransferQueue q = populatedQueue(SIZE); + public void testRemove() throws InterruptedException { + LinkedTransferQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer) q.remove()).intValue()); + assertEquals(i, (int) q.remove()); } try { q.remove(); shouldThrow(); - } catch (NoSuchElementException success) { - } - } - - /** - * remove(x) removes x and returns true if present - */ - public void testRemoveElement() { - LinkedTransferQueue q = populatedQueue(SIZE); - for (int i = 1; i < SIZE; i += 2) { - assertTrue(q.remove(new Integer(i))); - } - for (int i = 0; i < SIZE; i += 2) { - assertTrue(q.remove(new Integer(i))); - assertFalse(q.remove(new Integer(i + 1))); - } - assertTrue(q.isEmpty()); + } catch (NoSuchElementException success) {} + checkEmpty(q); } /** * An add following remove(x) succeeds */ - public void testRemoveElementAndAdd() { - try { - LinkedTransferQueue q = new LinkedTransferQueue(); - assertTrue(q.add(new Integer(1))); - assertTrue(q.add(new Integer(2))); - assertTrue(q.remove(new Integer(1))); - assertTrue(q.remove(new Integer(2))); - assertTrue(q.add(new Integer(3))); - assertTrue(q.take() != null); - } catch (Exception e) { - unexpectedException(); - } + public void testRemoveElementAndAdd() throws InterruptedException { + LinkedTransferQueue q = new LinkedTransferQueue(); + assertTrue(q.add(one)); + assertTrue(q.add(two)); + assertTrue(q.remove(one)); + assertTrue(q.remove(two)); + assertTrue(q.add(three)); + assertSame(q.take(), three); } /** * contains(x) reports true when elements added but not yet removed */ public void testContains() { - LinkedTransferQueue q = populatedQueue(SIZE); + LinkedTransferQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertTrue(q.contains(new Integer(i))); - q.poll(); - assertFalse(q.contains(new Integer(i))); + assertTrue(q.contains(i)); + assertEquals(i, (int) q.poll()); + assertFalse(q.contains(i)); } } /** * clear removes all elements */ - public void testClear() { + public void testClear() throws InterruptedException { LinkedTransferQueue q = populatedQueue(SIZE); - int remainingCapacity = q.remainingCapacity(); q.clear(); - assertTrue(q.isEmpty()); - assertEquals(0, q.size()); - assertEquals(remainingCapacity, q.remainingCapacity()); + checkEmpty(q); + assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); q.add(one); assertFalse(q.isEmpty()); + assertEquals(1, q.size()); assertTrue(q.contains(one)); q.clear(); - assertTrue(q.isEmpty()); + checkEmpty(q); } /** * containsAll(c) is true when c contains a subset of elements */ public void testContainsAll() { - LinkedTransferQueue q = populatedQueue(SIZE); - LinkedTransferQueue p = new LinkedTransferQueue(); + LinkedTransferQueue q = populatedQueue(SIZE); + LinkedTransferQueue p = new LinkedTransferQueue(); for (int i = 0; i < SIZE; ++i) { assertTrue(q.containsAll(p)); assertFalse(p.containsAll(q)); - p.add(new Integer(i)); + p.add(i); } assertTrue(p.containsAll(q)); } /** - * retainAll(c) retains only those elements of c and reports true if changed + * retainAll(c) retains only those elements of c and reports true + * if changed */ public void testRetainAll() { LinkedTransferQueue q = populatedQueue(SIZE); @@ -546,7 +444,8 @@ public class LinkedTransferQueueTest ext } /** - * removeAll(c) removes only those elements of c and reports true if changed + * removeAll(c) removes only those elements of c and reports true + * if changed */ public void testRemoveAll() { for (int i = 1; i < SIZE; ++i) { @@ -555,84 +454,74 @@ public class LinkedTransferQueueTest ext assertTrue(q.removeAll(p)); assertEquals(SIZE - i, q.size()); for (int j = 0; j < i; ++j) { - Integer I = (Integer) (p.remove()); - assertFalse(q.contains(I)); + assertFalse(q.contains(p.remove())); } } } /** - * toArray contains all elements + * toArray() contains all elements in FIFO order */ public void testToArray() { LinkedTransferQueue q = populatedQueue(SIZE); Object[] o = q.toArray(); - try { - for (int i = 0; i < o.length; i++) { - assertEquals(o[i], q.take()); - } - } catch (InterruptedException e) { - unexpectedException(); + for (int i = 0; i < o.length; i++) { + assertSame(o[i], q.poll()); } } /** - * toArray(a) contains all elements + * toArray(a) contains all elements in FIFO order */ public void testToArray2() { - LinkedTransferQueue q = populatedQueue(SIZE); + LinkedTransferQueue q = populatedQueue(SIZE); Integer[] ints = new Integer[SIZE]; - ints = (Integer[]) q.toArray(ints); - try { - for (int i = 0; i < ints.length; i++) { - assertEquals(ints[i], q.take()); - } - } catch (InterruptedException e) { - unexpectedException(); - } - } - - /** - * toArray(null) throws NPE - */ - public void testToArray_BadArg() { - try { - LinkedTransferQueue q = populatedQueue(SIZE); - Object o[] = q.toArray(null); - shouldThrow(); - } catch (NullPointerException success) { + Integer[] array = q.toArray(ints); + assertSame(ints, array); + for (int i = 0; i < ints.length; i++) { + assertSame(ints[i], q.poll()); } } /** - * toArray with incompatible array type throws CCE + * toArray(incompatible array type) throws ArrayStoreException */ public void testToArray1_BadArg() { + LinkedTransferQueue q = populatedQueue(SIZE); try { - LinkedTransferQueue q = populatedQueue(SIZE); - Object o[] = q.toArray(new String[10]); + q.toArray(new String[10]); shouldThrow(); - } catch (ArrayStoreException success) { - } + } catch (ArrayStoreException success) {} } /** * iterator iterates through all elements */ - public void testIterator() { + public void testIterator() throws InterruptedException { LinkedTransferQueue q = populatedQueue(SIZE); Iterator it = q.iterator(); - try { - while (it.hasNext()) { - assertEquals(it.next(), q.take()); - } - } catch (InterruptedException e) { - unexpectedException(); - } + 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()); } /** - * iterator.remove removes current element + * iterator.remove() removes current element */ public void testIteratorRemove() { final LinkedTransferQueue q = new LinkedTransferQueue(); @@ -645,8 +534,8 @@ public class LinkedTransferQueueTest ext it.remove(); it = q.iterator(); - assertEquals(it.next(), one); - assertEquals(it.next(), three); + assertSame(it.next(), one); + assertSame(it.next(), three); assertFalse(it.hasNext()); } @@ -654,16 +543,16 @@ public class LinkedTransferQueueTest ext * iterator ordering is FIFO */ public void testIteratorOrdering() { - final LinkedTransferQueue q = new LinkedTransferQueue(); - int remainingCapacity = q.remainingCapacity(); + final LinkedTransferQueue q + = new LinkedTransferQueue(); + assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); q.add(one); q.add(two); q.add(three); - assertEquals(remainingCapacity, q.remainingCapacity()); + assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); int k = 0; - for (Iterator it = q.iterator(); it.hasNext();) { - int i = ((Integer) (it.next())).intValue(); - assertEquals(++k, i); + for (Integer n : q) { + assertEquals(++k, (int) n); } assertEquals(3, k); } @@ -676,13 +565,9 @@ public class LinkedTransferQueueTest ext q.add(one); q.add(two); q.add(three); - try { - for (Iterator it = q.iterator(); it.hasNext();) { - q.remove(); - it.next(); - } - } catch (ConcurrentModificationException e) { - unexpectedException(); + for (Iterator it = q.iterator(); it.hasNext();) { + q.remove(); + it.next(); } assertEquals(0, q.size()); } @@ -694,7 +579,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))); } } @@ -703,65 +588,46 @@ public class LinkedTransferQueueTest ext */ public void testOfferInExecutor() { final LinkedTransferQueue q = new LinkedTransferQueue(); - q.add(one); - q.add(two); + final CheckedBarrier threadsStarted = new CheckedBarrier(2); ExecutorService executor = Executors.newFixedThreadPool(2); - executor.execute(new Runnable() { - public void run() { - try { - threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); - } catch (Exception e) { - threadUnexpectedException(); - } - } - }); - - executor.execute(new Runnable() { - - public void run() { - try { - Thread.sleep(SMALL_DELAY_MS); - threadAssertEquals(one, q.take()); - } catch (InterruptedException e) { - threadUnexpectedException(); - } - } - }); + executor.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { + threadsStarted.await(); + assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS)); + }}); + + executor.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { + threadsStarted.await(); + assertSame(one, q.take()); + checkEmpty(q); + }}); joinPool(executor); } /** - * poll retrieves elements across Executor threads + * timed poll retrieves elements across Executor threads */ public void testPollInExecutor() { final LinkedTransferQueue q = new LinkedTransferQueue(); + final CheckedBarrier threadsStarted = new CheckedBarrier(2); ExecutorService executor = Executors.newFixedThreadPool(2); - executor.execute(new Runnable() { - public void run() { - threadAssertNull(q.poll()); - try { - threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); - threadAssertTrue(q.isEmpty()); - } catch (InterruptedException e) { - threadUnexpectedException(); - } - } - }); - - executor.execute(new Runnable() { - - public void run() { - try { - Thread.sleep(SMALL_DELAY_MS); - q.put(one); - } catch (InterruptedException e) { - threadUnexpectedException(); - } - } - }); + 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); } @@ -769,50 +635,19 @@ public class LinkedTransferQueueTest ext /** * A deserialized serialized queue has same elements in same order */ - public void testSerialization() { - LinkedTransferQueue q = populatedQueue(SIZE); - - try { - ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); - ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); - out.writeObject(q); - out.close(); - - ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); - ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); - LinkedTransferQueue r = (LinkedTransferQueue) in.readObject(); - - assertEquals(q.size(), r.size()); - while (!q.isEmpty()) { - assertEquals(q.remove(), r.remove()); - } - } catch (Exception e) { - unexpectedException(); - } - } - - /** - * drainTo(null) throws NPE - */ - public void testDrainToNull() { - LinkedTransferQueue q = populatedQueue(SIZE); - try { - q.drainTo(null); - shouldThrow(); - } catch (NullPointerException success) { - } - } - - /** - * drainTo(this) throws IAE - */ - public void testDrainToSelf() { - LinkedTransferQueue q = populatedQueue(SIZE); - try { - q.drainTo(q); - shouldThrow(); - } catch (IllegalArgumentException success) { + public void testSerialization() throws Exception { + Queue x = populatedQueue(SIZE); + Queue y = serialClone(x); + + 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()); } + assertTrue(y.isEmpty()); } /** @@ -822,10 +657,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), new Integer(i)); + assertEquals(i, l.get(i)); } q.add(zero); q.add(one); @@ -834,414 +669,376 @@ 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), new Integer(i)); + assertEquals(i, l.get(i)); } } /** - * drainTo empties full queue, unblocking a waiting put. + * drainTo(c) empties full queue, unblocking a waiting put. */ - public void testDrainToWithActivePut() { + public void testDrainToWithActivePut() throws InterruptedException { final LinkedTransferQueue q = populatedQueue(SIZE); - Thread t = new Thread(new Runnable() { - - public void run() { - try { - q.put(new Integer(SIZE + 1)); - } catch (Exception ie) { - threadUnexpectedException(); - } - } - }); - try { - t.start(); - ArrayList l = new ArrayList(); - q.drainTo(l); - assertTrue(l.size() >= SIZE); - for (int i = 0; i < SIZE; ++i) { - assertEquals(l.get(i), new Integer(i)); - } - t.join(); - assertTrue(q.size() + l.size() >= SIZE); - } catch (Exception e) { - unexpectedException(); - } - } - - /** - * drainTo(null, n) throws NPE - */ - public void testDrainToNullN() { - LinkedTransferQueue q = populatedQueue(SIZE); - try { - q.drainTo(null, 0); - shouldThrow(); - } catch (NullPointerException success) { - } - } - - /** - * drainTo(this, n) throws IAE - */ - public void testDrainToSelfN() { - LinkedTransferQueue q = populatedQueue(SIZE); - try { - q.drainTo(q, 0); - shouldThrow(); - } catch (IllegalArgumentException success) { - } + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() { + q.put(SIZE + 1); + }}); + ArrayList l = new ArrayList(); + q.drainTo(l); + assertTrue(l.size() >= SIZE); + for (int i = 0; i < SIZE; ++i) + assertEquals(i, l.get(i)); + awaitTermination(t, MEDIUM_DELAY_MS); + assertTrue(q.size() + l.size() >= SIZE); } /** - * drainTo(c, n) empties first max {n, size} elements of queue into c + * drainTo(c, n) empties first min(n, size) elements of queue into c */ public void testDrainToN() { LinkedTransferQueue q = new LinkedTransferQueue(); for (int i = 0; i < SIZE + 2; ++i) { for (int j = 0; j < SIZE; j++) { - assertTrue(q.offer(new Integer(j))); + assertTrue(q.offer(j)); } 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), new Integer(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); } } - /* - * poll and take should decrement the waiting consumer count + /** + * timed poll() or take() increments the waiting consumer count; + * offer(e) decrements the waiting consumer count */ - public void testWaitingConsumer() { - try { - final LinkedTransferQueue q = new LinkedTransferQueue(); - final ConsumerObserver waiting = new ConsumerObserver(); - new Thread(new Runnable() { + public void testWaitingConsumer() throws InterruptedException { + final LinkedTransferQueue q = new LinkedTransferQueue(); + assertEquals(0, q.getWaitingConsumerCount()); + assertFalse(q.hasWaitingConsumer()); + final CountDownLatch threadStarted = new CountDownLatch(1); - public void run() { - try { - threadAssertTrue(q.hasWaitingConsumer()); - waiting.setWaitingConsumer(q.getWaitingConsumerCount()); - threadAssertTrue(q.offer(new Object())); - } catch (Exception ex) { - threadUnexpectedException(); - } + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + threadStarted.countDown(); + assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); + assertEquals(0, q.getWaitingConsumerCount()); + assertFalse(q.hasWaitingConsumer()); + }}); - } - }).start(); - assertTrue(q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS) != null); - assertTrue(q.getWaitingConsumerCount() < waiting.getWaitingConsumers()); - } catch (Exception ex) { - this.unexpectedException(); - } + threadStarted.await(); + waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); + assertEquals(1, q.getWaitingConsumerCount()); + assertTrue(q.hasWaitingConsumer()); + + assertTrue(q.offer(one)); + assertEquals(0, q.getWaitingConsumerCount()); + assertFalse(q.hasWaitingConsumer()); + + awaitTermination(t, MEDIUM_DELAY_MS); } - /* - * Inserts null into transfer throws NPE - */ - public void testTransfer1() { + /** + * transfer(null) throws NullPointerException + */ + public void testTransfer1() throws InterruptedException { try { LinkedTransferQueue q = new LinkedTransferQueue(); q.transfer(null); shouldThrow(); - } catch (NullPointerException ex) { - } catch (Exception ex) { - this.unexpectedException(); - } + } catch (NullPointerException success) {} } - /* - * transfer attempts to insert into the queue then wait until that - * object is removed via take or poll. + /** + * transfer waits until a poll occurs. The transfered element + * is returned by this associated poll. */ - public void testTransfer2() { - final LinkedTransferQueue q = new LinkedTransferQueue(); - new Thread(new Runnable() { + public void testTransfer2() throws InterruptedException { + final LinkedTransferQueue q + = new LinkedTransferQueue(); + final CountDownLatch threadStarted = new CountDownLatch(1); - public void run() { - try { - q.transfer(new Integer(SIZE)); - threadAssertTrue(q.isEmpty()); - } catch (Exception ex) { - threadUnexpectedException(); - } - } - }).start(); + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + threadStarted.countDown(); + q.transfer(five); + checkEmpty(q); + }}); - try { - Thread.sleep(SHORT_DELAY_MS); - assertEquals(1, q.size()); - q.poll(); - assertTrue(q.isEmpty()); - } catch (Exception ex) { - this.unexpectedException(); - } - } - /* - * transfer will attempt to transfer in fifo order and continue waiting if - * the element being transfered is not polled or taken - */ - - public void testTransfer3() { - final LinkedTransferQueue q = new LinkedTransferQueue(); - new Thread(new Runnable() { - public void run() { - try { - Integer i; - q.transfer((i = new Integer(SIZE + 1))); - threadAssertTrue(!q.contains(i)); - threadAssertEquals(1, q.size()); - } catch (Exception ex) { - threadUnexpectedException(); - } - } - }).start(); - Thread interruptedThread = - new Thread(new Runnable() { - public void run() { - try { - q.transfer(new Integer(SIZE)); - threadShouldThrow(); - } catch (InterruptedException ex) { - } - } - }); - interruptedThread.start(); - try { - Thread.sleep(LONG_DELAY_MS); - assertEquals(2, q.size()); - q.poll(); - Thread.sleep(LONG_DELAY_MS); - interruptedThread.interrupt(); - assertEquals(1, q.size()); - } catch (Exception ex) { - this.unexpectedException(); - } + threadStarted.await(); + waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); + assertEquals(1, q.size()); + assertSame(five, q.poll()); + checkEmpty(q); + awaitTermination(t, MEDIUM_DELAY_MS); + } + + /** + * transfer waits until a poll occurs, and then transfers in fifo order + */ + public void testTransfer3() throws InterruptedException { + final LinkedTransferQueue q + = new LinkedTransferQueue(); + + Thread first = newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + q.transfer(four); + assertTrue(!q.contains(four)); + assertEquals(1, q.size()); + }}); + + Thread interruptedThread = newStartedThread( + new CheckedInterruptedRunnable() { + public void realRun() throws InterruptedException { + while (q.isEmpty()) + Thread.yield(); + q.transfer(five); + }}); + + while (q.size() < 2) + Thread.yield(); + assertEquals(2, q.size()); + assertSame(four, q.poll()); + first.join(); + assertEquals(1, q.size()); + interruptedThread.interrupt(); + interruptedThread.join(); + checkEmpty(q); } /** - * transfer will wait as long as a poll or take occurs if one does occur - * the waiting is finished and the thread that tries to poll/take - * wins in retrieving the element + * transfer waits until a poll occurs, at which point the polling + * thread returns the element */ - public void testTransfer4() { + public void testTransfer4() throws InterruptedException { final LinkedTransferQueue q = new LinkedTransferQueue(); - new Thread(new Runnable() { - public void run() { - try { - q.transfer(new Integer(four)); - threadAssertFalse(q.contains(new Integer(four))); - threadAssertEquals(new Integer(three), q.poll()); - } catch (Exception ex) { - threadUnexpectedException(); - } - } - }).start(); - try { - Thread.sleep(MEDIUM_DELAY_MS); - assertTrue(q.offer(three)); - assertEquals(new Integer(four), q.poll()); - } catch (Exception ex) { - this.unexpectedException(); - } + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + q.transfer(four); + assertFalse(q.contains(four)); + assertSame(three, q.poll()); + }}); + + while (q.isEmpty()) + Thread.yield(); + assertFalse(q.isEmpty()); + assertEquals(1, q.size()); + assertTrue(q.offer(three)); + assertSame(four, q.poll()); + awaitTermination(t, MEDIUM_DELAY_MS); } - /* - * Insert null into trTransfer throws NPE + + /** + * transfer waits until a take occurs. The transfered element + * is returned by this associated take. */ + public void testTransfer5() throws InterruptedException { + final LinkedTransferQueue q + = new LinkedTransferQueue(); + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + q.transfer(four); + checkEmpty(q); + }}); + + while (q.isEmpty()) + Thread.yield(); + assertFalse(q.isEmpty()); + assertEquals(1, q.size()); + assertSame(four, q.take()); + checkEmpty(q); + awaitTermination(t, MEDIUM_DELAY_MS); + } + + /** + * tryTransfer(null) throws NullPointerException + */ public void testTryTransfer1() { try { final LinkedTransferQueue q = new LinkedTransferQueue(); q.tryTransfer(null); - this.shouldThrow(); - } catch (NullPointerException ex) { - } catch (Exception ex) { - this.unexpectedException(); - } + shouldThrow(); + } catch (NullPointerException success) {} } - /* - * tryTransfer returns false and does not enqueue if there are no consumers - * waiting to poll or take. - */ - public void testTryTransfer2() { - try { - final LinkedTransferQueue q = new LinkedTransferQueue(); - assertFalse(q.tryTransfer(new Object())); - assertEquals(0, q.size()); - } catch (Exception ex) { - this.unexpectedException(); - } + /** + * tryTransfer returns false and does not enqueue if there are no + * consumers waiting to poll or take. + */ + public void testTryTransfer2() throws InterruptedException { + final LinkedTransferQueue q = new LinkedTransferQueue(); + assertFalse(q.tryTransfer(new Object())); + assertFalse(q.hasWaitingConsumer()); + checkEmpty(q); } - /* - * if there is a consumer waiting poll or take tryTransfer returns - * true while enqueueing object + + /** + * If there is a consumer waiting in timed poll, tryTransfer + * returns true while successfully transfering object. */ + public void testTryTransfer3() throws InterruptedException { + final Object hotPotato = new Object(); + final LinkedTransferQueue q = new LinkedTransferQueue(); - public void testTryTransfer3() { - try { - final LinkedTransferQueue q = new LinkedTransferQueue(); - new Thread(new Runnable() { + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() { + while (! q.hasWaitingConsumer()) + Thread.yield(); + assertTrue(q.hasWaitingConsumer()); + checkEmpty(q); + assertTrue(q.tryTransfer(hotPotato)); + }}); + + assertSame(hotPotato, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); + checkEmpty(q); + awaitTermination(t, MEDIUM_DELAY_MS); + } - public void run() { - try { - threadAssertTrue(q.hasWaitingConsumer()); - threadAssertTrue(q.tryTransfer(new Object())); - } catch (Exception ex) { - threadUnexpectedException(); - } + /** + * If there is a consumer waiting in take, tryTransfer returns + * true while successfully transfering object. + */ + public void testTryTransfer4() throws InterruptedException { + final Object hotPotato = new Object(); + final LinkedTransferQueue q = new LinkedTransferQueue(); - } - }).start(); - assertTrue(q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS) != null); - assertTrue(q.isEmpty()); - } catch (Exception ex) { - this.unexpectedException(); - } + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() { + while (! q.hasWaitingConsumer()) + Thread.yield(); + assertTrue(q.hasWaitingConsumer()); + checkEmpty(q); + assertTrue(q.tryTransfer(hotPotato)); + }}); + + assertSame(q.take(), hotPotato); + checkEmpty(q); + awaitTermination(t, MEDIUM_DELAY_MS); } - /* - * tryTransfer waits the amount given if interrupted, show an - * interrupted exception + /** + * tryTransfer blocks interruptibly if no takers */ - public void testTryTransfer4() { + public void testTryTransfer5() throws InterruptedException { final LinkedTransferQueue q = new LinkedTransferQueue(); - Thread toInterrupt = new Thread(new Runnable() { + final CountDownLatch pleaseInterrupt = new CountDownLatch(1); + assertTrue(q.isEmpty()); - public void run() { + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + Thread.currentThread().interrupt(); try { - q.tryTransfer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); - threadShouldThrow(); - } catch (InterruptedException ex) { - } - } - }); - try { - toInterrupt.start(); - Thread.sleep(SMALL_DELAY_MS); - toInterrupt.interrupt(); - } catch (Exception ex) { - this.unexpectedException(); - } + q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + + pleaseInterrupt.countDown(); + try { + q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + }}); + + await(pleaseInterrupt); + assertThreadStaysAlive(t); + t.interrupt(); + awaitTermination(t); + checkEmpty(q); } - /* - * tryTransfer gives up after the timeout and return false + /** + * tryTransfer gives up after the timeout and returns false */ - public void testTryTransfer5() { + public void testTryTransfer6() throws InterruptedException { final LinkedTransferQueue q = new LinkedTransferQueue(); - try { - new Thread(new Runnable() { - public void run() { - try { - threadAssertFalse(q.tryTransfer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); - } catch (InterruptedException ex) { - threadUnexpectedException(); - } - } - }).start(); - Thread.sleep(LONG_DELAY_MS); - assertTrue(q.isEmpty()); - } catch (Exception ex) { - this.unexpectedException(); - } + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + long t0 = System.nanoTime(); + assertFalse(q.tryTransfer(new Object(), + timeoutMillis(), MILLISECONDS)); + assertTrue(millisElapsedSince(t0) >= timeoutMillis()); + checkEmpty(q); + }}); + + awaitTermination(t); + checkEmpty(q); } - /* + /** * tryTransfer waits for any elements previously in to be removed * before transfering to a poll or take */ - public void testTryTransfer6() { + public void testTryTransfer7() throws InterruptedException { final LinkedTransferQueue q = new LinkedTransferQueue(); - q.offer(new Integer(four)); - new Thread(new Runnable() { + assertTrue(q.offer(four)); - public void run() { - try { - threadAssertTrue(q.tryTransfer(new Integer(five), LONG_DELAY_MS, TimeUnit.MILLISECONDS)); - threadAssertTrue(q.isEmpty()); - } catch (InterruptedException ex) { - threadUnexpectedException(); - } - } - }).start(); - try { - Thread.sleep(SHORT_DELAY_MS); - assertEquals(2, q.size()); - assertEquals(new Integer(four), q.poll()); - assertEquals(new Integer(five), q.poll()); - assertTrue(q.isEmpty()); - } catch (Exception ex) { - this.unexpectedException(); - } + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + assertTrue(q.tryTransfer(five, MEDIUM_DELAY_MS, MILLISECONDS)); + checkEmpty(q); + }}); + + while (q.size() != 2) + Thread.yield(); + assertEquals(2, q.size()); + assertSame(four, q.poll()); + assertSame(five, q.poll()); + checkEmpty(q); + awaitTermination(t, MEDIUM_DELAY_MS); } - /* - * tryTransfer attempts to enqueue into the q and fails returning false not - * enqueueing and the successing poll is null + /** + * tryTransfer attempts to enqueue into the queue and fails + * returning false not enqueueing and the successive poll is null */ - public void testTryTransfer7() { + public void testTryTransfer8() throws InterruptedException { final LinkedTransferQueue q = new LinkedTransferQueue(); - q.offer(new Integer(four)); - new Thread(new Runnable() { - - public void run() { - try { - threadAssertFalse(q.tryTransfer(new Integer(five), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); - threadAssertTrue(q.isEmpty()); - } catch (InterruptedException ex) { - threadUnexpectedException(); - } - } - }).start(); - try { - assertEquals(1, q.size()); - assertEquals(new Integer(four), q.poll()); - Thread.sleep(MEDIUM_DELAY_MS); - assertNull(q.poll()); - } catch (Exception ex) { - this.unexpectedException(); - } + assertTrue(q.offer(four)); + assertEquals(1, q.size()); + long t0 = System.nanoTime(); + assertFalse(q.tryTransfer(five, timeoutMillis(), MILLISECONDS)); + assertTrue(millisElapsedSince(t0) >= timeoutMillis()); + assertEquals(1, q.size()); + assertSame(four, q.poll()); + assertNull(q.poll()); + checkEmpty(q); } - private LinkedTransferQueue populatedQueue( - int n) { - LinkedTransferQueue q = new LinkedTransferQueue(); - assertTrue(q.isEmpty()); - int remainingCapacity = q.remainingCapacity(); - for (int i = 0; i < - n; i++) { + private LinkedTransferQueue populatedQueue(int n) { + LinkedTransferQueue q = new LinkedTransferQueue(); + checkEmpty(q); + for (int i = 0; i < n; i++) { + assertEquals(i, q.size()); assertTrue(q.offer(i)); + assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); } - assertFalse(q.isEmpty()); - assertEquals(remainingCapacity, q.remainingCapacity()); - assertEquals(n, q.size()); return q; } - private static class ConsumerObserver { - - private int waitingConsumers; - - private ConsumerObserver() { - } - - private void setWaitingConsumer(int i) { - this.waitingConsumers = i; - } - - private int getWaitingConsumers() { - return waitingConsumers; + /** + * 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)); } } }