--- jsr166/src/test/tck/LinkedTransferQueueTest.java 2009/12/01 06:28:43 1.23 +++ jsr166/src/test/tck/LinkedTransferQueueTest.java 2010/10/28 19:05:04 1.29 @@ -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 { @@ -266,21 +274,7 @@ public class LinkedTransferQueueTest ext } /** - * take blocks interruptibly when empty - */ - public void testTakeFromEmpty() throws InterruptedException { - final LinkedTransferQueue q = new LinkedTransferQueue(); - Thread t = newStartedThread(new CheckedInterruptedRunnable() { - public 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); @@ -298,7 +292,7 @@ public class LinkedTransferQueueTest ext t.start(); Thread.sleep(SHORT_DELAY_MS); t.interrupt(); - t.join(); + awaitTermination(t, MEDIUM_DELAY_MS); checkEmpty(q); } @@ -334,8 +328,7 @@ public class LinkedTransferQueueTest ext 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); @@ -352,8 +345,7 @@ public class LinkedTransferQueueTest ext 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); } try { q.poll(LONG_DELAY_MS, MILLISECONDS); @@ -363,34 +355,11 @@ public class LinkedTransferQueueTest ext Thread.sleep(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 - */ - public void testTimedPollWithOffer() throws InterruptedException { - final LinkedTransferQueue q = new LinkedTransferQueue(); - Thread t = new Thread(new CheckedRunnable() { - public void realRun() throws InterruptedException { - assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); - assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); - try { - q.poll(LONG_DELAY_MS, MILLISECONDS); - shouldThrow(); - } catch (InterruptedException success) {} - }}); - - t.start(); - Thread.sleep(SMALL_DELAY_MS); - assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); - t.interrupt(); - t.join(); - } - - /** * peek returns next element, or null if empty */ public void testPeek() throws InterruptedException { @@ -614,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()); } @@ -680,7 +649,7 @@ public class LinkedTransferQueueTest ext executor.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { Thread.sleep(SMALL_DELAY_MS); - assertEquals(one, q.take()); + assertSame(one, q.take()); }}); joinPool(executor); @@ -796,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); } @@ -823,7 +792,7 @@ public class LinkedTransferQueueTest ext } /** - * 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(); @@ -866,7 +835,7 @@ public class LinkedTransferQueueTest ext assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); assertEquals(q.getWaitingConsumerCount(), 0); assertFalse(q.hasWaitingConsumer()); - t.join(); + awaitTermination(t, MEDIUM_DELAY_MS); } /** @@ -891,14 +860,14 @@ public class LinkedTransferQueueTest ext Thread t = newStartedThread(new CheckedRunnable() { 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); } /** @@ -912,8 +881,8 @@ public class LinkedTransferQueueTest ext 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( @@ -946,14 +915,14 @@ public class LinkedTransferQueueTest ext Thread t = newStartedThread(new CheckedRunnable() { 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); } /** @@ -973,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); } /** @@ -1010,15 +979,15 @@ public class LinkedTransferQueueTest ext 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)); }}); assertSame(hotPotato, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); checkEmpty(q); - t.join(); + awaitTermination(t, MEDIUM_DELAY_MS); } /** @@ -1033,15 +1002,15 @@ public class LinkedTransferQueueTest ext 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)); }}); assertSame(q.take(), hotPotato); checkEmpty(q); - t.join(); + awaitTermination(t, MEDIUM_DELAY_MS); } /** @@ -1069,13 +1038,15 @@ public class LinkedTransferQueueTest ext 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); }}); - Thread.sleep(SMALL_DELAY_MS); checkEmpty(q); - t.join(); + awaitTermination(t, MEDIUM_DELAY_MS); + checkEmpty(q); } /** @@ -1094,10 +1065,10 @@ public class LinkedTransferQueueTest ext 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); } /** @@ -1110,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); }