--- jsr166/src/test/tck/LinkedTransferQueueTest.java 2009/08/02 08:17:31 1.5 +++ jsr166/src/test/tck/LinkedTransferQueueTest.java 2010/11/03 07:54:52 1.36 @@ -13,25 +13,63 @@ import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.ArrayList; import java.util.Arrays; -import java.util.ConcurrentModificationException; 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 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()); } public static Test suite() { - return new TestSuite(LinkedTransferQueueTest.class); + 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); + } } /** - * 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()); @@ -39,26 +77,26 @@ public class LinkedTransferQueueTest ext } /** - * Initializing constructor with null collection throws NullPointerException + * 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 NullPointerException + * Initializing from Collection of null elements throws + * NullPointerException */ public void testConstructor3() { try { Integer[] ints = new Integer[SIZE]; - LinkedTransferQueue q = new LinkedTransferQueue(Arrays.asList(ints)); + new LinkedTransferQueue(Arrays.asList(ints)); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** @@ -71,31 +109,37 @@ public class LinkedTransferQueueTest ext for (int i = 0; i < SIZE - 1; ++i) { ints[i] = i; } - LinkedTransferQueue q = new LinkedTransferQueue(Arrays.asList(ints)); + new LinkedTransferQueue(Arrays.asList(ints)); 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] = 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); @@ -119,8 +163,7 @@ public class LinkedTransferQueueTest ext LinkedTransferQueue q = new LinkedTransferQueue(); q.offer(null); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** @@ -131,8 +174,7 @@ public class LinkedTransferQueueTest ext LinkedTransferQueue q = new LinkedTransferQueue(); q.add(null); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** @@ -143,8 +185,7 @@ public class LinkedTransferQueueTest ext LinkedTransferQueue q = new LinkedTransferQueue(); q.addAll(null); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** @@ -155,8 +196,7 @@ public class LinkedTransferQueueTest ext LinkedTransferQueue q = populatedQueue(SIZE); q.addAll(q); shouldThrow(); - } catch (IllegalArgumentException success) { - } + } catch (IllegalArgumentException success) {} } /** @@ -168,13 +208,12 @@ public class LinkedTransferQueueTest ext Integer[] ints = new Integer[SIZE]; q.addAll(Arrays.asList(ints)); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** - * addAll of a collection with any null elements throws NullPointerException after - * possibly adding some elements + * addAll of a collection with any null elements throws + * NullPointerException after possibly adding some elements */ public void testAddAll3() { try { @@ -185,8 +224,7 @@ public class LinkedTransferQueueTest ext } q.addAll(Arrays.asList(ints)); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** @@ -223,10 +261,10 @@ public class LinkedTransferQueueTest ext public void testPut() { LinkedTransferQueue q = new LinkedTransferQueue(); for (int i = 0; i < SIZE; ++i) { + assertEquals(q.size(), i); q.put(i); assertTrue(q.contains(i)); } - assertEquals(q.size(), SIZE); } /** @@ -240,81 +278,68 @@ public class LinkedTransferQueueTest ext } /** - * take blocks interruptibly when empty - */ - public void testTakeFromEmpty() throws InterruptedException { - final LinkedTransferQueue q = new LinkedTransferQueue(); - Thread t = new Thread(new Runnable() { - public void run() { - try { - q.take(); - threadShouldThrow(); - } catch (InterruptedException success) { - } catch (Throwable ex) { - threadUnexpectedException(ex); - } - }}); - t.start(); - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); - } - - /** - * Take removes existing elements until empty, then blocks interruptibly + * take removes existing elements until empty, then blocks interruptibly */ public void testBlockingTake() throws InterruptedException { - Thread t = new Thread(new Runnable() { - public void run() { + 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) { + assertEquals(i, (int) q.take()); + } + aboutToWait.countDown(); try { - LinkedTransferQueue q = populatedQueue(SIZE); - for (int i = 0; i < SIZE; ++i) { - threadAssertEquals(i, (int) q.take()); - } q.take(); - threadShouldThrow(); - } catch (InterruptedException success) { - } catch (Throwable ex) { - threadUnexpectedException(ex); - } + shouldThrow(); + } catch (InterruptedException success) {} }}); - t.start(); - Thread.sleep(SHORT_DELAY_MS); + + aboutToWait.await(); + waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); t.interrupt(); - t.join(); + awaitTermination(t, MEDIUM_DELAY_MS); + checkEmpty(q); } /** * poll succeeds unless empty */ - public void testPoll() { + public void testPoll() throws InterruptedException { LinkedTransferQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { 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() throws InterruptedException { LinkedTransferQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, (int) q.poll(0, TimeUnit.MILLISECONDS)); + assertEquals(i, (int) q.poll(0, MILLISECONDS)); } - assertNull(q.poll(0, TimeUnit.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() throws InterruptedException { LinkedTransferQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, (int) q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); - } - assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); + 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); + checkEmpty(q); } /** @@ -322,54 +347,60 @@ public class LinkedTransferQueueTest ext * returning timeout status */ public void testInterruptedTimedPoll() throws InterruptedException { - Thread t = new Thread(new Runnable() { - public void run() { + 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, (int) q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); - } - threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); + q.poll(MEDIUM_DELAY_MS, MILLISECONDS); + shouldThrow(); } catch (InterruptedException success) { - } catch (Throwable ex) { - threadUnexpectedException(ex); + assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); } }}); - t.start(); - Thread.sleep(SHORT_DELAY_MS); + + aboutToWait.await(); + waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); t.interrupt(); - t.join(); + awaitTermination(t, MEDIUM_DELAY_MS); + checkEmpty(q); } /** - * timed poll before a delayed offer fails; after offer succeeds; - * on interruption throws + * timed poll after thread interrupted throws InterruptedException + * instead of returning timeout status */ - public void testTimedPollWithOffer() throws InterruptedException { - 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) { - } catch (Throwable ex) { - threadUnexpectedException(ex); + 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 { + q.poll(MEDIUM_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} }}); - t.start(); - Thread.sleep(SMALL_DELAY_MS); - assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); - t.interrupt(); - t.join(); + + awaitTermination(t, MEDIUM_DELAY_MS); + checkEmpty(q); } /** * peek returns next element, or null if empty */ - public void testPeek() { + public void testPeek() throws InterruptedException { LinkedTransferQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { assertEquals(i, (int) q.peek()); @@ -378,12 +409,13 @@ public class LinkedTransferQueueTest ext i != (int) q.peek()); } assertNull(q.peek()); + checkEmpty(q); } /** * element returns next element, or throws NoSuchElementException if empty */ - public void testElement() { + public void testElement() throws InterruptedException { LinkedTransferQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { assertEquals(i, (int) q.element()); @@ -392,14 +424,14 @@ public class LinkedTransferQueueTest ext try { q.element(); shouldThrow(); - } catch (NoSuchElementException success) { - } + } catch (NoSuchElementException success) {} + checkEmpty(q); } /** * remove removes next element, or throws NoSuchElementException if empty */ - public void testRemove() { + public void testRemove() throws InterruptedException { LinkedTransferQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { assertEquals(i, (int) q.remove()); @@ -407,14 +439,14 @@ public class LinkedTransferQueueTest ext try { q.remove(); shouldThrow(); - } catch (NoSuchElementException success) { - } + } catch (NoSuchElementException success) {} + checkEmpty(q); } /** * remove(x) removes x and returns true if present */ - public void testRemoveElement() { + public void testRemoveElement() throws InterruptedException { LinkedTransferQueue q = populatedQueue(SIZE); for (int i = 1; i < SIZE; i += 2) { assertTrue(q.remove(i)); @@ -423,7 +455,7 @@ public class LinkedTransferQueueTest ext assertTrue(q.remove(i)); assertFalse(q.remove(i + 1)); } - assertTrue(q.isEmpty()); + checkEmpty(q); } /** @@ -436,7 +468,7 @@ public class LinkedTransferQueueTest ext assertTrue(q.remove(one)); assertTrue(q.remove(two)); assertTrue(q.add(three)); - assertTrue(q.take() != null); + assertSame(q.take(), three); } /** @@ -454,17 +486,17 @@ public class LinkedTransferQueueTest ext /** * clear removes all elements */ - public void testClear() { + public void testClear() throws InterruptedException { LinkedTransferQueue q = populatedQueue(SIZE); q.clear(); - assertTrue(q.isEmpty()); - assertEquals(0, q.size()); + 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); } /** @@ -482,7 +514,8 @@ public class LinkedTransferQueueTest ext } /** - * 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); @@ -501,7 +534,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) { @@ -516,7 +550,7 @@ public class LinkedTransferQueueTest ext } /** - * toArray contains all elements + * toArray() contains all elements */ public void testToArray() throws InterruptedException { LinkedTransferQueue q = populatedQueue(SIZE); @@ -542,24 +576,22 @@ public class LinkedTransferQueueTest ext * toArray(null) throws NullPointerException */ public void testToArray_BadArg() { + LinkedTransferQueue q = populatedQueue(SIZE); try { - LinkedTransferQueue q = populatedQueue(SIZE); Object o[] = q.toArray(null); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** - * 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) {} } /** @@ -568,13 +600,15 @@ 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(), q.take()); + assertEquals(it.next(), i++); } + assertEquals(i, SIZE); } /** - * iterator.remove removes current element + * iterator.remove() removes current element */ public void testIteratorRemove() { final LinkedTransferQueue q = new LinkedTransferQueue(); @@ -587,8 +621,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()); } @@ -596,7 +630,8 @@ 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); @@ -640,56 +675,49 @@ public class LinkedTransferQueueTest ext */ public void testOfferInExecutor() { final LinkedTransferQueue q = new LinkedTransferQueue(); - q.add(one); - q.add(two); + final CountDownLatch threadsStarted = new CountDownLatch(2); ExecutorService executor = Executors.newFixedThreadPool(2); - executor.execute(new Runnable() { - public void run() { - try { - threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); - } catch (Throwable ex) { - threadUnexpectedException(ex); - } + + executor.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { + threadsStarted.countDown(); + threadsStarted.await(); + assertTrue(q.offer(one, MEDIUM_DELAY_MS, MILLISECONDS)); }}); - executor.execute(new Runnable() { - public void run() { - try { - Thread.sleep(SMALL_DELAY_MS); - threadAssertEquals(one, q.take()); - } catch (Throwable ex) { - threadUnexpectedException(ex); - } + executor.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { + threadsStarted.countDown(); + 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 CountDownLatch threadsStarted = new CountDownLatch(2); ExecutorService executor = Executors.newFixedThreadPool(2); - executor.execute(new Runnable() { - public void run() { - try { - threadAssertNull(q.poll()); - threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); - threadAssertTrue(q.isEmpty()); - } catch (Throwable ex) { - threadUnexpectedException(ex); - } + + 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); }}); - executor.execute(new Runnable() { - public void run() { - try { - Thread.sleep(SMALL_DELAY_MS); - q.put(one); - } catch (Throwable ex) { - threadUnexpectedException(ex); - } + executor.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { + threadsStarted.countDown(); + threadsStarted.await(); + q.put(one); }}); joinPool(executor); @@ -702,15 +730,20 @@ public class LinkedTransferQueueTest ext LinkedTransferQueue q = populatedQueue(SIZE); ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); - ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); + 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)); + 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()); } @@ -724,8 +757,7 @@ public class LinkedTransferQueueTest ext try { q.drainTo(null); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** @@ -736,8 +768,7 @@ public class LinkedTransferQueueTest ext try { q.drainTo(q); shouldThrow(); - } catch (IllegalArgumentException success) { - } + } catch (IllegalArgumentException success) {} } /** @@ -767,26 +798,21 @@ public class LinkedTransferQueueTest ext } /** - * drainTo empties full queue, unblocking a waiting put. + * drainTo(c) empties full queue, unblocking a waiting put. */ public void testDrainToWithActivePut() throws InterruptedException { final LinkedTransferQueue q = populatedQueue(SIZE); - Thread t = new Thread(new Runnable() { - public void run() { - try { - q.put(SIZE + 1); - } catch (Throwable ex) { - threadUnexpectedException(ex); - } + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() { + q.put(SIZE + 1); }}); - t.start(); ArrayList l = new ArrayList(); q.drainTo(l); assertTrue(l.size() >= SIZE); for (int i = 0; i < SIZE; ++i) { assertEquals(l.get(i), i); } - t.join(); + awaitTermination(t, MEDIUM_DELAY_MS); assertTrue(q.size() + l.size() >= SIZE); } @@ -796,10 +822,9 @@ public class LinkedTransferQueueTest ext public void testDrainToNullN() { LinkedTransferQueue q = populatedQueue(SIZE); try { - q.drainTo(null, 0); + q.drainTo(null, SIZE); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** @@ -808,14 +833,13 @@ public class LinkedTransferQueueTest ext public void testDrainToSelfN() { LinkedTransferQueue q = populatedQueue(SIZE); try { - q.drainTo(q, 0); + q.drainTo(q, SIZE); shouldThrow(); - } catch (IllegalArgumentException success) { - } + } catch (IllegalArgumentException success) {} } /** - * 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(); @@ -837,23 +861,33 @@ public class LinkedTransferQueueTest ext } /** - * 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() throws InterruptedException { final LinkedTransferQueue q = new LinkedTransferQueue(); - final ConsumerObserver waiting = new ConsumerObserver(); - new Thread(new Runnable() { - public void run() { - try { - threadAssertTrue(q.hasWaitingConsumer()); - waiting.setWaitingConsumer(q.getWaitingConsumerCount()); - threadAssertTrue(q.offer(new Object())); - } catch (Throwable ex) { - threadUnexpectedException(ex); - } - }}).start(); - assertTrue(q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS) != null); - assertTrue(q.getWaitingConsumerCount() < waiting.getWaitingConsumers()); + assertEquals(q.getWaitingConsumerCount(), 0); + assertFalse(q.hasWaitingConsumer()); + final CountDownLatch threadStarted = new CountDownLatch(1); + + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + threadStarted.countDown(); + assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); + assertEquals(q.getWaitingConsumerCount(), 0); + assertFalse(q.hasWaitingConsumer()); + }}); + + threadStarted.await(); + waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); + assertEquals(q.getWaitingConsumerCount(), 1); + assertTrue(q.hasWaitingConsumer()); + + assertTrue(q.offer(one)); + assertEquals(q.getWaitingConsumerCount(), 0); + assertFalse(q.hasWaitingConsumer()); + + awaitTermination(t, MEDIUM_DELAY_MS); } /** @@ -864,101 +898,110 @@ public class LinkedTransferQueueTest ext LinkedTransferQueue q = new LinkedTransferQueue(); q.transfer(null); shouldThrow(); - } catch (NullPointerException ex) { - } + } 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() throws InterruptedException { - final LinkedTransferQueue q = new LinkedTransferQueue(); - - new Thread(new Runnable() { - public void run() { - try { - q.transfer(SIZE); - threadAssertTrue(q.isEmpty()); - } catch (Throwable ex) { - threadUnexpectedException(ex); - } - }}).start(); + final LinkedTransferQueue q + = new LinkedTransferQueue(); + final CountDownLatch threadStarted = new CountDownLatch(1); + + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + threadStarted.countDown(); + q.transfer(five); + checkEmpty(q); + }}); - Thread.sleep(SHORT_DELAY_MS); + threadStarted.await(); + waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); assertEquals(1, q.size()); - assertEquals(SIZE, (int) q.poll()); - assertTrue(q.isEmpty()); + assertSame(five, q.poll()); + checkEmpty(q); + awaitTermination(t, MEDIUM_DELAY_MS); } /** - * transfer will attempt to transfer in fifo order and continue - * waiting if the element being transfered is not polled or taken + * 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 = - new Thread(new Runnable() { - public void run() { - try { - Integer i = SIZE + 1; - q.transfer(i); - threadAssertTrue(!q.contains(i)); - threadAssertEquals(1, q.size()); - } catch (Throwable ex) { - threadUnexpectedException(ex); - } - }}); - first.start(); + Thread first = newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + q.transfer(four); + assertTrue(!q.contains(four)); + assertEquals(1, q.size()); + }}); - Thread interruptedThread = - new Thread(new Runnable() { - public void run() { - try { - while (q.size() == 0) - Thread.yield(); - q.transfer(SIZE); - threadShouldThrow(); - } catch (InterruptedException success) { - } catch (Throwable ex) { - threadUnexpectedException(ex); - } + Thread interruptedThread = newStartedThread( + new CheckedInterruptedRunnable() { + public void realRun() throws InterruptedException { + while (q.isEmpty()) + Thread.yield(); + q.transfer(five); }}); - interruptedThread.start(); while (q.size() < 2) Thread.yield(); assertEquals(2, q.size()); - assertEquals(SIZE + 1, (int) q.poll()); + assertSame(four, q.poll()); first.join(); assertEquals(1, q.size()); interruptedThread.interrupt(); interruptedThread.join(); - assertEquals(0, q.size()); - assertTrue(q.isEmpty()); + 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() throws InterruptedException { final LinkedTransferQueue q = new LinkedTransferQueue(); - new Thread(new Runnable() { - public void run() { - try { - q.transfer(four); - threadAssertFalse(q.contains(four)); - threadAssertEquals(three, q.poll()); - } catch (Throwable ex) { - threadUnexpectedException(ex); - } - }}).start(); - Thread.sleep(MEDIUM_DELAY_MS); + + 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)); - assertEquals(four, q.poll()); + assertSame(four, q.poll()); + awaitTermination(t, MEDIUM_DELAY_MS); + } + + /** + * 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); } /** @@ -968,21 +1011,19 @@ public class LinkedTransferQueueTest ext try { final LinkedTransferQueue q = new LinkedTransferQueue(); q.tryTransfer(null); - this.shouldThrow(); - } catch (NullPointerException ex) { - } + shouldThrow(); + } catch (NullPointerException success) {} } /** * tryTransfer returns false and does not enqueue if there are no * consumers waiting to poll or take. */ - public void testTryTransfer2() { + public void testTryTransfer2() throws InterruptedException { final LinkedTransferQueue q = new LinkedTransferQueue(); assertFalse(q.tryTransfer(new Object())); assertFalse(q.hasWaitingConsumer()); - assertTrue(q.isEmpty()); - assertEquals(0, q.size()); + checkEmpty(q); } /** @@ -992,21 +1033,19 @@ public class LinkedTransferQueueTest ext public void testTryTransfer3() throws InterruptedException { final Object hotPotato = new Object(); final LinkedTransferQueue q = new LinkedTransferQueue(); - new Thread(new Runnable() { - public void run() { - try { - while (! q.hasWaitingConsumer()) - Thread.yield(); - threadAssertTrue(q.hasWaitingConsumer()); - threadAssertTrue(q.isEmpty()); - threadAssertTrue(q.size() == 0); - threadAssertTrue(q.tryTransfer(hotPotato)); - } catch (Throwable ex) { - threadUnexpectedException(ex); - } - }}).start(); - assertTrue(q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS) == hotPotato); - assertTrue(q.isEmpty()); + + 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); } /** @@ -1016,59 +1055,64 @@ public class LinkedTransferQueueTest ext public void testTryTransfer4() throws InterruptedException { final Object hotPotato = new Object(); final LinkedTransferQueue q = new LinkedTransferQueue(); - new Thread(new Runnable() { - public void run() { - try { - while (! q.hasWaitingConsumer()) - Thread.yield(); - threadAssertTrue(q.hasWaitingConsumer()); - threadAssertTrue(q.isEmpty()); - threadAssertTrue(q.size() == 0); - threadAssertTrue(q.tryTransfer(hotPotato)); - } catch (Throwable ex) { - threadUnexpectedException(ex); - } - }}).start(); - assertTrue(q.take() == hotPotato); - assertTrue(q.isEmpty()); + + 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 waits the amount given if interrupted, and + * throws interrupted exception */ public void testTryTransfer5() throws InterruptedException { final LinkedTransferQueue q = new LinkedTransferQueue(); - Thread toInterrupt = new Thread(new Runnable() { - public void run() { + final CountDownLatch threadStarted = new CountDownLatch(1); + + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + long t0 = System.nanoTime(); + threadStarted.countDown(); try { - q.tryTransfer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); - threadShouldThrow(); - } catch (InterruptedException success) { - } catch (Throwable ex) { - threadUnexpectedException(ex); - } + q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); }}); - toInterrupt.start(); - Thread.sleep(SMALL_DELAY_MS); - toInterrupt.interrupt(); + + threadStarted.await(); + Thread.sleep(SHORT_DELAY_MS); + t.interrupt(); + awaitTermination(t, MEDIUM_DELAY_MS); + checkEmpty(q); } /** - * tryTransfer gives up after the timeout and return false + * tryTransfer gives up after the timeout and returns false */ public void testTryTransfer6() throws InterruptedException { final LinkedTransferQueue q = new LinkedTransferQueue(); - new Thread(new Runnable() { - public void run() { - try { - threadAssertFalse(q.tryTransfer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); - } catch (Throwable ex) { - threadUnexpectedException(ex); - } - }}).start(); - Thread.sleep(LONG_DELAY_MS); - assertTrue(q.isEmpty()); + + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + long t0 = System.nanoTime(); + assertFalse(q.tryTransfer(new Object(), + SHORT_DELAY_MS, MILLISECONDS)); + assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); + checkEmpty(q); + }}); + + awaitTermination(t, MEDIUM_DELAY_MS); + checkEmpty(q); } /** @@ -1078,20 +1122,20 @@ public class LinkedTransferQueueTest ext public void testTryTransfer7() throws InterruptedException { final LinkedTransferQueue q = new LinkedTransferQueue(); assertTrue(q.offer(four)); - new Thread(new Runnable() { - public void run() { - try { - threadAssertTrue(q.tryTransfer(five, LONG_DELAY_MS, TimeUnit.MILLISECONDS)); - threadAssertTrue(q.isEmpty()); - } catch (Throwable ex) { - threadUnexpectedException(ex); - } - }}).start(); - Thread.sleep(SHORT_DELAY_MS); + + 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()); - assertEquals(four, q.poll()); - assertEquals(five, q.poll()); - assertTrue(q.isEmpty()); + assertSame(four, q.poll()); + assertSame(five, q.poll()); + checkEmpty(q); + awaitTermination(t, MEDIUM_DELAY_MS); } /** @@ -1102,16 +1146,18 @@ public class LinkedTransferQueueTest ext final LinkedTransferQueue q = new LinkedTransferQueue(); assertTrue(q.offer(four)); assertEquals(1, q.size()); - assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); + long t0 = System.nanoTime(); + assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS)); + assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); assertEquals(1, q.size()); - assertEquals(four, q.poll()); - threadAssertTrue(q.isEmpty()); + assertSame(four, q.poll()); assertNull(q.poll()); + checkEmpty(q); } private LinkedTransferQueue populatedQueue(int n) { LinkedTransferQueue q = new LinkedTransferQueue(); - assertTrue(q.isEmpty()); + checkEmpty(q); for (int i = 0; i < n; i++) { assertEquals(i, q.size()); assertTrue(q.offer(i)); @@ -1120,20 +1166,4 @@ public class LinkedTransferQueueTest ext assertFalse(q.isEmpty()); return q; } - - private static class ConsumerObserver { - - private int waitingConsumers; - - private ConsumerObserver() { - } - - private void setWaitingConsumer(int i) { - this.waitingConsumers = i; - } - - private int getWaitingConsumers() { - return waitingConsumers; - } - } }