--- jsr166/src/test/tck/SynchronousQueueTest.java 2010/10/06 07:49:23 1.25 +++ jsr166/src/test/tck/SynchronousQueueTest.java 2011/05/21 06:24:33 1.34 @@ -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. */ @@ -37,10 +37,9 @@ public class SynchronousQueueTest extend } /** - * A SynchronousQueue is both empty and full + * Any SynchronousQueue is both empty and full */ - public void testEmptyFull() { - SynchronousQueue q = new SynchronousQueue(); + public void testEmptyFull(SynchronousQueue q) { assertTrue(q.isEmpty()); assertEquals(0, q.size()); assertEquals(0, q.remainingCapacity()); @@ -48,14 +47,17 @@ public class SynchronousQueueTest extend } /** + * A non-fair SynchronousQueue is both empty and full + */ + public void testEmptyFull() { + testEmptyFull(new SynchronousQueue()); + } + + /** * A fair SynchronousQueue is both empty and full */ public void testFairEmptyFull() { - SynchronousQueue q = new SynchronousQueue(true); - assertTrue(q.isEmpty()); - assertEquals(0, q.size()); - assertEquals(0, q.remainingCapacity()); - assertFalse(q.offer(zero)); + testEmptyFull(new SynchronousQueue(true)); } /** @@ -157,7 +159,7 @@ public class SynchronousQueueTest extend q.put(null); shouldThrow(); } catch (NullPointerException success) {} - } + } /** * put blocks interruptibly if no active taker @@ -170,7 +172,7 @@ public class SynchronousQueueTest extend }}); t.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); t.interrupt(); t.join(); } @@ -194,9 +196,9 @@ public class SynchronousQueueTest extend }}); t.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertEquals(0, q.take()); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); t.interrupt(); t.join(); } @@ -204,37 +206,39 @@ public class SynchronousQueueTest extend /** * timed offer times out if elements not taken */ - public void testTimedOffer() throws InterruptedException { - final SynchronousQueue q = new SynchronousQueue(); - Thread t = new Thread(new CheckedInterruptedRunnable() { - public void realRun() throws InterruptedException { - assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); - q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); + public void testTimedOffer(final SynchronousQueue q) + throws InterruptedException { + final CountDownLatch pleaseInterrupt = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + long startTime = System.nanoTime(); + assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); + pleaseInterrupt.countDown(); + try { + 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); } - /** - * take blocks interruptibly when empty + * timed offer times out if elements not taken */ - public void testTakeFromEmpty() throws InterruptedException { - final SynchronousQueue q = new SynchronousQueue(); - Thread t = new Thread(new CheckedInterruptedRunnable() { - public void realRun() throws InterruptedException { - q.take(); - }}); - - t.start(); - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); + public void testTimedOffer() throws InterruptedException { + testTimedOffer(new SynchronousQueue()); } + /** + * timed offer times out if elements not taken + */ + public void testFairTimedOffer() throws InterruptedException { + testTimedOffer(new SynchronousQueue(true)); + } /** * put blocks interruptibly if no active taker @@ -247,7 +251,7 @@ public class SynchronousQueueTest extend }}); t.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); t.interrupt(); t.join(); } @@ -271,31 +275,13 @@ public class SynchronousQueueTest extend }}); t.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); assertEquals(0, q.take()); - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); - } - - /** - * timed offer times out if elements not taken - */ - public void testFairTimedOffer() throws InterruptedException { - final SynchronousQueue q = new SynchronousQueue(true); - Thread t = new Thread(new CheckedInterruptedRunnable() { - public void realRun() throws InterruptedException { - assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); - q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); - }}); - - t.start(); - Thread.sleep(SMALL_DELAY_MS); + delay(SHORT_DELAY_MS); t.interrupt(); t.join(); } - /** * take blocks interruptibly when empty */ @@ -307,13 +293,13 @@ public class SynchronousQueueTest extend }}); t.start(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); t.interrupt(); t.join(); } /** - * poll fails unless active taker + * poll return null if no active putter */ public void testPoll() { SynchronousQueue q = new SynchronousQueue(); @@ -321,7 +307,7 @@ public class SynchronousQueueTest extend } /** - * timed pool with zero timeout times out if no active taker + * timed poll with zero timeout times out if no active putter */ public void testTimedPoll0() throws InterruptedException { SynchronousQueue q = new SynchronousQueue(); @@ -329,73 +315,93 @@ public class SynchronousQueueTest extend } /** - * timed pool with nonzero timeout times out if no active taker + * timed poll with nonzero timeout times out if no active putter */ public void testTimedPoll() throws InterruptedException { SynchronousQueue q = new SynchronousQueue(); + long t0 = System.nanoTime(); assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); + assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); } /** * Interrupted timed poll throws InterruptedException instead of * returning timeout status */ - public void testInterruptedTimedPoll() throws InterruptedException { - final SynchronousQueue q = new SynchronousQueue(); - Thread t = new Thread(new CheckedInterruptedRunnable() { + public void testInterruptedTimedPoll(final SynchronousQueue q) + throws InterruptedException { + final CountDownLatch threadStarted = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { - q.poll(SMALL_DELAY_MS, MILLISECONDS); + long t0 = System.nanoTime(); + threadStarted.countDown(); + try { + q.poll(LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); + assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); }}); - t.start(); - Thread.sleep(SHORT_DELAY_MS); + threadStarted.await(); + delay(SHORT_DELAY_MS); t.interrupt(); - t.join(); + awaitTermination(t, MEDIUM_DELAY_MS); } /** * Interrupted timed poll throws InterruptedException instead of * returning timeout status */ - public void testFairInterruptedTimedPoll() throws InterruptedException { - Thread t = new Thread(new CheckedInterruptedRunnable() { - public void realRun() throws InterruptedException { - SynchronousQueue q = new SynchronousQueue(true); - q.poll(SMALL_DELAY_MS, MILLISECONDS); - }}); + public void testInterruptedTimedPoll() throws InterruptedException { + testInterruptedTimedPoll(new SynchronousQueue()); + } - t.start(); - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); + /** + * Interrupted timed poll throws InterruptedException instead of + * returning timeout status + */ + public void testFairInterruptedTimedPoll() throws InterruptedException { + testInterruptedTimedPoll(new SynchronousQueue(true)); } /** - * timed poll before a delayed offer fails; after offer succeeds; - * on interruption throws + * timed poll before a delayed offer times out, returning null; + * after offer succeeds; on interruption throws */ public void testFairTimedPollWithOffer() throws InterruptedException { final SynchronousQueue q = new SynchronousQueue(true); - Thread t = new Thread(new CheckedRunnable() { + final CountDownLatch pleaseOffer = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { + long t0 = System.nanoTime(); + assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); + assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); + + pleaseOffer.countDown(); + t0 = System.nanoTime(); + assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); + assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); + + t0 = System.nanoTime(); try { - assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); - assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); q.poll(LONG_DELAY_MS, MILLISECONDS); - threadShouldThrow(); + shouldThrow(); } catch (InterruptedException success) {} + assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); }}); - t.start(); - Thread.sleep(SMALL_DELAY_MS); - assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); + assertTrue(pleaseOffer.await(MEDIUM_DELAY_MS, MILLISECONDS)); + long t0 = System.nanoTime(); + assertTrue(q.offer(zero, LONG_DELAY_MS, MILLISECONDS)); + assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); + t.interrupt(); - t.join(); + awaitTermination(t, MEDIUM_DELAY_MS); } - /** - * peek returns null + * peek() returns null if no active putter */ public void testPeek() { SynchronousQueue q = new SynchronousQueue(); @@ -403,7 +409,7 @@ public class SynchronousQueueTest extend } /** - * element throws NSEE + * element() throws NSEE if no active putter */ public void testElement() { SynchronousQueue q = new SynchronousQueue(); @@ -414,7 +420,7 @@ public class SynchronousQueueTest extend } /** - * remove throws NSEE if no active taker + * remove() throws NSEE if no active putter */ public void testRemove() { SynchronousQueue q = new SynchronousQueue(); @@ -565,7 +571,7 @@ public class SynchronousQueueTest extend executor.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { - Thread.sleep(SMALL_DELAY_MS); + delay(SMALL_DELAY_MS); assertSame(one, q.take()); }}); @@ -587,7 +593,7 @@ public class SynchronousQueueTest extend executor.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); q.put(one); }}); @@ -657,7 +663,7 @@ public class SynchronousQueueTest extend t.start(); ArrayList l = new ArrayList(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); q.drainTo(l); assertTrue(l.size() <= 1); if (l.size() > 0) @@ -706,7 +712,7 @@ public class SynchronousQueueTest extend t1.start(); t2.start(); ArrayList l = new ArrayList(); - Thread.sleep(SHORT_DELAY_MS); + delay(SHORT_DELAY_MS); q.drainTo(l, 1); assertEquals(1, l.size()); q.drainTo(l, 1);