--- jsr166/src/test/tck/LinkedTransferQueueTest.java 2009/08/12 02:15:04 1.11 +++ jsr166/src/test/tck/LinkedTransferQueueTest.java 2010/11/18 20:21:53 1.42 @@ -13,23 +13,58 @@ 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); + } } /** @@ -49,8 +84,7 @@ public class LinkedTransferQueueTest ext try { new LinkedTransferQueue(null); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** @@ -62,8 +96,7 @@ public class LinkedTransferQueueTest ext Integer[] ints = new Integer[SIZE]; new LinkedTransferQueue(Arrays.asList(ints)); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** @@ -78,30 +111,35 @@ public class LinkedTransferQueueTest ext } 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); @@ -125,8 +163,7 @@ public class LinkedTransferQueueTest ext LinkedTransferQueue q = new LinkedTransferQueue(); q.offer(null); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** @@ -137,8 +174,7 @@ public class LinkedTransferQueueTest ext LinkedTransferQueue q = new LinkedTransferQueue(); q.add(null); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** @@ -149,8 +185,7 @@ public class LinkedTransferQueueTest ext LinkedTransferQueue q = new LinkedTransferQueue(); q.addAll(null); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** @@ -161,8 +196,7 @@ public class LinkedTransferQueueTest ext LinkedTransferQueue q = populatedQueue(SIZE); q.addAll(q); shouldThrow(); - } catch (IllegalArgumentException success) { - } + } catch (IllegalArgumentException success) {} } /** @@ -174,8 +208,7 @@ public class LinkedTransferQueueTest ext Integer[] ints = new Integer[SIZE]; q.addAll(Arrays.asList(ints)); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** @@ -191,8 +224,7 @@ public class LinkedTransferQueueTest ext } q.addAll(Arrays.asList(ints)); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** @@ -229,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); } /** @@ -246,49 +278,44 @@ public class LinkedTransferQueueTest ext } /** - * take blocks interruptibly when empty - */ - public void testTakeFromEmpty() throws InterruptedException { - final LinkedTransferQueue q = new LinkedTransferQueue(); - Thread t = newStartedThread(new CheckedInterruptedRunnable() { - void realRun() throws InterruptedException { - q.take(); - }}); - 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 = newStartedThread(new CheckedInterruptedRunnable() { - void realRun() throws InterruptedException { - LinkedTransferQueue q = populatedQueue(SIZE); + 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) { - threadAssertEquals(i, (int) q.take()); + assertEquals(i, (int) q.take()); } - q.take(); + aboutToWait.countDown(); + try { + q.take(); + shouldThrow(); + } catch (InterruptedException success) {} }}); - 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); @@ -296,17 +323,23 @@ public class LinkedTransferQueueTest ext 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() throws InterruptedException { LinkedTransferQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, (int) q.poll(SHORT_DELAY_MS, 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); } /** @@ -314,44 +347,60 @@ public class LinkedTransferQueueTest ext * returning timeout status */ public void testInterruptedTimedPoll() throws InterruptedException { - final LinkedTransferQueue q = populatedQueue(SIZE); - Thread t = newStartedThread(new CheckedInterruptedRunnable() { - void realRun() 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) { - threadAssertEquals(i, (int) q.poll(LONG_DELAY_MS, - MILLISECONDS)); + long t0 = System.nanoTime(); + assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); + assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); + } + long t0 = System.nanoTime(); + aboutToWait.countDown(); + try { + q.poll(MEDIUM_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) { + assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); } - q.poll(LONG_DELAY_MS, MILLISECONDS); }}); - Thread.sleep(SMALL_DELAY_MS); + + aboutToWait.await(); + waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); t.interrupt(); - t.join(); - assertEquals(q.size(), 0); - assertNull(q.poll()); + 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 = newStartedThread(new CheckedInterruptedRunnable() { - void realRun() throws InterruptedException { - threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); - q.poll(LONG_DELAY_MS, MILLISECONDS); - q.poll(LONG_DELAY_MS, MILLISECONDS); + 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) {} }}); - Thread.sleep(SMALL_DELAY_MS); - assertTrue(q.offer(zero, SHORT_DELAY_MS, 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()); @@ -360,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()); @@ -374,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()); @@ -389,23 +439,29 @@ 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) { + for (int i = 1; i < SIZE; i+=2) { + assertTrue(q.contains(i)); assertTrue(q.remove(i)); + assertFalse(q.contains(i)); + assertTrue(q.contains(i-1)); } - for (int i = 0; i < SIZE; i += 2) { + for (int i = 0; i < SIZE; i+=2) { + assertTrue(q.contains(i)); assertTrue(q.remove(i)); - assertFalse(q.remove(i + 1)); + assertFalse(q.contains(i)); + assertFalse(q.remove(i+1)); + assertFalse(q.contains(i+1)); } - assertTrue(q.isEmpty()); + checkEmpty(q); } /** @@ -418,7 +474,7 @@ public class LinkedTransferQueueTest ext assertTrue(q.remove(one)); assertTrue(q.remove(two)); assertTrue(q.add(three)); - assertTrue(q.take() == three); + assertSame(q.take(), three); } /** @@ -436,17 +492,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); } /** @@ -500,50 +556,49 @@ 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_BadArg() { + public void testToArray_NullArg() { + LinkedTransferQueue q = populatedQueue(SIZE); try { - LinkedTransferQueue q = populatedQueue(SIZE); - Object o[] = q.toArray(null); + 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) {} } /** @@ -560,7 +615,7 @@ public class LinkedTransferQueueTest ext } /** - * iterator.remove removes current element + * iterator.remove() removes current element */ public void testIteratorRemove() { final LinkedTransferQueue q = new LinkedTransferQueue(); @@ -573,8 +628,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()); } @@ -627,43 +682,48 @@ 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 CheckedRunnable() { - void realRun() { - threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, - MILLISECONDS)); + public void realRun() throws InterruptedException { + threadsStarted.countDown(); + threadsStarted.await(); + assertTrue(q.offer(one, MEDIUM_DELAY_MS, MILLISECONDS)); }}); executor.execute(new CheckedRunnable() { - void realRun() throws InterruptedException { - Thread.sleep(SMALL_DELAY_MS); - threadAssertEquals(one, q.take()); + 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 CheckedRunnable() { - void realRun() throws InterruptedException { - threadAssertNull(q.poll()); - threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, - MILLISECONDS)); - threadAssertTrue(q.isEmpty()); + 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 CheckedRunnable() { - void realRun() throws InterruptedException { - Thread.sleep(SMALL_DELAY_MS); + public void realRun() throws InterruptedException { + threadsStarted.countDown(); + threadsStarted.await(); q.put(one); }}); @@ -689,6 +749,8 @@ public class LinkedTransferQueueTest ext 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()); } @@ -702,8 +764,7 @@ public class LinkedTransferQueueTest ext try { q.drainTo(null); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** @@ -714,8 +775,7 @@ public class LinkedTransferQueueTest ext try { q.drainTo(q); shouldThrow(); - } catch (IllegalArgumentException success) { - } + } catch (IllegalArgumentException success) {} } /** @@ -745,12 +805,12 @@ 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 = newStartedThread(new CheckedRunnable() { - void realRun() { + public void realRun() { q.put(SIZE + 1); }}); ArrayList l = new ArrayList(); @@ -759,7 +819,7 @@ public class LinkedTransferQueueTest ext 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); } @@ -769,10 +829,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) {} } /** @@ -781,14 +840,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(); @@ -810,24 +868,33 @@ public class LinkedTransferQueueTest ext } /** - * poll and take 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(); + assertEquals(q.getWaitingConsumerCount(), 0); + assertFalse(q.hasWaitingConsumer()); + final CountDownLatch threadStarted = new CountDownLatch(1); Thread t = newStartedThread(new CheckedRunnable() { - void realRun() throws InterruptedException { - Thread.sleep(SMALL_DELAY_MS); - threadAssertTrue(q.hasWaitingConsumer()); - waiting.setWaitingConsumer(q.getWaitingConsumerCount()); - threadAssertTrue(q.offer(new Object())); + public void realRun() throws InterruptedException { + threadStarted.countDown(); + assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); + assertEquals(q.getWaitingConsumerCount(), 0); + assertFalse(q.hasWaitingConsumer()); }}); - assertTrue(q.poll(LONG_DELAY_MS, MILLISECONDS) != null); - assertTrue(q.getWaitingConsumerCount() - < waiting.getWaitingConsumers()); - t.join(); + 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); } /** @@ -838,8 +905,7 @@ public class LinkedTransferQueueTest ext LinkedTransferQueue q = new LinkedTransferQueue(); q.transfer(null); shouldThrow(); - } catch (NullPointerException ex) { - } + } catch (NullPointerException success) {} } /** @@ -849,18 +915,21 @@ public class LinkedTransferQueueTest ext public void testTransfer2() throws InterruptedException { final LinkedTransferQueue q = new LinkedTransferQueue(); + final CountDownLatch threadStarted = new CountDownLatch(1); Thread t = newStartedThread(new CheckedRunnable() { - void realRun() throws InterruptedException { - q.transfer(SIZE); - threadAssertTrue(q.isEmpty()); + 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()); - t.join(); + assertSame(five, q.poll()); + checkEmpty(q); + awaitTermination(t, MEDIUM_DELAY_MS); } /** @@ -871,31 +940,29 @@ public class LinkedTransferQueueTest ext = new LinkedTransferQueue(); Thread first = newStartedThread(new CheckedRunnable() { - void realRun() throws InterruptedException { - Integer i = SIZE + 1; - q.transfer(i); - threadAssertTrue(!q.contains(i)); - threadAssertEquals(1, q.size()); + public void realRun() throws InterruptedException { + q.transfer(four); + assertTrue(!q.contains(four)); + assertEquals(1, q.size()); }}); Thread interruptedThread = newStartedThread( new CheckedInterruptedRunnable() { - void realRun() throws InterruptedException { - while (q.size() == 0) + public void realRun() throws InterruptedException { + while (q.isEmpty()) Thread.yield(); - q.transfer(SIZE); + q.transfer(five); }}); 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); } /** @@ -906,16 +973,19 @@ public class LinkedTransferQueueTest ext final LinkedTransferQueue q = new LinkedTransferQueue(); Thread t = newStartedThread(new CheckedRunnable() { - void realRun() throws InterruptedException { + public void realRun() throws InterruptedException { q.transfer(four); - threadAssertFalse(q.contains(four)); - threadAssertEquals(three, q.poll()); + assertFalse(q.contains(four)); + assertSame(three, q.poll()); }}); - Thread.sleep(SHORT_DELAY_MS); + while (q.isEmpty()) + Thread.yield(); + assertFalse(q.isEmpty()); + assertEquals(1, q.size()); assertTrue(q.offer(three)); - assertEquals(four, q.poll()); - t.join(); + assertSame(four, q.poll()); + awaitTermination(t, MEDIUM_DELAY_MS); } /** @@ -927,15 +997,18 @@ public class LinkedTransferQueueTest ext = new LinkedTransferQueue(); Thread t = newStartedThread(new CheckedRunnable() { - void realRun() throws InterruptedException { - q.transfer(SIZE); - threadAssertTrue(q.isEmpty()); + public void realRun() throws InterruptedException { + q.transfer(four); + checkEmpty(q); }}); - Thread.sleep(SHORT_DELAY_MS); - assertEquals(SIZE, (int) q.take()); - assertTrue(q.isEmpty()); - t.join(); + while (q.isEmpty()) + Thread.yield(); + assertFalse(q.isEmpty()); + assertEquals(1, q.size()); + assertSame(four, q.take()); + checkEmpty(q); + awaitTermination(t, MEDIUM_DELAY_MS); } /** @@ -946,20 +1019,18 @@ public class LinkedTransferQueueTest ext final LinkedTransferQueue q = new LinkedTransferQueue(); q.tryTransfer(null); shouldThrow(); - } catch (NullPointerException ex) { - } + } 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); } /** @@ -971,18 +1042,17 @@ public class LinkedTransferQueueTest ext final LinkedTransferQueue q = new LinkedTransferQueue(); Thread t = newStartedThread(new CheckedRunnable() { - void realRun() { + public void realRun() { while (! q.hasWaitingConsumer()) Thread.yield(); - threadAssertTrue(q.hasWaitingConsumer()); - threadAssertTrue(q.isEmpty()); - threadAssertTrue(q.size() == 0); - threadAssertTrue(q.tryTransfer(hotPotato)); + assertTrue(q.hasWaitingConsumer()); + checkEmpty(q); + assertTrue(q.tryTransfer(hotPotato)); }}); - assertTrue(q.poll(MEDIUM_DELAY_MS, MILLISECONDS) == hotPotato); - assertTrue(q.isEmpty()); - t.join(); + assertSame(hotPotato, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); + checkEmpty(q); + awaitTermination(t, MEDIUM_DELAY_MS); } /** @@ -994,53 +1064,66 @@ public class LinkedTransferQueueTest ext final LinkedTransferQueue q = new LinkedTransferQueue(); Thread t = newStartedThread(new CheckedRunnable() { - void realRun() { + public void realRun() { while (! q.hasWaitingConsumer()) Thread.yield(); - threadAssertTrue(q.hasWaitingConsumer()); - threadAssertTrue(q.isEmpty()); - threadAssertTrue(q.size() == 0); - threadAssertTrue(q.tryTransfer(hotPotato)); + assertTrue(q.hasWaitingConsumer()); + checkEmpty(q); + assertTrue(q.tryTransfer(hotPotato)); }}); - assertTrue(q.take() == hotPotato); - assertTrue(q.isEmpty()); - t.join(); + assertSame(q.take(), hotPotato); + checkEmpty(q); + awaitTermination(t, MEDIUM_DELAY_MS); } /** - * tryTransfer waits the amount given if interrupted, and - * throws interrupted exception + * tryTransfer waits the amount given, and throws + * InterruptedException when interrupted. */ public void testTryTransfer5() throws InterruptedException { final LinkedTransferQueue q = new LinkedTransferQueue(); + final CountDownLatch threadStarted = new CountDownLatch(1); + assertTrue(q.isEmpty()); - Thread toInterrupt = newStartedThread(new CheckedInterruptedRunnable() { - void realRun() throws InterruptedException { - q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + long t0 = System.nanoTime(); + threadStarted.countDown(); + try { + q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); + assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); }}); - Thread.sleep(SMALL_DELAY_MS); - toInterrupt.interrupt(); - toInterrupt.join(); + threadStarted.await(); + while (q.isEmpty()) + Thread.yield(); + 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(); Thread t = newStartedThread(new CheckedRunnable() { - void realRun() throws InterruptedException { - threadAssertFalse - (q.tryTransfer(new Object(), - SHORT_DELAY_MS, MILLISECONDS)); + 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); }}); - Thread.sleep(SMALL_DELAY_MS); - assertTrue(q.isEmpty()); - t.join(); + awaitTermination(t, MEDIUM_DELAY_MS); + checkEmpty(q); } /** @@ -1052,38 +1135,40 @@ public class LinkedTransferQueueTest ext assertTrue(q.offer(four)); Thread t = newStartedThread(new CheckedRunnable() { - void realRun() throws InterruptedException { - threadAssertTrue(q.tryTransfer(five, - MEDIUM_DELAY_MS, MILLISECONDS)); - threadAssertTrue(q.isEmpty()); + public void realRun() throws InterruptedException { + assertTrue(q.tryTransfer(five, MEDIUM_DELAY_MS, MILLISECONDS)); + checkEmpty(q); }}); - Thread.sleep(SHORT_DELAY_MS); + while (q.size() != 2) + Thread.yield(); assertEquals(2, q.size()); - assertEquals(four, q.poll()); - assertEquals(five, q.poll()); - assertTrue(q.isEmpty()); - t.join(); + 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 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); 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)); @@ -1092,20 +1177,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; - } - } }