--- jsr166/src/test/tck/DelayQueueTest.java 2003/12/29 19:05:40 1.8 +++ jsr166/src/test/tck/DelayQueueTest.java 2009/11/02 20:28:31 1.15 @@ -2,8 +2,8 @@ * 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. + * Other contributors include Andrew Wright, Jeffrey Hayes, + * Pat Fisher, Mike Judd. */ import junit.framework.*; @@ -12,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() { @@ -25,11 +25,11 @@ public class DelayQueueTest extends JSR1 * 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; @@ -37,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; @@ -69,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; @@ -82,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; @@ -128,7 +128,7 @@ public class DelayQueueTest extends JSR1 assertEquals(n, q.size()); return q; } - + /** * A new queue has unbounded capacity */ @@ -229,7 +229,7 @@ public class DelayQueueTest extends JSR1 DelayQueue q = new DelayQueue(); q.offer(null); shouldThrow(); - } catch (NullPointerException success) { } + } catch (NullPointerException success) { } } /** @@ -240,7 +240,7 @@ public class DelayQueueTest extends JSR1 DelayQueue q = new DelayQueue(); q.add(null); shouldThrow(); - } catch (NullPointerException success) { } + } catch (NullPointerException success) { } } /** @@ -342,9 +342,9 @@ public class DelayQueueTest extends JSR1 DelayQueue q = new DelayQueue(); q.put(null); shouldThrow(); - } + } catch (NullPointerException success){ - } + } } /** @@ -412,7 +412,7 @@ public class DelayQueueTest extends JSR1 } finally { } } }); - + try { t.start(); Thread.sleep(SMALL_DELAY_MS); @@ -434,7 +434,7 @@ public class DelayQueueTest extends JSR1 } } catch (InterruptedException e){ unexpectedException(); - } + } } /** @@ -447,7 +447,7 @@ public class DelayQueueTest extends JSR1 try { q.take(); threadShouldThrow(); - } catch (InterruptedException success){ } + } catch (InterruptedException success){ } } }); try { @@ -474,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(); } @@ -511,7 +511,7 @@ public class DelayQueueTest extends JSR1 assertNull(q.poll(0, TimeUnit.MILLISECONDS)); } catch (InterruptedException e){ unexpectedException(); - } + } } /** @@ -526,7 +526,7 @@ public class DelayQueueTest extends JSR1 assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); } catch (InterruptedException e){ unexpectedException(); - } + } } /** @@ -543,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(); } @@ -569,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 { @@ -581,7 +581,7 @@ public class DelayQueueTest extends JSR1 } catch (Exception e){ unexpectedException(); } - } + } /** @@ -592,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()); } @@ -626,7 +628,7 @@ public class DelayQueueTest extends JSR1 q.remove(); shouldThrow(); } catch (NoSuchElementException success){ - } + } } /** @@ -643,7 +645,7 @@ public class DelayQueueTest extends JSR1 } assertTrue(q.isEmpty()); } - + /** * contains(x) reports true when elements added but not yet removed */ @@ -665,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()); } @@ -732,7 +736,7 @@ public class DelayQueueTest extends JSR1 assertEquals(o[i], q.take()); } catch (InterruptedException e){ unexpectedException(); - } + } } /** @@ -748,7 +752,7 @@ public class DelayQueueTest extends JSR1 assertEquals(ints[i], q.take()); } catch (InterruptedException e){ unexpectedException(); - } + } } @@ -773,7 +777,7 @@ public class DelayQueueTest extends JSR1 shouldThrow(); } catch(ArrayStoreException success){} } - + /** * iterator iterates through all elements */ @@ -815,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 @@ -871,7 +875,7 @@ 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; } @@ -881,10 +885,41 @@ public class DelayQueueTest extends JSR1 } } + /** + * 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 { @@ -896,7 +931,7 @@ public class DelayQueueTest extends JSR1 /** * drainTo(this) throws IAE - */ + */ public void testDrainToSelf() { DelayQueue q = populatedQueue(SIZE); try { @@ -908,18 +943,35 @@ public class DelayQueueTest extends JSR1 /** * drainTo(c) empties queue into another collection c - */ + */ public void testDrainTo() { - DelayQueue q = populatedQueue(SIZE); + 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); - assertEquals(l.size(), SIZE); + 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() { @@ -933,7 +985,7 @@ public class DelayQueueTest extends JSR1 q.drainTo(l); assertTrue(l.size() >= SIZE); t.join(); - assertTrue(q.size() + l.size() == SIZE+1); + assertTrue(q.size() + l.size() >= SIZE); } catch(Exception e){ unexpectedException(); } @@ -941,7 +993,7 @@ public class DelayQueueTest extends JSR1 /** * drainTo(null, n) throws NPE - */ + */ public void testDrainToNullN() { DelayQueue q = populatedQueue(SIZE); try { @@ -953,7 +1005,7 @@ public class DelayQueueTest extends JSR1 /** * drainTo(this, n) throws IAE - */ + */ public void testDrainToSelfN() { DelayQueue q = populatedQueue(SIZE); try { @@ -965,7 +1017,7 @@ public class DelayQueueTest extends JSR1 /** * 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);