--- jsr166/src/test/tck/DelayQueueTest.java 2010/11/03 16:46:34 1.43 +++ jsr166/src/test/tck/DelayQueueTest.java 2011/05/31 16:16:23 1.54 @@ -1,23 +1,43 @@ /* * 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. */ import junit.framework.*; -import java.util.*; +import java.util.Arrays; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.NoSuchElementException; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Delayed; +import java.util.concurrent.DelayQueue; +import java.util.concurrent.Executors; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.TimeUnit; import static java.util.concurrent.TimeUnit.MILLISECONDS; -import java.util.concurrent.*; public class DelayQueueTest extends JSR166TestCase { + + public static class Generic extends BlockingQueueTest { + protected BlockingQueue emptyCollection() { + return new DelayQueue(); + } + protected PDelay makeElement(int i) { + return new PDelay(i); + } + } + public static void main(String[] args) { junit.textui.TestRunner.run(suite()); } public static Test suite() { - return new TestSuite(DelayQueueTest.class); + return newTestSuite(DelayQueueTest.class, + new Generic().testSuite()); } private static final int NOCAP = Integer.MAX_VALUE; @@ -49,7 +69,6 @@ public class DelayQueueTest extends JSR1 return other.pseudodelay == pseudodelay; } - public long getDelay(TimeUnit ignore) { return pseudodelay; } @@ -62,7 +81,6 @@ public class DelayQueueTest extends JSR1 } } - /** * Delayed implementation that actually delays */ @@ -104,13 +122,12 @@ 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))); @@ -209,28 +226,6 @@ public class DelayQueueTest extends JSR1 } /** - * offer(null) throws NPE - */ - public void testOfferNull() { - try { - DelayQueue q = new DelayQueue(); - q.offer(null); - shouldThrow(); - } catch (NullPointerException success) {} - } - - /** - * add(null) throws NPE - */ - public void testAddNull() { - try { - DelayQueue q = new DelayQueue(); - q.add(null); - shouldThrow(); - } catch (NullPointerException success) {} - } - - /** * offer non-null succeeds */ public void testOffer() { @@ -251,18 +246,6 @@ public class DelayQueueTest extends JSR1 } /** - * addAll(null) throws NPE - */ - public void testAddAll1() { - try { - DelayQueue q = new DelayQueue(); - q.addAll(null); - shouldThrow(); - } catch (NullPointerException success) {} - } - - - /** * addAll(this) throws IAE */ public void testAddAllSelf() { @@ -274,18 +257,6 @@ public class DelayQueueTest extends JSR1 } /** - * addAll of a collection with null elements throws NPE - */ - public void testAddAll2() { - try { - DelayQueue q = new DelayQueue(); - PDelay[] ints = new PDelay[SIZE]; - q.addAll(Arrays.asList(ints)); - shouldThrow(); - } catch (NullPointerException success) {} - } - - /** * addAll of a collection with any null elements throws NPE after * possibly adding some elements */ @@ -316,27 +287,16 @@ public class DelayQueueTest extends JSR1 } /** - * put(null) throws NPE - */ - 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()); } /** @@ -344,7 +304,7 @@ public class DelayQueueTest extends JSR1 */ public void testPutWithTake() throws InterruptedException { final DelayQueue q = new DelayQueue(); - Thread t = new Thread(new CheckedRunnable() { + Thread t = newStartedThread(new CheckedRunnable() { public void realRun() { q.put(new PDelay(0)); q.put(new PDelay(0)); @@ -352,11 +312,8 @@ public class DelayQueueTest extends JSR1 q.put(new PDelay(0)); }}); - t.start(); - Thread.sleep(SHORT_DELAY_MS); - q.take(); - t.interrupt(); - t.join(); + awaitTermination(t); + assertEquals(4, q.size()); } /** @@ -364,7 +321,7 @@ public class DelayQueueTest extends JSR1 */ public void testTimedOffer() throws InterruptedException { final DelayQueue q = new DelayQueue(); - Thread t = new Thread(new CheckedRunnable() { + Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { q.put(new PDelay(0)); q.put(new PDelay(0)); @@ -372,10 +329,7 @@ public class DelayQueueTest extends JSR1 assertTrue(q.offer(new PDelay(0), LONG_DELAY_MS, MILLISECONDS)); }}); - t.start(); - Thread.sleep(SMALL_DELAY_MS); - t.interrupt(); - t.join(); + awaitTermination(t); } /** @@ -389,53 +343,38 @@ public class DelayQueueTest extends JSR1 } /** - * take blocks interruptibly when empty - */ - 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(); - awaitTermination(t, MEDIUM_DELAY_MS); - assertFalse(t.isAlive()); - } - - /** * Take removes existing elements until empty, then blocks interruptibly */ public void testBlockingTake() throws InterruptedException { final DelayQueue q = populatedQueue(SIZE); - Thread t = new Thread(new CheckedRunnable() { + final CountDownLatch pleaseInterrupt = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { for (int i = 0; i < SIZE; ++i) { assertEquals(new PDelay(i), ((PDelay)q.take())); } + + Thread.currentThread().interrupt(); + try { + q.take(); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + + pleaseInterrupt.countDown(); try { q.take(); shouldThrow(); } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); }}); - t.start(); - Thread.sleep(SHORT_DELAY_MS); + await(pleaseInterrupt); + assertThreadStaysAlive(t); t.interrupt(); - t.join(); + awaitTermination(t); } - /** * poll succeeds unless empty */ @@ -464,9 +403,14 @@ public class DelayQueueTest extends JSR1 public void testTimedPoll() throws InterruptedException { DelayQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS))); - } - assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); + long startTime = System.nanoTime(); + assertEquals(new PDelay(i), ((PDelay)q.poll(LONG_DELAY_MS, MILLISECONDS))); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); + } + long startTime = System.nanoTime(); + assertNull(q.poll(timeoutMillis(), MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); + checkEmpty(q); } /** @@ -474,62 +418,35 @@ public class DelayQueueTest extends JSR1 * returning timeout status */ public void testInterruptedTimedPoll() throws InterruptedException { - Thread t = new Thread(new CheckedRunnable() { + final CountDownLatch pleaseInterrupt = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { DelayQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS))); } - try { - q.poll(SMALL_DELAY_MS, MILLISECONDS); - shouldThrow(); - } catch (InterruptedException success) {} - }}); - - t.start(); - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); - } - - /** - * 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)); - - barrier.await(); - assertSame(pdelay, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); Thread.currentThread().interrupt(); try { - q.poll(SHORT_DELAY_MS, MILLISECONDS); + q.poll(LONG_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); - barrier.await(); + pleaseInterrupt.countDown(); try { - q.poll(MEDIUM_DELAY_MS, MILLISECONDS); + q.poll(LONG_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); }}); - t.start(); - barrier.await(); - assertTrue(q.offer(pdelay, SHORT_DELAY_MS, MILLISECONDS)); - barrier.await(); - sleep(SHORT_DELAY_MS); + await(pleaseInterrupt); + assertThreadStaysAlive(t); t.interrupt(); - t.join(); + awaitTermination(t); } - /** * peek returns next element, or null if empty */ @@ -676,31 +593,20 @@ 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()); - } - - - /** - * toArray(null) throws NullPointerException - */ - public void testToArray_NullArg() { - DelayQueue q = populatedQueue(SIZE); - try { - q.toArray(null); - shouldThrow(); - } catch (NullPointerException success) {} + assertSame(ints[i], q.remove()); } /** @@ -745,7 +651,6 @@ public class DelayQueueTest extends JSR1 assertFalse(it.hasNext()); } - /** * toString contains toStrings of elements */ @@ -753,33 +658,34 @@ public class DelayQueueTest extends JSR1 DelayQueue q = populatedQueue(SIZE); String s = q.toString(); for (int i = 0; i < SIZE; ++i) { - assertTrue(s.indexOf(String.valueOf(Integer.MIN_VALUE+i)) >= 0); + assertTrue(s.contains(String.valueOf(Integer.MIN_VALUE+i))); } } /** - * offer transfers elements across Executor tasks + * timed poll transfers elements across Executor tasks */ public void testPollInExecutor() { final DelayQueue q = new DelayQueue(); + final CheckedBarrier threadsStarted = new CheckedBarrier(2); ExecutorService executor = Executors.newFixedThreadPool(2); executor.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { assertNull(q.poll()); - assertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); - assertTrue(q.isEmpty()); + threadsStarted.await(); + assertTrue(null != q.poll(LONG_DELAY_MS, MILLISECONDS)); + checkEmpty(q); }}); executor.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { - Thread.sleep(SHORT_DELAY_MS); + threadsStarted.await(); q.put(new PDelay(1)); }}); joinPool(executor); } - /** * Delayed actions do not occur until their delay elapses */ @@ -809,7 +715,6 @@ public class DelayQueueTest extends JSR1 assertNotNull(q.peek()); } - /** * poll of a non-empty queue returns null if no expired elements. */ @@ -825,29 +730,7 @@ public class DelayQueueTest extends JSR1 public void testTimedPollDelayed() throws InterruptedException { DelayQueue q = new DelayQueue(); q.add(new NanoDelay(LONG_DELAY_MS * 1000000L)); - assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); - } - - /** - * drainTo(null) throws NPE - */ - public void testDrainToNull() { - DelayQueue q = populatedQueue(SIZE); - try { - q.drainTo(null); - shouldThrow(); - } catch (NullPointerException success) {} - } - - /** - * drainTo(this) throws IAE - */ - public void testDrainToSelf() { - DelayQueue q = populatedQueue(SIZE); - try { - q.drainTo(q); - shouldThrow(); - } catch (IllegalArgumentException success) {} + assertNull(q.poll(timeoutMillis(), MILLISECONDS)); } /** @@ -897,28 +780,6 @@ public class DelayQueueTest extends JSR1 } /** - * drainTo(null, n) throws NPE - */ - public void testDrainToNullN() { - DelayQueue q = populatedQueue(SIZE); - try { - q.drainTo(null, 0); - shouldThrow(); - } catch (NullPointerException success) {} - } - - /** - * drainTo(this, n) throws IAE - */ - public void testDrainToSelfN() { - DelayQueue q = populatedQueue(SIZE); - try { - q.drainTo(q, 0); - shouldThrow(); - } catch (IllegalArgumentException success) {} - } - - /** * drainTo(c, n) empties first min(n, size) elements of queue into c */ public void testDrainToN() { @@ -932,5 +793,4 @@ public class DelayQueueTest extends JSR1 } } - }