--- jsr166/src/test/tck/DelayQueueTest.java 2010/08/25 00:07:03 1.31 +++ jsr166/src/test/tck/DelayQueueTest.java 2011/05/06 11:22:07 1.49 @@ -1,7 +1,7 @@ /* * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at - * http://creativecommons.org/licenses/publicdomain + * http://creativecommons.org/publicdomain/zero/1.0/ * Other contributors include Andrew Wright, Jeffrey Hayes, * Pat Fisher, Mike Judd. */ @@ -109,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))); @@ -284,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 @@ -317,25 +318,25 @@ public class DelayQueueTest extends JSR1 /** * put(null) throws NPE */ - public void testPutNull() { + public void testPutNull() { try { DelayQueue q = new DelayQueue(); q.put(null); shouldThrow(); } catch (NullPointerException success) {} - } + } /** * all elements successfully put are contained */ - public void testPut() { - DelayQueue q = new DelayQueue(); - for (int i = 0; i < SIZE; ++i) { - PDelay I = new PDelay(i); - q.put(I); - assertTrue(q.contains(I)); - } - assertEquals(SIZE, q.size()); + public void testPut() { + DelayQueue q = new DelayQueue(); + for (int i = 0; i < SIZE; ++i) { + PDelay I = new PDelay(i); + q.put(I); + assertTrue(q.contains(I)); + } + assertEquals(SIZE, q.size()); } /** @@ -352,7 +353,7 @@ public class DelayQueueTest extends JSR1 }}); t.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); q.take(); t.interrupt(); t.join(); @@ -372,7 +373,7 @@ public class DelayQueueTest extends JSR1 }}); t.start(); - Thread.sleep(SMALL_DELAY_MS); + delay(SMALL_DELAY_MS); t.interrupt(); t.join(); } @@ -390,17 +391,26 @@ 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(); - Thread.sleep(SHORT_DELAY_MS); + 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 success) {} + assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); + }}); + threadStarted.await(); + delay(SHORT_DELAY_MS); + assertTrue(t.isAlive()); t.interrupt(); - t.join(); + awaitTermination(t, MEDIUM_DELAY_MS); + assertFalse(t.isAlive()); } /** @@ -420,7 +430,7 @@ public class DelayQueueTest extends JSR1 }}); t.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); t.interrupt(); t.join(); } @@ -438,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); @@ -449,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); @@ -477,31 +487,44 @@ public class DelayQueueTest extends JSR1 }}); t.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); t.interrupt(); t.join(); } /** - * 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)); - assertSame(pdelay, 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); + barrier.await(); assertTrue(q.offer(pdelay, SHORT_DELAY_MS, MILLISECONDS)); + barrier.await(); + sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); } @@ -653,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 { - 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 { - Object o[] = q.toArray(new String[10]); + q.toArray(new String[10]); shouldThrow(); } catch (ArrayStoreException success) {} } @@ -749,7 +773,7 @@ public class DelayQueueTest extends JSR1 executor.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); q.put(new PDelay(1)); }}); @@ -761,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()); } /** @@ -787,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()); } @@ -900,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); }