--- jsr166/src/test/tck/DelayQueueTest.java 2005/09/25 13:10:59 1.14 +++ jsr166/src/test/tck/DelayQueueTest.java 2009/11/16 04:57:10 1.16 @@ -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,8 +25,8 @@ 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(PDelay y) { @@ -69,9 +69,9 @@ 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(NanoDelay y) { @@ -119,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 */ @@ -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(); } - } + } /** @@ -628,7 +628,7 @@ public class DelayQueueTest extends JSR1 q.remove(); shouldThrow(); } catch (NoSuchElementException success){ - } + } } /** @@ -645,7 +645,7 @@ public class DelayQueueTest extends JSR1 } assertTrue(q.isEmpty()); } - + /** * contains(x) reports true when elements added but not yet removed */ @@ -732,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(); - } + } } /** @@ -748,11 +748,11 @@ 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(); - } + } } @@ -764,7 +764,7 @@ public class DelayQueueTest extends JSR1 DelayQueue q = populatedQueue(SIZE); Object o[] = q.toArray(null); shouldThrow(); - } catch(NullPointerException success){} + } catch (NullPointerException success){} } /** @@ -775,9 +775,9 @@ public class DelayQueueTest extends JSR1 DelayQueue q = populatedQueue(SIZE); Object o[] = q.toArray(new String[10] ); shouldThrow(); - } catch(ArrayStoreException success){} + } catch (ArrayStoreException success){} } - + /** * iterator iterates through all elements */ @@ -785,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; } @@ -819,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 @@ -875,12 +875,12 @@ 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(); } } @@ -919,31 +919,31 @@ public class DelayQueueTest extends JSR1 /** * drainTo(null) throws NPE - */ + */ public void testDrainToNull() { DelayQueue q = populatedQueue(SIZE); try { q.drainTo(null); shouldThrow(); - } catch(NullPointerException success) { + } catch (NullPointerException success) { } } /** * drainTo(this) throws IAE - */ + */ public void testDrainToSelf() { DelayQueue q = populatedQueue(SIZE); try { q.drainTo(q); shouldThrow(); - } catch(IllegalArgumentException success) { + } catch (IllegalArgumentException success) { } } /** * drainTo(c) empties queue into another collection c - */ + */ public void testDrainTo() { DelayQueue q = new DelayQueue(); PDelay[] elems = new PDelay[SIZE]; @@ -954,7 +954,7 @@ public class DelayQueueTest extends JSR1 ArrayList l = new ArrayList(); q.drainTo(l); assertEquals(q.size(), 0); - for (int i = 0; i < SIZE; ++i) + for (int i = 0; i < SIZE; ++i) assertEquals(l.get(i), elems[i]); q.add(elems[0]); q.add(elems[1]); @@ -965,13 +965,13 @@ public class DelayQueueTest extends JSR1 q.drainTo(l); assertEquals(q.size(), 0); assertEquals(l.size(), 2); - for (int i = 0; i < 2; ++i) + 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() { @@ -986,38 +986,38 @@ public class DelayQueueTest extends JSR1 assertTrue(l.size() >= SIZE); t.join(); assertTrue(q.size() + l.size() >= SIZE); - } catch(Exception e){ + } 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) { + } catch (NullPointerException success) { } } /** * drainTo(this, n) throws IAE - */ + */ public void testDrainToSelfN() { DelayQueue q = populatedQueue(SIZE); try { q.drainTo(q, 0); shouldThrow(); - } catch(IllegalArgumentException success) { + } 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);