--- jsr166/src/test/tck/DelayQueueTest.java 2011/05/28 22:40:00 1.52 +++ jsr166/src/test/tck/DelayQueueTest.java 2011/06/27 20:37:32 1.55 @@ -7,17 +7,37 @@ */ 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; @@ -29,33 +49,22 @@ public class DelayQueueTest extends JSR1 */ static class PDelay implements Delayed { int pseudodelay; - PDelay(int i) { pseudodelay = Integer.MIN_VALUE + i; } - public int compareTo(PDelay y) { - int i = pseudodelay; - int j = y.pseudodelay; - if (i < j) return -1; - if (i > j) return 1; - return 0; + PDelay(int i) { pseudodelay = i; } + public int compareTo(PDelay other) { + int a = this.pseudodelay; + int b = other.pseudodelay; + return (a < b) ? -1 : (a > b) ? 1 : 0; } - public int compareTo(Delayed y) { return compareTo((PDelay)y); } - public boolean equals(Object other) { - return equals((PDelay)other); - } - public boolean equals(PDelay other) { - return other.pseudodelay == pseudodelay; + return (other instanceof PDelay) && + this.pseudodelay == ((PDelay)other).pseudodelay; } - public long getDelay(TimeUnit ignore) { - return pseudodelay; + return Integer.MIN_VALUE + pseudodelay; } - public int intValue() { - return pseudodelay; - } - public String toString() { return String.valueOf(pseudodelay); } @@ -206,28 +215,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() { @@ -248,17 +235,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() { @@ -270,18 +246,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 */ @@ -312,17 +276,6 @@ 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() { @@ -379,29 +332,6 @@ 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() { - threadStarted.countDown(); - try { - q.take(); - shouldThrow(); - } catch (InterruptedException success) {} - assertFalse(Thread.interrupted()); - }}); - - await(threadStarted); - assertThreadStaysAlive(t); - t.interrupt(); - awaitTermination(t); - } - - /** * Take removes existing elements until empty, then blocks interruptibly */ public void testBlockingTake() throws InterruptedException { @@ -507,47 +437,6 @@ public class DelayQueueTest extends JSR1 } /** - * 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 = newStartedThread(new CheckedRunnable() { - public void realRun() throws InterruptedException { - assertNull(q.poll(timeoutMillis(), MILLISECONDS)); - - barrier.await(); - assertSame(pdelay, q.poll(LONG_DELAY_MS, MILLISECONDS)); - - Thread.currentThread().interrupt(); - try { - q.poll(LONG_DELAY_MS, MILLISECONDS); - shouldThrow(); - } catch (InterruptedException success) {} - assertFalse(Thread.interrupted()); - - barrier.await(); - try { - q.poll(LONG_DELAY_MS, MILLISECONDS); - shouldThrow(); - } catch (InterruptedException success) {} - assertFalse(Thread.interrupted()); - }}); - - barrier.await(); - long startTime = System.nanoTime(); - assertTrue(q.offer(pdelay, LONG_DELAY_MS, MILLISECONDS)); - assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); - - barrier.await(); - assertThreadStaysAlive(t); - t.interrupt(); - awaitTermination(t); - } - - /** * peek returns next element, or null if empty */ public void testPeek() { @@ -710,17 +599,6 @@ public class DelayQueueTest extends JSR1 } /** - * toArray(null) throws NullPointerException - */ - public void testToArray_NullArg() { - DelayQueue q = populatedQueue(SIZE); - try { - q.toArray(null); - shouldThrow(); - } catch (NullPointerException success) {} - } - - /** * toArray(incompatible array type) throws ArrayStoreException */ public void testToArray1_BadArg() { @@ -768,9 +646,8 @@ public class DelayQueueTest extends JSR1 public void testToString() { DelayQueue q = populatedQueue(SIZE); String s = q.toString(); - for (int i = 0; i < SIZE; ++i) { - assertTrue(s.contains(String.valueOf(Integer.MIN_VALUE+i))); - } + for (Object e : q) + assertTrue(s.contains(e.toString())); } /** @@ -845,28 +722,6 @@ public class DelayQueueTest extends JSR1 } /** - * 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) {} - } - - /** * drainTo(c) empties queue into another collection c */ public void testDrainTo() { @@ -913,28 +768,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() {