--- jsr166/src/test/tck/LinkedTransferQueueTest.java 2010/10/06 07:49:22 1.25 +++ jsr166/src/test/tck/LinkedTransferQueueTest.java 2010/10/28 19:05:04 1.29 @@ -18,6 +18,7 @@ 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; @@ -273,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); @@ -305,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); } @@ -341,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); @@ -359,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); @@ -370,7 +355,7 @@ public class LinkedTransferQueueTest ext Thread.sleep(SMALL_DELAY_MS); t.interrupt(); - t.join(); + awaitTermination(t, MEDIUM_DELAY_MS); checkEmpty(q); } @@ -780,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); } @@ -807,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(); @@ -850,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); } /** @@ -882,7 +867,7 @@ public class LinkedTransferQueueTest ext assertEquals(1, q.size()); assertEquals(SIZE, (int) q.poll()); assertTrue(q.isEmpty()); - t.join(); + awaitTermination(t, MEDIUM_DELAY_MS); } /** @@ -937,7 +922,7 @@ public class LinkedTransferQueueTest ext Thread.sleep(SHORT_DELAY_MS); assertTrue(q.offer(three)); assertSame(four, q.poll()); - t.join(); + awaitTermination(t, MEDIUM_DELAY_MS); } /** @@ -957,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); } /** @@ -1002,7 +987,7 @@ public class LinkedTransferQueueTest ext assertSame(hotPotato, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); checkEmpty(q); - t.join(); + awaitTermination(t, MEDIUM_DELAY_MS); } /** @@ -1025,7 +1010,7 @@ public class LinkedTransferQueueTest ext assertSame(q.take(), hotPotato); checkEmpty(q); - t.join(); + awaitTermination(t, MEDIUM_DELAY_MS); } /** @@ -1053,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); } /** @@ -1081,7 +1068,7 @@ public class LinkedTransferQueueTest ext assertSame(four, q.poll()); assertSame(five, q.poll()); checkEmpty(q); - t.join(); + awaitTermination(t, MEDIUM_DELAY_MS); } /**