--- jsr166/src/test/tck/LinkedBlockingQueueTest.java 2010/11/18 20:21:53 1.38 +++ jsr166/src/test/tck/LinkedBlockingQueueTest.java 2011/05/21 06:24:33 1.43 @@ -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. */ @@ -277,13 +277,13 @@ public class LinkedBlockingQueueTest ext /** * put(null) throws NPE */ - public void testPutNull() throws InterruptedException { + public void testPutNull() throws InterruptedException { try { LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); q.put(null); shouldThrow(); } catch (NullPointerException success) {} - } + } /** * all elements successfully put are contained @@ -316,7 +316,7 @@ public class LinkedBlockingQueueTest ext }}); t.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); t.interrupt(); t.join(); assertEquals(SIZE, q.size()); @@ -340,10 +340,10 @@ public class LinkedBlockingQueueTest ext }}); t.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertEquals(q.remainingCapacity(), 0); assertEquals(0, q.take()); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); t.interrupt(); t.join(); assertEquals(q.remainingCapacity(), 0); @@ -352,23 +352,26 @@ public class LinkedBlockingQueueTest ext /** * timed offer times out if full and elements not taken */ - public void testTimedOffer() throws InterruptedException { + public void testTimedOffer() { final LinkedBlockingQueue q = new LinkedBlockingQueue(2); - Thread t = new Thread(new CheckedRunnable() { + final CountDownLatch pleaseInterrupt = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { q.put(new Object()); q.put(new Object()); - assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); + long startTime = System.nanoTime(); + assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); + pleaseInterrupt.countDown(); try { - q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); + q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (InterruptedException success) {} }}); - t.start(); - Thread.sleep(SMALL_DELAY_MS); + await(pleaseInterrupt); t.interrupt(); - t.join(); + awaitTermination(t); } /** @@ -398,7 +401,7 @@ public class LinkedBlockingQueueTest ext }}); t.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); t.interrupt(); t.join(); } @@ -441,22 +444,30 @@ public class LinkedBlockingQueueTest ext * returning timeout status */ public void testInterruptedTimedPoll() throws InterruptedException { - Thread t = new Thread(new CheckedRunnable() { + final BlockingQueue q = populatedQueue(SIZE); + final CountDownLatch aboutToWait = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { - LinkedBlockingQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); + long t0 = System.nanoTime(); + assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); + assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); } + long t0 = System.nanoTime(); + aboutToWait.countDown(); try { - q.poll(SMALL_DELAY_MS, MILLISECONDS); + q.poll(MEDIUM_DELAY_MS, MILLISECONDS); shouldThrow(); - } catch (InterruptedException success) {} + } catch (InterruptedException success) { + assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); + } }}); - t.start(); - Thread.sleep(SHORT_DELAY_MS); + aboutToWait.await(); + waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); t.interrupt(); - t.join(); + awaitTermination(t, MEDIUM_DELAY_MS); + checkEmpty(q); } /** @@ -750,7 +761,7 @@ public class LinkedBlockingQueueTest ext executor.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { - Thread.sleep(SMALL_DELAY_MS); + delay(SMALL_DELAY_MS); assertSame(one, q.take()); }}); @@ -772,7 +783,7 @@ public class LinkedBlockingQueueTest ext executor.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { - Thread.sleep(SMALL_DELAY_MS); + delay(SMALL_DELAY_MS); q.put(one); }});