--- jsr166/src/test/tck/LinkedTransferQueueTest.java 2009/08/15 03:20:36 1.14 +++ jsr166/src/test/tck/LinkedTransferQueueTest.java 2010/10/28 22:20:47 1.30 @@ -18,18 +18,26 @@ 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(LinkedTransferQueue q) throws InterruptedException { @@ -44,18 +52,15 @@ public class LinkedTransferQueueTest ext try { q.element(); shouldThrow(); - } catch (NoSuchElementException success) { - } + } catch (NoSuchElementException success) {} try { q.iterator().next(); shouldThrow(); - } catch (NoSuchElementException success) { - } + } catch (NoSuchElementException success) {} try { q.remove(); shouldThrow(); - } catch (NoSuchElementException success) { - } + } catch (NoSuchElementException success) {} } /** @@ -75,8 +80,7 @@ public class LinkedTransferQueueTest ext try { new LinkedTransferQueue(null); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** @@ -88,8 +92,7 @@ public class LinkedTransferQueueTest ext Integer[] ints = new Integer[SIZE]; new LinkedTransferQueue(Arrays.asList(ints)); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** @@ -104,8 +107,7 @@ public class LinkedTransferQueueTest ext } new LinkedTransferQueue(Arrays.asList(ints)); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** @@ -157,8 +159,7 @@ public class LinkedTransferQueueTest ext LinkedTransferQueue q = new LinkedTransferQueue(); q.offer(null); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** @@ -169,8 +170,7 @@ public class LinkedTransferQueueTest ext LinkedTransferQueue q = new LinkedTransferQueue(); q.add(null); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** @@ -181,8 +181,7 @@ public class LinkedTransferQueueTest ext LinkedTransferQueue q = new LinkedTransferQueue(); q.addAll(null); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** @@ -193,8 +192,7 @@ public class LinkedTransferQueueTest ext LinkedTransferQueue q = populatedQueue(SIZE); q.addAll(q); shouldThrow(); - } catch (IllegalArgumentException success) { - } + } catch (IllegalArgumentException success) {} } /** @@ -206,8 +204,7 @@ public class LinkedTransferQueueTest ext Integer[] ints = new Integer[SIZE]; q.addAll(Arrays.asList(ints)); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** @@ -223,8 +220,7 @@ public class LinkedTransferQueueTest ext } q.addAll(Arrays.asList(ints)); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** @@ -278,34 +274,25 @@ 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 { final LinkedTransferQueue q = populatedQueue(SIZE); - Thread t = newStartedThread(new CheckedInterruptedRunnable() { - void realRun() throws InterruptedException { + Thread t = new Thread(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(); + try { + q.take(); + shouldThrow(); + } catch (InterruptedException success) {} }}); - Thread.sleep(SMALL_DELAY_MS); + + t.start(); + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); - t.join(); + awaitTermination(t, MEDIUM_DELAY_MS); checkEmpty(q); } @@ -322,7 +309,7 @@ public class LinkedTransferQueueTest ext } /** - * 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); @@ -334,15 +321,14 @@ public class LinkedTransferQueueTest ext } /** - * 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) { long t0 = System.nanoTime(); assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); - long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024); - assertTrue(millisElapsed < SMALL_DELAY_MS); + assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); } assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); checkEmpty(q); @@ -354,39 +340,23 @@ public class LinkedTransferQueueTest ext */ public void testInterruptedTimedPoll() throws InterruptedException { final LinkedTransferQueue q = populatedQueue(SIZE); - Thread t = newStartedThread(new CheckedInterruptedRunnable() { - void realRun() throws InterruptedException { + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { for (int i = 0; i < SIZE; ++i) { long t0 = System.nanoTime(); - threadAssertEquals(i, (int) q.poll(LONG_DELAY_MS, - MILLISECONDS)); - long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024); - assertTrue(millisElapsed < SMALL_DELAY_MS); + assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); + assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); } - q.poll(LONG_DELAY_MS, MILLISECONDS); + try { + q.poll(LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} }}); - Thread.sleep(SMALL_DELAY_MS); - t.interrupt(); - t.join(); - checkEmpty(q); - } - /** - * timed poll before a delayed offer fails; after offer succeeds; - * on interruption throws - */ - 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); - }}); Thread.sleep(SMALL_DELAY_MS); - assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); t.interrupt(); - t.join(); + awaitTermination(t, MEDIUM_DELAY_MS); + checkEmpty(q); } /** @@ -416,8 +386,7 @@ public class LinkedTransferQueueTest ext try { q.element(); shouldThrow(); - } catch (NoSuchElementException success) { - } + } catch (NoSuchElementException success) {} checkEmpty(q); } @@ -432,8 +401,7 @@ public class LinkedTransferQueueTest ext try { q.remove(); shouldThrow(); - } catch (NoSuchElementException success) { - } + } catch (NoSuchElementException success) {} checkEmpty(q); } @@ -462,7 +430,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); } /** @@ -570,24 +538,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(incompatible array type) throws CCE */ public void testToArray1_BadArg() { + LinkedTransferQueue q = populatedQueue(SIZE); try { - LinkedTransferQueue q = populatedQueue(SIZE); Object o[] = q.toArray(new String[10]); shouldThrow(); - } catch (ArrayStoreException success) { - } + } catch (ArrayStoreException success) {} } /** @@ -617,8 +583,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()); } @@ -676,15 +642,14 @@ public class LinkedTransferQueueTest ext ExecutorService executor = Executors.newFixedThreadPool(2); executor.execute(new CheckedRunnable() { - void realRun() { - threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, - MILLISECONDS)); + public void realRun() { + assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); }}); executor.execute(new CheckedRunnable() { - void realRun() throws InterruptedException { + public void realRun() throws InterruptedException { Thread.sleep(SMALL_DELAY_MS); - threadAssertEquals(one, q.take()); + assertSame(one, q.take()); }}); joinPool(executor); @@ -698,15 +663,14 @@ public class LinkedTransferQueueTest ext 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()); + assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); + assertTrue(q.isEmpty()); }}); executor.execute(new CheckedRunnable() { - void realRun() throws InterruptedException { + public void realRun() throws InterruptedException { Thread.sleep(SMALL_DELAY_MS); q.put(one); }}); @@ -746,8 +710,7 @@ public class LinkedTransferQueueTest ext try { q.drainTo(null); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** @@ -758,8 +721,7 @@ public class LinkedTransferQueueTest ext try { q.drainTo(q); shouldThrow(); - } catch (IllegalArgumentException success) { - } + } catch (IllegalArgumentException success) {} } /** @@ -794,7 +756,7 @@ public class LinkedTransferQueueTest ext 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(); @@ -803,7 +765,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); } @@ -815,8 +777,7 @@ public class LinkedTransferQueueTest ext try { q.drainTo(null, SIZE); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** @@ -827,12 +788,11 @@ public class LinkedTransferQueueTest ext try { 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(); @@ -863,19 +823,19 @@ public class LinkedTransferQueueTest ext assertFalse(q.hasWaitingConsumer()); Thread t = newStartedThread(new CheckedRunnable() { - void realRun() throws InterruptedException { + public void realRun() throws InterruptedException { Thread.sleep(SMALL_DELAY_MS); - threadAssertTrue(q.hasWaitingConsumer()); - threadAssertEquals(q.getWaitingConsumerCount(), 1); - threadAssertTrue(q.offer(new Object())); - threadAssertFalse(q.hasWaitingConsumer()); - threadAssertEquals(q.getWaitingConsumerCount(), 0); + assertTrue(q.hasWaitingConsumer()); + assertEquals(q.getWaitingConsumerCount(), 1); + assertTrue(q.offer(one)); + assertFalse(q.hasWaitingConsumer()); + assertEquals(q.getWaitingConsumerCount(), 0); }}); - assertTrue(q.poll(LONG_DELAY_MS, MILLISECONDS) != null); + assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); assertEquals(q.getWaitingConsumerCount(), 0); assertFalse(q.hasWaitingConsumer()); - t.join(); + awaitTermination(t, MEDIUM_DELAY_MS); } /** @@ -886,8 +846,7 @@ public class LinkedTransferQueueTest ext LinkedTransferQueue q = new LinkedTransferQueue(); q.transfer(null); shouldThrow(); - } catch (NullPointerException ex) { - } + } catch (NullPointerException success) {} } /** @@ -899,16 +858,16 @@ public class LinkedTransferQueueTest ext = new LinkedTransferQueue(); Thread t = newStartedThread(new CheckedRunnable() { - void realRun() throws InterruptedException { + public void realRun() throws InterruptedException { q.transfer(SIZE); - threadAssertTrue(q.isEmpty()); + assertTrue(q.isEmpty()); }}); Thread.sleep(SHORT_DELAY_MS); assertEquals(1, q.size()); assertEquals(SIZE, (int) q.poll()); assertTrue(q.isEmpty()); - t.join(); + awaitTermination(t, MEDIUM_DELAY_MS); } /** @@ -919,16 +878,16 @@ public class LinkedTransferQueueTest ext = new LinkedTransferQueue(); Thread first = newStartedThread(new CheckedRunnable() { - void realRun() throws InterruptedException { + public void realRun() throws InterruptedException { Integer i = SIZE + 1; q.transfer(i); - threadAssertTrue(!q.contains(i)); - threadAssertEquals(1, q.size()); + assertTrue(!q.contains(i)); + assertEquals(1, q.size()); }}); Thread interruptedThread = newStartedThread( new CheckedInterruptedRunnable() { - void realRun() throws InterruptedException { + public void realRun() throws InterruptedException { while (q.size() == 0) Thread.yield(); q.transfer(SIZE); @@ -954,16 +913,16 @@ 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); assertTrue(q.offer(three)); - assertEquals(four, q.poll()); - t.join(); + assertSame(four, q.poll()); + awaitTermination(t, MEDIUM_DELAY_MS); } /** @@ -975,7 +934,7 @@ public class LinkedTransferQueueTest ext = new LinkedTransferQueue(); Thread t = newStartedThread(new CheckedRunnable() { - void realRun() throws InterruptedException { + public void realRun() throws InterruptedException { q.transfer(SIZE); checkEmpty(q); }}); @@ -983,7 +942,7 @@ public class LinkedTransferQueueTest ext Thread.sleep(SHORT_DELAY_MS); assertEquals(SIZE, (int) q.take()); checkEmpty(q); - t.join(); + awaitTermination(t, MEDIUM_DELAY_MS); } /** @@ -994,8 +953,7 @@ public class LinkedTransferQueueTest ext final LinkedTransferQueue q = new LinkedTransferQueue(); q.tryTransfer(null); shouldThrow(); - } catch (NullPointerException ex) { - } + } catch (NullPointerException success) {} } /** @@ -1018,18 +976,18 @@ 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()); + assertTrue(q.isEmpty()); + assertEquals(q.size(), 0); + assertTrue(q.tryTransfer(hotPotato)); }}); - assertTrue(q.poll(MEDIUM_DELAY_MS, MILLISECONDS) == hotPotato); + assertSame(hotPotato, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); checkEmpty(q); - t.join(); + awaitTermination(t, MEDIUM_DELAY_MS); } /** @@ -1041,18 +999,18 @@ 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()); + assertTrue(q.isEmpty()); + assertEquals(q.size(), 0); + assertTrue(q.tryTransfer(hotPotato)); }}); - assertTrue(q.take() == hotPotato); + assertSame(q.take(), hotPotato); checkEmpty(q); - t.join(); + awaitTermination(t, MEDIUM_DELAY_MS); } /** @@ -1063,7 +1021,7 @@ public class LinkedTransferQueueTest ext final LinkedTransferQueue q = new LinkedTransferQueue(); Thread toInterrupt = newStartedThread(new CheckedInterruptedRunnable() { - void realRun() throws InterruptedException { + public void realRun() throws InterruptedException { q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); }}); @@ -1079,15 +1037,16 @@ public class LinkedTransferQueueTest ext 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); }}); - Thread.sleep(SMALL_DELAY_MS); checkEmpty(q); - t.join(); + awaitTermination(t, MEDIUM_DELAY_MS); + checkEmpty(q); } /** @@ -1099,18 +1058,17 @@ 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)); + assertTrue(q.isEmpty()); }}); Thread.sleep(SHORT_DELAY_MS); assertEquals(2, q.size()); - assertEquals(four, q.poll()); - assertEquals(five, q.poll()); + assertSame(four, q.poll()); + assertSame(five, q.poll()); checkEmpty(q); - t.join(); + awaitTermination(t, MEDIUM_DELAY_MS); } /** @@ -1123,7 +1081,7 @@ public class LinkedTransferQueueTest ext assertEquals(1, q.size()); assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS)); assertEquals(1, q.size()); - assertEquals(four, q.poll()); + assertSame(four, q.poll()); assertNull(q.poll()); checkEmpty(q); }