--- jsr166/src/test/tck/DelayQueueTest.java 2010/11/05 00:17:22 1.45 +++ jsr166/src/test/tck/DelayQueueTest.java 2011/05/28 12:45:38 1.51 @@ -1,7 +1,7 @@ /* * 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. */ @@ -49,7 +49,6 @@ public class DelayQueueTest extends JSR1 return other.pseudodelay == pseudodelay; } - public long getDelay(TimeUnit ignore) { return pseudodelay; } @@ -62,7 +61,6 @@ public class DelayQueueTest extends JSR1 } } - /** * Delayed implementation that actually delays */ @@ -104,7 +102,6 @@ public class DelayQueueTest extends JSR1 } } - /** * Create a queue of given size containing consecutive * PDelays 0 ... n. @@ -261,7 +258,6 @@ public class DelayQueueTest extends JSR1 } catch (NullPointerException success) {} } - /** * addAll(this) throws IAE */ @@ -318,25 +314,25 @@ public class DelayQueueTest extends JSR1 /** * put(null) throws NPE */ - public void testPutNull() { + 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 +340,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 +348,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 +357,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 +365,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); } /** @@ -397,20 +387,18 @@ public class DelayQueueTest extends JSR1 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); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); }}); - threadStarted.await(); - Thread.sleep(SHORT_DELAY_MS); - assertTrue(t.isAlive()); + + await(threadStarted); + assertThreadStaysAlive(t); t.interrupt(); - awaitTermination(t, MEDIUM_DELAY_MS); - assertFalse(t.isAlive()); + awaitTermination(t); } /** @@ -418,24 +406,34 @@ public class DelayQueueTest extends JSR1 */ 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 +462,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,22 +477,33 @@ 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))); } + + Thread.currentThread().interrupt(); + try { + q.poll(LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + + pleaseInterrupt.countDown(); try { - q.poll(SMALL_DELAY_MS, MILLISECONDS); + q.poll(LONG_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); }}); - t.start(); - Thread.sleep(SHORT_DELAY_MS); + await(pleaseInterrupt); + assertThreadStaysAlive(t); t.interrupt(); - t.join(); + awaitTermination(t); } /** @@ -500,36 +514,39 @@ public class DelayQueueTest extends JSR1 final DelayQueue q = new DelayQueue(); final PDelay pdelay = new PDelay(0); final CheckedBarrier barrier = new CheckedBarrier(2); - Thread t = new Thread(new CheckedRunnable() { + Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); barrier.await(); - assertSame(pdelay, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); + assertSame(pdelay, q.poll(LONG_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(); 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)); + long startTime = System.nanoTime(); + assertTrue(q.offer(pdelay, LONG_DELAY_MS, MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); + barrier.await(); - sleep(SHORT_DELAY_MS); + assertThreadStaysAlive(t); t.interrupt(); - t.join(); + awaitTermination(t); } - /** * peek returns next element, or null if empty */ @@ -692,7 +709,6 @@ public class DelayQueueTest extends JSR1 assertSame(ints[i], q.remove()); } - /** * toArray(null) throws NullPointerException */ @@ -746,7 +762,6 @@ public class DelayQueueTest extends JSR1 assertFalse(it.hasNext()); } - /** * toString contains toStrings of elements */ @@ -754,33 +769,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 */ @@ -810,7 +826,6 @@ public class DelayQueueTest extends JSR1 assertNotNull(q.peek()); } - /** * poll of a non-empty queue returns null if no expired elements. */ @@ -933,5 +948,4 @@ public class DelayQueueTest extends JSR1 } } - }