--- jsr166/src/test/tck/DelayQueueTest.java 2003/09/25 11:02:41 1.5 +++ jsr166/src/test/tck/DelayQueueTest.java 2009/11/16 04:57:10 1.16 @@ -1,8 +1,9 @@ /* - * Written by members of JCP JSR-166 Expert Group and released to the - * public domain. Use, modify, and redistribute this code in any way - * without acknowledgement. Other contributors include Andrew Wright, - * Jeffrey Hayes, Pat Fischer, Mike Judd. + * 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 + * Other contributors include Andrew Wright, Jeffrey Hayes, + * Pat Fisher, Mike Judd. */ import junit.framework.*; @@ -11,7 +12,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() { @@ -21,14 +22,14 @@ public class DelayQueueTest extends JSR1 private static final int NOCAP = Integer.MAX_VALUE; /** - * A delayed implmentation for testing. + * A delayed implementation for testing. * Most tests use Pseudodelays, where delays are all elapsed * (so, no blocking solely for delays) but are still ordered - */ - static class PDelay implements Delayed { + */ + static class PDelay implements Delayed { int pseudodelay; PDelay(int i) { pseudodelay = Integer.MIN_VALUE + i; } - public int compareTo(Object y) { + public int compareTo(PDelay y) { int i = pseudodelay; int j = ((PDelay)y).pseudodelay; if (i < j) return -1; @@ -36,7 +37,7 @@ public class DelayQueueTest extends JSR1 return 0; } - public int compareTo(PDelay y) { + public int compareTo(Delayed y) { int i = pseudodelay; int j = ((PDelay)y).pseudodelay; if (i < j) return -1; @@ -68,12 +69,12 @@ public class DelayQueueTest extends JSR1 /** * Delayed implementation that actually delays */ - static class NanoDelay implements Delayed { + static class NanoDelay implements Delayed { long trigger; - NanoDelay(long i) { + NanoDelay(long i) { trigger = System.nanoTime() + i; } - public int compareTo(Object y) { + public int compareTo(NanoDelay y) { long i = trigger; long j = ((NanoDelay)y).trigger; if (i < j) return -1; @@ -81,7 +82,7 @@ public class DelayQueueTest extends JSR1 return 0; } - public int compareTo(NanoDelay y) { + public int compareTo(Delayed y) { long i = trigger; long j = ((NanoDelay)y).trigger; if (i < j) return -1; @@ -118,16 +119,16 @@ public class DelayQueueTest extends JSR1 private DelayQueue populatedQueue(int n) { DelayQueue q = new DelayQueue(); assertTrue(q.isEmpty()); - for(int i = n-1; i >= 0; i-=2) + for (int i = n-1; i >= 0; i-=2) assertTrue(q.offer(new PDelay(i))); - for(int i = (n & 1); i < n; i+=2) + for (int i = (n & 1); i < n; i+=2) assertTrue(q.offer(new PDelay(i))); assertFalse(q.isEmpty()); assertEquals(NOCAP, q.remainingCapacity()); assertEquals(n, q.size()); return q; } - + /** * A new queue has unbounded capacity */ @@ -228,7 +229,18 @@ public class DelayQueueTest extends JSR1 DelayQueue q = new DelayQueue(); q.offer(null); shouldThrow(); - } catch (NullPointerException success) { } + } catch (NullPointerException success) { } + } + + /** + * add(null) throws NPE + */ + public void testAddNull() { + try { + DelayQueue q = new DelayQueue(); + q.add(null); + shouldThrow(); + } catch (NullPointerException success) { } } /** @@ -262,6 +274,20 @@ public class DelayQueueTest extends JSR1 } catch (NullPointerException success) {} } + + + /** + * addAll(this) throws IAE + */ + public void testAddAllSelf() { + try { + DelayQueue q = populatedQueue(SIZE); + q.addAll(q); + shouldThrow(); + } + catch (IllegalArgumentException success) {} + } + /** * addAll of a collection with null elements throws NPE */ @@ -316,9 +342,9 @@ public class DelayQueueTest extends JSR1 DelayQueue q = new DelayQueue(); q.put(null); shouldThrow(); - } + } catch (NullPointerException success){ - } + } } /** @@ -386,7 +412,7 @@ public class DelayQueueTest extends JSR1 } finally { } } }); - + try { t.start(); Thread.sleep(SMALL_DELAY_MS); @@ -408,7 +434,7 @@ public class DelayQueueTest extends JSR1 } } catch (InterruptedException e){ unexpectedException(); - } + } } /** @@ -421,7 +447,7 @@ public class DelayQueueTest extends JSR1 try { q.take(); threadShouldThrow(); - } catch (InterruptedException success){ } + } catch (InterruptedException success){ } } }); try { @@ -448,11 +474,11 @@ public class DelayQueueTest extends JSR1 q.take(); threadShouldThrow(); } catch (InterruptedException success){ - } + } }}); t.start(); - try { - Thread.sleep(SHORT_DELAY_MS); + try { + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); } @@ -485,7 +511,7 @@ public class DelayQueueTest extends JSR1 assertNull(q.poll(0, TimeUnit.MILLISECONDS)); } catch (InterruptedException e){ unexpectedException(); - } + } } /** @@ -500,7 +526,7 @@ public class DelayQueueTest extends JSR1 assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); } catch (InterruptedException e){ unexpectedException(); - } + } } /** @@ -517,11 +543,11 @@ public class DelayQueueTest extends JSR1 } threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); } catch (InterruptedException success){ - } + } }}); t.start(); - try { - Thread.sleep(SHORT_DELAY_MS); + try { + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); } @@ -543,7 +569,7 @@ public class DelayQueueTest extends JSR1 q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); threadFail("Should block"); - } catch (InterruptedException success) { } + } catch (InterruptedException success) { } } }); try { @@ -555,7 +581,7 @@ public class DelayQueueTest extends JSR1 } catch (Exception e){ unexpectedException(); } - } + } /** @@ -566,8 +592,10 @@ public class DelayQueueTest extends JSR1 for (int i = 0; i < SIZE; ++i) { assertEquals(new PDelay(i), ((PDelay)q.peek())); q.poll(); - assertTrue(q.peek() == null || - i != ((PDelay)q.peek()).intValue()); + if (q.isEmpty()) + assertNull(q.peek()); + else + assertTrue(i != ((PDelay)q.peek()).intValue()); } assertNull(q.peek()); } @@ -600,7 +628,7 @@ public class DelayQueueTest extends JSR1 q.remove(); shouldThrow(); } catch (NoSuchElementException success){ - } + } } /** @@ -617,7 +645,7 @@ public class DelayQueueTest extends JSR1 } assertTrue(q.isEmpty()); } - + /** * contains(x) reports true when elements added but not yet removed */ @@ -639,8 +667,10 @@ public class DelayQueueTest extends JSR1 assertTrue(q.isEmpty()); assertEquals(0, q.size()); assertEquals(NOCAP, q.remainingCapacity()); - q.add(new PDelay(1)); + PDelay x = new PDelay(1); + q.add(x); assertFalse(q.isEmpty()); + assertTrue(q.contains(x)); q.clear(); assertTrue(q.isEmpty()); } @@ -702,11 +732,11 @@ public class DelayQueueTest extends JSR1 Object[] o = q.toArray(); Arrays.sort(o); try { - for(int i = 0; i < o.length; i++) + for (int i = 0; i < o.length; i++) assertEquals(o[i], q.take()); } catch (InterruptedException e){ unexpectedException(); - } + } } /** @@ -718,13 +748,36 @@ public class DelayQueueTest extends JSR1 ints = (PDelay[])q.toArray(ints); Arrays.sort(ints); try { - for(int i = 0; i < ints.length; i++) + for (int i = 0; i < ints.length; i++) assertEquals(ints[i], q.take()); } catch (InterruptedException e){ unexpectedException(); - } + } } - + + + /** + * toArray(null) throws NPE + */ + public void testToArray_BadArg() { + try { + DelayQueue q = populatedQueue(SIZE); + Object o[] = q.toArray(null); + shouldThrow(); + } catch (NullPointerException success){} + } + + /** + * toArray with incompatible array type throws CCE + */ + public void testToArray1_BadArg() { + try { + DelayQueue q = populatedQueue(SIZE); + Object o[] = q.toArray(new String[10] ); + shouldThrow(); + } catch (ArrayStoreException success){} + } + /** * iterator iterates through all elements */ @@ -732,7 +785,7 @@ public class DelayQueueTest extends JSR1 DelayQueue q = populatedQueue(SIZE); int i = 0; Iterator it = q.iterator(); - while(it.hasNext()) { + while (it.hasNext()) { assertTrue(q.contains(it.next())); ++i; } @@ -766,7 +819,7 @@ public class DelayQueueTest extends JSR1 for (int i = 0; i < SIZE; ++i) { assertTrue(s.indexOf(String.valueOf(Integer.MIN_VALUE+i)) >= 0); } - } + } /** * offer transfers elements across Executor tasks @@ -804,7 +857,7 @@ public class DelayQueueTest extends JSR1 /** - * Dekayed actions do not occur until their delay elapses + * Delayed actions do not occur until their delay elapses */ public void testDelay() { DelayQueue q = new DelayQueue(); @@ -822,14 +875,159 @@ public class DelayQueueTest extends JSR1 NanoDelay e = (NanoDelay)(q.take()); long tt = e.getTriggerTime(); assertTrue(tt <= System.nanoTime()); - if (i != 0) + if (i != 0) assertTrue(tt >= last); last = tt; } } - catch(InterruptedException ie) { + catch (InterruptedException ie) { + unexpectedException(); + } + } + + /** + * peek of a non-empty queue returns non-null even if not expired + */ + public void testPeekDelayed() { + DelayQueue q = new DelayQueue(); + q.add(new NanoDelay(Long.MAX_VALUE)); + assert(q.peek() != null); + } + + + /** + * poll of a non-empty queue returns null if no expired elements. + */ + public void testPollDelayed() { + DelayQueue q = new DelayQueue(); + q.add(new NanoDelay(Long.MAX_VALUE)); + assertNull(q.poll()); + } + + /** + * timed poll of a non-empty queue returns null if no expired elements. + */ + public void testTimedPollDelayed() { + DelayQueue q = new DelayQueue(); + q.add(new NanoDelay(LONG_DELAY_MS * 1000000L)); + try { + assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); + } catch (Exception ex) { + unexpectedException(); + } + } + + /** + * 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() { + DelayQueue q = new DelayQueue(); + PDelay[] elems = new PDelay[SIZE]; + for (int i = 0; i < SIZE; ++i) { + elems[i] = new PDelay(i); + q.add(elems[i]); + } + ArrayList l = new ArrayList(); + q.drainTo(l); + assertEquals(q.size(), 0); + for (int i = 0; i < SIZE; ++i) + assertEquals(l.get(i), elems[i]); + q.add(elems[0]); + q.add(elems[1]); + assertFalse(q.isEmpty()); + assertTrue(q.contains(elems[0])); + assertTrue(q.contains(elems[1])); + l.clear(); + q.drainTo(l); + assertEquals(q.size(), 0); + assertEquals(l.size(), 2); + for (int i = 0; i < 2; ++i) + assertEquals(l.get(i), elems[i]); + } + + /** + * drainTo empties queue + */ + public void testDrainToWithActivePut() { + final DelayQueue q = populatedQueue(SIZE); + Thread t = new Thread(new Runnable() { + public void run() { + q.put(new PDelay(SIZE+1)); + } + }); + try { + t.start(); + ArrayList l = new ArrayList(); + q.drainTo(l); + assertTrue(l.size() >= SIZE); + t.join(); + assertTrue(q.size() + l.size() >= SIZE); + } catch (Exception e){ unexpectedException(); } } + /** + * 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 max {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; + assertEquals(q.size(), SIZE-k); + assertEquals(l.size(), k); + } + } + + }