--- jsr166/src/test/tck/SynchronousQueueTest.java 2009/11/19 01:15:42 1.13 +++ jsr166/src/test/tck/SynchronousQueueTest.java 2010/11/18 20:18:43 1.29 @@ -14,41 +14,57 @@ import java.io.*; public class SynchronousQueueTest extends JSR166TestCase { + public static class Fair extends BlockingQueueTest { + protected BlockingQueue emptyCollection() { + return new SynchronousQueue(true); + } + } + + public static class NonFair extends BlockingQueueTest { + protected BlockingQueue emptyCollection() { + return new SynchronousQueue(false); + } + } + public static void main(String[] args) { - junit.textui.TestRunner.run (suite()); + junit.textui.TestRunner.run(suite()); } public static Test suite() { - return new TestSuite(SynchronousQueueTest.class); + return newTestSuite(SynchronousQueueTest.class, + new Fair().testSuite(), + new NonFair().testSuite()); } /** - * 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.size()); assertEquals(0, q.remainingCapacity()); assertFalse(q.offer(zero)); } /** + * 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)); } /** * offer(null) throws NPE */ public void testOfferNull() { - try { + try { SynchronousQueue q = new SynchronousQueue(); q.offer(null); shouldThrow(); @@ -59,7 +75,7 @@ public class SynchronousQueueTest extend * add(null) throws NPE */ public void testAddNull() { - try { + try { SynchronousQueue q = new SynchronousQueue(); q.add(null); shouldThrow(); @@ -78,7 +94,7 @@ public class SynchronousQueueTest extend * add throws ISE if no active taker */ public void testAdd() { - try { + try { SynchronousQueue q = new SynchronousQueue(); assertEquals(0, q.remainingCapacity()); q.add(one); @@ -119,6 +135,7 @@ public class SynchronousQueueTest extend shouldThrow(); } catch (NullPointerException success) {} } + /** * addAll throws ISE if no active taker */ @@ -137,7 +154,7 @@ public class SynchronousQueueTest extend * put(null) throws NPE */ public void testPutNull() throws InterruptedException { - try { + try { SynchronousQueue q = new SynchronousQueue(); q.put(null); shouldThrow(); @@ -148,8 +165,8 @@ public class SynchronousQueueTest extend * put blocks interruptibly if no active taker */ public void testBlockingPut() throws InterruptedException { - Thread t = new Thread(new CheckedInterruptedRunnable() { - public void realRun() throws InterruptedException { + Thread t = new Thread(new CheckedInterruptedRunnable() { + public void realRun() throws InterruptedException { SynchronousQueue q = new SynchronousQueue(); q.put(zero); }}); @@ -165,27 +182,22 @@ public class SynchronousQueueTest extend */ public void testPutWithTake() throws InterruptedException { final SynchronousQueue q = new SynchronousQueue(); - Thread t = new Thread(new CheckedRunnable() { - public void realRun() throws InterruptedException { + Thread t = new Thread(new CheckedRunnable() { + public void realRun() throws InterruptedException { int added = 0; try { - q.put(new Object()); - ++added; - q.put(new Object()); - ++added; - q.put(new Object()); - ++added; - q.put(new Object()); - ++added; - threadShouldThrow(); + while (true) { + q.put(added); + ++added; + } } catch (InterruptedException success) { - assertTrue(added >= 1); + assertEquals(1, added); } }}); t.start(); Thread.sleep(SHORT_DELAY_MS); - q.take(); + assertEquals(0, q.take()); Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); @@ -194,44 +206,48 @@ 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 { - threadAssertFalse(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 t0 = System.nanoTime(); + assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); + assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); + pleaseInterrupt.countDown(); + t0 = System.nanoTime(); + try { + q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); }}); - t.start(); - Thread.sleep(SMALL_DELAY_MS); + assertTrue(pleaseInterrupt.await(MEDIUM_DELAY_MS, MILLISECONDS)); t.interrupt(); - t.join(); + awaitTermination(t, MEDIUM_DELAY_MS); } - /** - * 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 */ public void testFairBlockingPut() throws InterruptedException { - Thread t = new Thread(new CheckedInterruptedRunnable() { - public void realRun() throws InterruptedException { + Thread t = new Thread(new CheckedInterruptedRunnable() { + public void realRun() throws InterruptedException { SynchronousQueue q = new SynchronousQueue(true); q.put(zero); }}); @@ -247,57 +263,34 @@ public class SynchronousQueueTest extend */ public void testFairPutWithTake() throws InterruptedException { final SynchronousQueue q = new SynchronousQueue(true); - Thread t = new Thread(new CheckedRunnable() { - public void realRun() throws InterruptedException { + Thread t = new Thread(new CheckedRunnable() { + public void realRun() throws InterruptedException { int added = 0; try { - q.put(new Object()); - ++added; - q.put(new Object()); - ++added; - q.put(new Object()); - ++added; - q.put(new Object()); - ++added; - threadShouldThrow(); + while (true) { + q.put(added); + ++added; + } } catch (InterruptedException success) { - assertTrue(added >= 1); + assertEquals(1, added); } }}); t.start(); Thread.sleep(SHORT_DELAY_MS); - q.take(); + 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 { - threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); - q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); - }}); - - t.start(); - Thread.sleep(SMALL_DELAY_MS); - t.interrupt(); - t.join(); - } - - - /** * take blocks interruptibly when empty */ public void testFairTakeFromEmpty() throws InterruptedException { final SynchronousQueue q = new SynchronousQueue(true); - Thread t = new Thread(new CheckedInterruptedRunnable() { - public void realRun() throws InterruptedException { + Thread t = new Thread(new CheckedInterruptedRunnable() { + public void realRun() throws InterruptedException { q.take(); }}); @@ -308,15 +301,15 @@ public class SynchronousQueueTest extend } /** - * poll fails unless active taker + * poll return null if no active putter */ public void testPoll() { SynchronousQueue q = new SynchronousQueue(); - assertNull(q.poll()); + assertNull(q.poll()); } /** - * 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(); @@ -324,51 +317,46 @@ 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 { - Thread t = new Thread(new CheckedInterruptedRunnable() { - public void realRun() throws InterruptedException { - SynchronousQueue q = new SynchronousQueue(); - q.poll(SMALL_DELAY_MS, MILLISECONDS); + public void testInterruptedTimedPoll(final SynchronousQueue q) + throws InterruptedException { + final CountDownLatch threadStarted = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + 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(); + threadStarted.await(); Thread.sleep(SHORT_DELAY_MS); t.interrupt(); - t.join(); + awaitTermination(t, MEDIUM_DELAY_MS); } /** - * timed poll before a delayed offer fails; after offer succeeds; - * on interruption throws + * Interrupted timed poll throws InterruptedException instead of + * returning timeout status */ - public void testTimedPollWithOffer() throws InterruptedException { - final SynchronousQueue q = new SynchronousQueue(); - Thread t = new Thread(new CheckedRunnable() { - public void realRun() throws InterruptedException { - threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); - assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); - try { - q.poll(LONG_DELAY_MS, MILLISECONDS); - threadShouldThrow(); - } catch (InterruptedException success) {} - }}); - - t.start(); - Thread.sleep(SMALL_DELAY_MS); - assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); - t.interrupt(); - t.join(); + public void testInterruptedTimedPoll() throws InterruptedException { + testInterruptedTimedPoll(new SynchronousQueue()); } /** @@ -376,52 +364,54 @@ public class SynchronousQueueTest extend * 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); - }}); - - t.start(); - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); + 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() { - public void realRun() throws InterruptedException { - threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); + 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 { 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(); - assertNull(q.peek()); + assertNull(q.peek()); } /** - * element throws NSEE + * element() throws NSEE if no active putter */ public void testElement() { SynchronousQueue q = new SynchronousQueue(); @@ -432,15 +422,14 @@ 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(); try { q.remove(); shouldThrow(); - } catch (NoSuchElementException success) { - } + } catch (NoSuchElementException success) {} } /** @@ -508,7 +497,7 @@ public class SynchronousQueueTest extend */ public void testToArray() { SynchronousQueue q = new SynchronousQueue(); - Object[] o = q.toArray(); + Object[] o = q.toArray(); assertEquals(o.length, 0); } @@ -517,7 +506,7 @@ public class SynchronousQueueTest extend */ public void testToArray2() { SynchronousQueue q = new SynchronousQueue(); - Integer[] ints = new Integer[1]; + Integer[] ints = new Integer[1]; assertNull(ints[0]); } @@ -525,11 +514,11 @@ public class SynchronousQueueTest extend * toArray(null) throws NPE */ public void testToArray_BadArg() { - try { - SynchronousQueue q = new SynchronousQueue(); - Object o[] = q.toArray(null); - shouldThrow(); - } catch (NullPointerException success) {} + SynchronousQueue q = new SynchronousQueue(); + try { + Object o[] = q.toArray(null); + shouldThrow(); + } catch (NullPointerException success) {} } @@ -538,7 +527,7 @@ public class SynchronousQueueTest extend */ public void testIterator() { SynchronousQueue q = new SynchronousQueue(); - Iterator it = q.iterator(); + Iterator it = q.iterator(); assertFalse(it.hasNext()); try { Object x = it.next(); @@ -551,7 +540,7 @@ public class SynchronousQueueTest extend */ public void testIteratorRemove() { SynchronousQueue q = new SynchronousQueue(); - Iterator it = q.iterator(); + Iterator it = q.iterator(); try { it.remove(); shouldThrow(); @@ -574,23 +563,21 @@ public class SynchronousQueueTest extend public void testOfferInExecutor() { final SynchronousQueue q = new SynchronousQueue(); ExecutorService executor = Executors.newFixedThreadPool(2); - final Integer one = new Integer(1); executor.execute(new CheckedRunnable() { - public void realRun() throws InterruptedException { - threadAssertFalse(q.offer(one)); - threadAssertTrue(q.offer(one, MEDIUM_DELAY_MS, MILLISECONDS)); - threadAssertEquals(0, q.remainingCapacity()); + public void realRun() throws InterruptedException { + assertFalse(q.offer(one)); + assertTrue(q.offer(one, MEDIUM_DELAY_MS, MILLISECONDS)); + assertEquals(0, q.remainingCapacity()); }}); executor.execute(new CheckedRunnable() { - public void realRun() throws InterruptedException { + public void realRun() throws InterruptedException { Thread.sleep(SMALL_DELAY_MS); - threadAssertEquals(one, q.take()); + assertSame(one, q.take()); }}); joinPool(executor); - } /** @@ -600,16 +587,16 @@ public class SynchronousQueueTest extend final SynchronousQueue q = new SynchronousQueue(); ExecutorService executor = Executors.newFixedThreadPool(2); executor.execute(new CheckedRunnable() { - public void realRun() throws InterruptedException { - threadAssertNull(q.poll()); - threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); - threadAssertTrue(q.isEmpty()); + public void realRun() throws InterruptedException { + assertNull(q.poll()); + assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); + assertTrue(q.isEmpty()); }}); executor.execute(new CheckedRunnable() { - public void realRun() throws InterruptedException { - Thread.sleep(SMALL_DELAY_MS); - q.put(new Integer(1)); + public void realRun() throws InterruptedException { + Thread.sleep(SHORT_DELAY_MS); + q.put(one); }}); joinPool(executor); @@ -671,8 +658,8 @@ public class SynchronousQueueTest extend */ public void testDrainToWithActivePut() throws InterruptedException { final SynchronousQueue q = new SynchronousQueue(); - Thread t = new Thread(new CheckedRunnable() { - public void realRun() throws InterruptedException { + Thread t = new Thread(new CheckedRunnable() { + public void realRun() throws InterruptedException { q.put(new Integer(1)); }}); @@ -706,8 +693,7 @@ public class SynchronousQueueTest extend try { q.drainTo(q, 0); shouldThrow(); - } catch (IllegalArgumentException success) { - } + } catch (IllegalArgumentException success) {} } /** @@ -715,13 +701,13 @@ public class SynchronousQueueTest extend */ public void testDrainToN() throws InterruptedException { final SynchronousQueue q = new SynchronousQueue(); - Thread t1 = new Thread(new CheckedRunnable() { - public void realRun() throws InterruptedException { + Thread t1 = new Thread(new CheckedRunnable() { + public void realRun() throws InterruptedException { q.put(one); }}); - Thread t2 = new Thread(new CheckedRunnable() { - public void realRun() throws InterruptedException { + Thread t2 = new Thread(new CheckedRunnable() { + public void realRun() throws InterruptedException { q.put(two); }}); @@ -730,9 +716,9 @@ public class SynchronousQueueTest extend ArrayList l = new ArrayList(); Thread.sleep(SHORT_DELAY_MS); q.drainTo(l, 1); - assertTrue(l.size() == 1); + assertEquals(1, l.size()); q.drainTo(l, 1); - assertTrue(l.size() == 2); + assertEquals(2, l.size()); assertTrue(l.contains(one)); assertTrue(l.contains(two)); t1.join();