--- jsr166/src/test/tck/BlockingQueueTest.java 2011/05/06 11:22:07 1.6 +++ jsr166/src/test/tck/BlockingQueueTest.java 2011/05/27 19:32:04 1.7 @@ -37,7 +37,7 @@ public abstract class BlockingQueueTest protected abstract BlockingQueue emptyCollection(); /** - * timed poll before a delayed offer fails; after offer succeeds; + * timed poll before a delayed offer times out; after offer succeeds; * on interruption throws */ public void testTimedPollWithOffer() throws InterruptedException { @@ -45,76 +45,120 @@ public abstract class BlockingQueueTest final CheckedBarrier barrier = new CheckedBarrier(2); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { - assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); + long startTime = System.nanoTime(); + assertNull(q.poll(timeoutMillis(), MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); barrier.await(); - assertSame(zero, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); + + assertSame(zero, 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()); }}); barrier.await(); - assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); + long startTime = System.nanoTime(); + assertTrue(q.offer(zero, LONG_DELAY_MS, MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); + barrier.await(); - sleep(SHORT_DELAY_MS); + assertThreadStaysAlive(t); t.interrupt(); - awaitTermination(t, MEDIUM_DELAY_MS); + awaitTermination(t); } /** * take() blocks interruptibly when empty */ - public void testTakeFromEmptyBlocksInterruptibly() - throws InterruptedException { + public void testTakeFromEmptyBlocksInterruptibly() { final BlockingQueue q = emptyCollection(); 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 success) {} - assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); + assertFalse(Thread.interrupted()); }}); - threadStarted.await(); - delay(SHORT_DELAY_MS); - assertTrue(t.isAlive()); + await(threadStarted); + assertThreadStaysAlive(t); t.interrupt(); - awaitTermination(t, MEDIUM_DELAY_MS); + awaitTermination(t); } /** * take() throws InterruptedException immediately if interrupted * before waiting */ - public void testTakeFromEmptyAfterInterrupt() - throws InterruptedException { + public void testTakeFromEmptyAfterInterrupt() { final BlockingQueue q = emptyCollection(); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() { - long t0 = System.nanoTime(); Thread.currentThread().interrupt(); try { q.take(); shouldThrow(); } catch (InterruptedException success) {} - assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); + assertFalse(Thread.interrupted()); + }}); + + awaitTermination(t); + } + + /** + * timed poll() blocks interruptibly when empty + */ + public void testTimedPollFromEmptyBlocksInterruptibly() { + final BlockingQueue q = emptyCollection(); + final CountDownLatch threadStarted = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() { + threadStarted.countDown(); + try { + q.poll(2 * LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + }}); + + await(threadStarted); + assertThreadStaysAlive(t); + t.interrupt(); + awaitTermination(t); + } + + /** + * timed poll() throws InterruptedException immediately if + * interrupted before waiting + */ + public void testTimedPollFromEmptyAfterInterrupt() { + final BlockingQueue q = emptyCollection(); + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() { + Thread.currentThread().interrupt(); + try { + q.poll(2 * LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); }}); - awaitTermination(t, MEDIUM_DELAY_MS); + awaitTermination(t); } /** For debugging. */