--- jsr166/src/test/tck/DelayQueueTest.java 2009/11/21 09:28:16 1.20 +++ jsr166/src/test/tck/DelayQueueTest.java 2010/11/05 00:17:22 1.45 @@ -13,7 +13,7 @@ import java.util.concurrent.*; public class DelayQueueTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run (suite()); + junit.textui.TestRunner.run(suite()); } public static Test suite() { @@ -24,7 +24,7 @@ public class DelayQueueTest extends JSR1 /** * A delayed implementation for testing. - * Most tests use Pseudodelays, where delays are all elapsed + * Most tests use Pseudodelays, where delays are all elapsed * (so, no blocking solely for delays) but are still ordered */ static class PDelay implements Delayed { @@ -32,25 +32,21 @@ public class DelayQueueTest extends JSR1 PDelay(int i) { pseudodelay = Integer.MIN_VALUE + i; } public int compareTo(PDelay y) { int i = pseudodelay; - int j = ((PDelay)y).pseudodelay; + int j = y.pseudodelay; if (i < j) return -1; if (i > j) return 1; return 0; } public int compareTo(Delayed y) { - int i = pseudodelay; - int j = ((PDelay)y).pseudodelay; - if (i < j) return -1; - if (i > j) return 1; - return 0; + return compareTo((PDelay)y); } public boolean equals(Object other) { - return ((PDelay)other).pseudodelay == pseudodelay; + return equals((PDelay)other); } public boolean equals(PDelay other) { - return ((PDelay)other).pseudodelay == pseudodelay; + return other.pseudodelay == pseudodelay; } @@ -77,25 +73,21 @@ public class DelayQueueTest extends JSR1 } public int compareTo(NanoDelay y) { long i = trigger; - long j = ((NanoDelay)y).trigger; + long j = y.trigger; if (i < j) return -1; if (i > j) return 1; return 0; } public int compareTo(Delayed y) { - long i = trigger; - long j = ((NanoDelay)y).trigger; - if (i < j) return -1; - if (i > j) return 1; - return 0; + return compareTo((NanoDelay)y); } public boolean equals(Object other) { - return ((NanoDelay)other).trigger == trigger; + return equals((NanoDelay)other); } public boolean equals(NanoDelay other) { - return ((NanoDelay)other).trigger == trigger; + return other.trigger == trigger; } public long getDelay(TimeUnit unit) { @@ -117,8 +109,8 @@ public class DelayQueueTest extends JSR1 * Create a queue of given size containing consecutive * PDelays 0 ... n. */ - private DelayQueue populatedQueue(int n) { - DelayQueue q = new DelayQueue(); + private DelayQueue populatedQueue(int n) { + DelayQueue q = new DelayQueue(); assertTrue(q.isEmpty()); for (int i = n-1; i >= 0; i-=2) assertTrue(q.offer(new PDelay(i))); @@ -175,15 +167,12 @@ public class DelayQueueTest extends JSR1 * Queue contains all elements of collection used to initialize */ public void testConstructor6() { - try { - PDelay[] ints = new PDelay[SIZE]; - for (int i = 0; i < SIZE; ++i) - ints[i] = new PDelay(i); - DelayQueue q = new DelayQueue(Arrays.asList(ints)); - for (int i = 0; i < SIZE; ++i) - assertEquals(ints[i], q.poll()); - } - finally {} + PDelay[] ints = new PDelay[SIZE]; + for (int i = 0; i < SIZE; ++i) + ints[i] = new PDelay(i); + DelayQueue q = new DelayQueue(Arrays.asList(ints)); + for (int i = 0; i < SIZE; ++i) + assertEquals(ints[i], q.poll()); } /** @@ -202,7 +191,7 @@ public class DelayQueueTest extends JSR1 } /** - * remainingCapacity does not change when elementa added or removed, + * remainingCapacity does not change when elements added or removed, * but size does */ public void testRemainingCapacity() { @@ -295,6 +284,7 @@ public class DelayQueueTest extends JSR1 shouldThrow(); } catch (NullPointerException success) {} } + /** * addAll of a collection with any null elements throws NPE after * possibly adding some elements @@ -314,18 +304,15 @@ public class DelayQueueTest extends JSR1 * Queue contains all elements of successful addAll */ public void testAddAll5() { - try { - PDelay[] empty = new PDelay[0]; - PDelay[] ints = new PDelay[SIZE]; - for (int i = SIZE-1; i >= 0; --i) - ints[i] = new PDelay(i); - DelayQueue q = new DelayQueue(); - assertFalse(q.addAll(Arrays.asList(empty))); - assertTrue(q.addAll(Arrays.asList(ints))); - for (int i = 0; i < SIZE; ++i) - assertEquals(ints[i], q.poll()); - } - finally {} + PDelay[] empty = new PDelay[0]; + PDelay[] ints = new PDelay[SIZE]; + for (int i = SIZE-1; i >= 0; --i) + ints[i] = new PDelay(i); + DelayQueue q = new DelayQueue(); + assertFalse(q.addAll(Arrays.asList(empty))); + assertTrue(q.addAll(Arrays.asList(ints))); + for (int i = 0; i < SIZE; ++i) + assertEquals(ints[i], q.poll()); } /** @@ -381,8 +368,8 @@ public class DelayQueueTest extends JSR1 public void realRun() throws InterruptedException { q.put(new PDelay(0)); q.put(new PDelay(0)); - threadAssertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, MILLISECONDS)); - threadAssertTrue(q.offer(new PDelay(0), LONG_DELAY_MS, MILLISECONDS)); + assertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, MILLISECONDS)); + assertTrue(q.offer(new PDelay(0), LONG_DELAY_MS, MILLISECONDS)); }}); t.start(); @@ -404,31 +391,43 @@ public class DelayQueueTest extends JSR1 /** * take blocks interruptibly when empty */ - public void testTakeFromEmpty() throws InterruptedException { - final DelayQueue q = new DelayQueue(); - Thread t = new ThreadShouldThrow(InterruptedException.class) { - public void realRun() throws InterruptedException { - q.take(); - }}; - - t.start(); + public void testTakeFromEmptyBlocksInterruptibly() + throws InterruptedException { + final BlockingQueue q = new DelayQueue(); + final CountDownLatch threadStarted = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() { + long t0 = System.nanoTime(); + threadStarted.countDown(); + try { + q.take(); + shouldThrow(); + } catch (InterruptedException expected) {} + assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); + }}); + threadStarted.await(); Thread.sleep(SHORT_DELAY_MS); + assertTrue(t.isAlive()); t.interrupt(); - t.join(); + awaitTermination(t, MEDIUM_DELAY_MS); + assertFalse(t.isAlive()); } /** * Take removes existing elements until empty, then blocks interruptibly */ public void testBlockingTake() throws InterruptedException { - Thread t = new ThreadShouldThrow(InterruptedException.class) { + final DelayQueue q = populatedQueue(SIZE); + Thread t = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { - DelayQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - threadAssertEquals(new PDelay(i), ((PDelay)q.take())); + assertEquals(new PDelay(i), ((PDelay)q.take())); } - q.take(); - }}; + try { + q.take(); + shouldThrow(); + } catch (InterruptedException success) {} + }}); t.start(); Thread.sleep(SHORT_DELAY_MS); @@ -449,7 +448,7 @@ public class DelayQueueTest extends JSR1 } /** - * 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 { DelayQueue q = populatedQueue(SIZE); @@ -460,7 +459,7 @@ public class DelayQueueTest extends JSR1 } /** - * 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 { DelayQueue q = populatedQueue(SIZE); @@ -482,7 +481,7 @@ public class DelayQueueTest extends JSR1 assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS))); } try { - q.poll(LONG_DELAY_MS, MILLISECONDS); + q.poll(SMALL_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (InterruptedException success) {} }}); @@ -494,24 +493,38 @@ public class DelayQueueTest extends JSR1 } /** - * timed poll before a delayed offer fails; after offer succeeds; - * on interruption throws + * timed poll before a delayed offer fails; after offer succeeds; + * on interruption throws */ public void testTimedPollWithOffer() throws InterruptedException { final DelayQueue q = new DelayQueue(); + final PDelay pdelay = new PDelay(0); + final CheckedBarrier barrier = new CheckedBarrier(2); Thread t = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); - q.poll(LONG_DELAY_MS, MILLISECONDS); + + barrier.await(); + assertSame(pdelay, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); + + Thread.currentThread().interrupt(); + try { + q.poll(SHORT_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + + barrier.await(); try { - q.poll(LONG_DELAY_MS, MILLISECONDS); + q.poll(MEDIUM_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (InterruptedException success) {} }}); t.start(); - Thread.sleep(SMALL_DELAY_MS); - assertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, MILLISECONDS)); + barrier.await(); + assertTrue(q.offer(pdelay, SHORT_DELAY_MS, MILLISECONDS)); + barrier.await(); + sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); } @@ -524,11 +537,11 @@ public class DelayQueueTest extends JSR1 DelayQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { assertEquals(new PDelay(i), ((PDelay)q.peek())); - q.poll(); + assertEquals(new PDelay(i), ((PDelay)q.poll())); if (q.isEmpty()) assertNull(q.peek()); else - assertTrue(i != ((PDelay)q.peek()).intValue()); + assertFalse(new PDelay(i).equals(q.peek())); } assertNull(q.peek()); } @@ -663,40 +676,41 @@ public class DelayQueueTest extends JSR1 Object[] o = q.toArray(); Arrays.sort(o); for (int i = 0; i < o.length; i++) - assertEquals(o[i], q.take()); + assertSame(o[i], q.take()); } /** * toArray(a) contains all elements */ - public void testToArray2() throws InterruptedException { - DelayQueue q = populatedQueue(SIZE); + public void testToArray2() { + DelayQueue q = populatedQueue(SIZE); PDelay[] ints = new PDelay[SIZE]; - ints = (PDelay[])q.toArray(ints); + PDelay[] array = q.toArray(ints); + assertSame(ints, array); Arrays.sort(ints); for (int i = 0; i < ints.length; i++) - assertEquals(ints[i], q.take()); + assertSame(ints[i], q.remove()); } /** - * toArray(null) throws NPE + * toArray(null) throws NullPointerException */ - public void testToArray_BadArg() { + public void testToArray_NullArg() { + DelayQueue q = populatedQueue(SIZE); try { - DelayQueue q = populatedQueue(SIZE); - Object o[] = q.toArray(null); + q.toArray(null); shouldThrow(); } catch (NullPointerException success) {} } /** - * toArray with incompatible array type throws CCE + * toArray(incompatible array type) throws ArrayStoreException */ public void testToArray1_BadArg() { + DelayQueue q = populatedQueue(SIZE); try { - DelayQueue q = populatedQueue(SIZE); - Object o[] = q.toArray(new String[10] ); + q.toArray(new String[10]); shouldThrow(); } catch (ArrayStoreException success) {} } @@ -718,7 +732,7 @@ public class DelayQueueTest extends JSR1 /** * iterator.remove removes current element */ - public void testIteratorRemove () { + public void testIteratorRemove() { final DelayQueue q = new DelayQueue(); q.add(new PDelay(2)); q.add(new PDelay(1)); @@ -752,9 +766,9 @@ public class DelayQueueTest extends JSR1 ExecutorService executor = Executors.newFixedThreadPool(2); executor.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { - threadAssertNull(q.poll()); - threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); - threadAssertTrue(q.isEmpty()); + assertNull(q.poll()); + assertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); + assertTrue(q.isEmpty()); }}); executor.execute(new CheckedRunnable() { @@ -764,7 +778,6 @@ public class DelayQueueTest extends JSR1 }}); joinPool(executor); - } @@ -772,24 +785,20 @@ public class DelayQueueTest extends JSR1 * Delayed actions do not occur until their delay elapses */ public void testDelay() throws InterruptedException { - DelayQueue q = new DelayQueue(); - NanoDelay[] elements = new NanoDelay[SIZE]; - for (int i = 0; i < SIZE; ++i) { - elements[i] = new NanoDelay(1000000000L + 1000000L * (SIZE - i)); - } - for (int i = 0; i < SIZE; ++i) { - q.add(elements[i]); - } + DelayQueue q = new DelayQueue(); + for (int i = 0; i < SIZE; ++i) + q.add(new NanoDelay(1000000L * (SIZE - i))); long last = 0; for (int i = 0; i < SIZE; ++i) { - NanoDelay e = (NanoDelay)(q.take()); + NanoDelay e = q.take(); long tt = e.getTriggerTime(); - assertTrue(tt <= System.nanoTime()); + assertTrue(System.nanoTime() - tt >= 0); if (i != 0) assertTrue(tt >= last); last = tt; } + assertTrue(q.isEmpty()); } /** @@ -798,7 +807,7 @@ public class DelayQueueTest extends JSR1 public void testPeekDelayed() { DelayQueue q = new DelayQueue(); q.add(new NanoDelay(Long.MAX_VALUE)); - assert(q.peek() != null); + assertNotNull(q.peek()); } @@ -911,14 +920,14 @@ public class DelayQueueTest extends JSR1 } /** - * 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() { for (int i = 0; i < SIZE + 2; ++i) { DelayQueue q = populatedQueue(SIZE); ArrayList l = new ArrayList(); q.drainTo(l, i); - int k = (i < SIZE)? i : SIZE; + int k = (i < SIZE) ? i : SIZE; assertEquals(q.size(), SIZE-k); assertEquals(l.size(), k); }