--- jsr166/src/test/tck/SynchronousQueueTest.java 2009/11/21 21:00:34 1.16 +++ jsr166/src/test/tck/SynchronousQueueTest.java 2010/10/06 07:49:23 1.25 @@ -14,12 +14,26 @@ 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()); } /** @@ -119,6 +133,7 @@ public class SynchronousQueueTest extend shouldThrow(); } catch (NullPointerException success) {} } + /** * addAll throws ISE if no active taker */ @@ -169,23 +184,18 @@ public class SynchronousQueueTest extend 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(); @@ -198,7 +208,7 @@ public class SynchronousQueueTest extend 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)); + assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); }}); @@ -251,23 +261,18 @@ public class SynchronousQueueTest extend 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(); @@ -280,7 +285,7 @@ public class SynchronousQueueTest extend 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)); + assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); }}); @@ -349,29 +354,6 @@ public class SynchronousQueueTest extend } /** - * timed poll before a delayed offer fails; after offer succeeds; - * on interruption throws - */ - public void testTimedPollWithOffer() throws InterruptedException { - final SynchronousQueue q = new SynchronousQueue(); - Thread t = new Thread(new CheckedRunnable() { - public void realRun() throws InterruptedException { - assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); - assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); - try { - q.poll(LONG_DELAY_MS, MILLISECONDS); - shouldThrow(); - } catch (InterruptedException success) {} - }}); - - t.start(); - Thread.sleep(SMALL_DELAY_MS); - assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); - t.interrupt(); - t.join(); - } - - /** * Interrupted timed poll throws InterruptedException instead of * returning timeout status */ @@ -396,9 +378,9 @@ public class SynchronousQueueTest extend final SynchronousQueue q = new SynchronousQueue(true); Thread t = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { - assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); - assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); try { + assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); + assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); q.poll(LONG_DELAY_MS, MILLISECONDS); threadShouldThrow(); } catch (InterruptedException success) {} @@ -524,8 +506,8 @@ public class SynchronousQueueTest extend * toArray(null) throws NPE */ public void testToArray_BadArg() { + SynchronousQueue q = new SynchronousQueue(); try { - SynchronousQueue q = new SynchronousQueue(); Object o[] = q.toArray(null); shouldThrow(); } catch (NullPointerException success) {} @@ -573,23 +555,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()); + 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 { Thread.sleep(SMALL_DELAY_MS); - threadAssertEquals(one, q.take()); + assertSame(one, q.take()); }}); joinPool(executor); - } /** @@ -600,15 +580,15 @@ public class SynchronousQueueTest extend 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()); + 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)); + Thread.sleep(SHORT_DELAY_MS); + q.put(one); }}); joinPool(executor); @@ -728,9 +708,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();