--- jsr166/src/test/tck/LinkedTransferQueueTest.java 2009/11/21 19:11:53 1.16 +++ jsr166/src/test/tck/LinkedTransferQueueTest.java 2010/10/06 07:49:22 1.25 @@ -24,12 +24,19 @@ import junit.framework.TestSuite; @SuppressWarnings({"unchecked", "rawtypes"}) public class LinkedTransferQueueTest extends JSR166TestCase { + public static class Generic extends BlockingQueueTest { + protected BlockingQueue emptyCollection() { + return new LinkedTransferQueue(); + } + } + public static void main(String[] args) { junit.textui.TestRunner.run(suite()); } public static Test suite() { - return new TestSuite(LinkedTransferQueueTest.class); + return newTestSuite(LinkedTransferQueueTest.class, + new Generic().testSuite()); } void checkEmpty(LinkedTransferQueue q) throws InterruptedException { @@ -44,18 +51,15 @@ public class LinkedTransferQueueTest ext try { q.element(); shouldThrow(); - } catch (NoSuchElementException success) { - } + } catch (NoSuchElementException success) {} try { q.iterator().next(); shouldThrow(); - } catch (NoSuchElementException success) { - } + } catch (NoSuchElementException success) {} try { q.remove(); shouldThrow(); - } catch (NoSuchElementException success) { - } + } catch (NoSuchElementException success) {} } /** @@ -75,8 +79,7 @@ public class LinkedTransferQueueTest ext try { new LinkedTransferQueue(null); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** @@ -88,8 +91,7 @@ public class LinkedTransferQueueTest ext Integer[] ints = new Integer[SIZE]; new LinkedTransferQueue(Arrays.asList(ints)); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** @@ -104,8 +106,7 @@ public class LinkedTransferQueueTest ext } new LinkedTransferQueue(Arrays.asList(ints)); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** @@ -157,8 +158,7 @@ public class LinkedTransferQueueTest ext LinkedTransferQueue q = new LinkedTransferQueue(); q.offer(null); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** @@ -169,8 +169,7 @@ public class LinkedTransferQueueTest ext LinkedTransferQueue q = new LinkedTransferQueue(); q.add(null); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** @@ -181,8 +180,7 @@ public class LinkedTransferQueueTest ext LinkedTransferQueue q = new LinkedTransferQueue(); q.addAll(null); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** @@ -193,8 +191,7 @@ public class LinkedTransferQueueTest ext LinkedTransferQueue q = populatedQueue(SIZE); q.addAll(q); shouldThrow(); - } catch (IllegalArgumentException success) { - } + } catch (IllegalArgumentException success) {} } /** @@ -206,8 +203,7 @@ public class LinkedTransferQueueTest ext Integer[] ints = new Integer[SIZE]; q.addAll(Arrays.asList(ints)); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** @@ -223,8 +219,7 @@ public class LinkedTransferQueueTest ext } q.addAll(Arrays.asList(ints)); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** @@ -283,7 +278,7 @@ public class LinkedTransferQueueTest ext public void testTakeFromEmpty() throws InterruptedException { final LinkedTransferQueue q = new LinkedTransferQueue(); Thread t = newStartedThread(new CheckedInterruptedRunnable() { - void realRun() throws InterruptedException { + public void realRun() throws InterruptedException { q.take(); }}); Thread.sleep(SHORT_DELAY_MS); @@ -296,14 +291,19 @@ public class LinkedTransferQueueTest ext */ public void testBlockingTake() throws InterruptedException { final LinkedTransferQueue q = populatedQueue(SIZE); - Thread t = newStartedThread(new CheckedInterruptedRunnable() { - void realRun() throws InterruptedException { + Thread t = new Thread(new CheckedRunnable() { + public void realRun() throws InterruptedException { for (int i = 0; i < SIZE; ++i) { - threadAssertEquals(i, (int) q.take()); + assertEquals(i, (int) q.take()); } - q.take(); + try { + q.take(); + shouldThrow(); + } catch (InterruptedException success) {} }}); - Thread.sleep(SMALL_DELAY_MS); + + t.start(); + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); checkEmpty(q); @@ -355,40 +355,23 @@ public class LinkedTransferQueueTest ext public void testInterruptedTimedPoll() throws InterruptedException { final LinkedTransferQueue q = populatedQueue(SIZE); Thread t = newStartedThread(new CheckedRunnable() { - void realRun() throws InterruptedException { + public void realRun() throws InterruptedException { for (int i = 0; i < SIZE; ++i) { long t0 = System.nanoTime(); - threadAssertEquals(i, (int) q.poll(LONG_DELAY_MS, - MILLISECONDS)); + assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024); assertTrue(millisElapsed < SMALL_DELAY_MS); } try { q.poll(LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); } catch (InterruptedException success) {} }}); - Thread.sleep(SMALL_DELAY_MS); - t.interrupt(); - t.join(); - checkEmpty(q); - } - /** - * timed poll before a delayed offer fails; after offer succeeds; - * on interruption throws - */ - public void testTimedPollWithOffer() throws InterruptedException { - final LinkedTransferQueue q = new LinkedTransferQueue(); - Thread t = newStartedThread(new CheckedInterruptedRunnable() { - void realRun() throws InterruptedException { - threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); - q.poll(LONG_DELAY_MS, MILLISECONDS); - q.poll(LONG_DELAY_MS, MILLISECONDS); - }}); Thread.sleep(SMALL_DELAY_MS); - assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); t.interrupt(); t.join(); + checkEmpty(q); } /** @@ -418,8 +401,7 @@ public class LinkedTransferQueueTest ext try { q.element(); shouldThrow(); - } catch (NoSuchElementException success) { - } + } catch (NoSuchElementException success) {} checkEmpty(q); } @@ -434,8 +416,7 @@ public class LinkedTransferQueueTest ext try { q.remove(); shouldThrow(); - } catch (NoSuchElementException success) { - } + } catch (NoSuchElementException success) {} checkEmpty(q); } @@ -464,7 +445,7 @@ public class LinkedTransferQueueTest ext assertTrue(q.remove(one)); assertTrue(q.remove(two)); assertTrue(q.add(three)); - assertTrue(q.take() == three); + assertSame(q.take(), three); } /** @@ -572,24 +553,22 @@ public class LinkedTransferQueueTest ext * toArray(null) throws NullPointerException */ public void testToArray_BadArg() { + LinkedTransferQueue q = populatedQueue(SIZE); try { - LinkedTransferQueue q = populatedQueue(SIZE); Object o[] = q.toArray(null); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** * toArray(incompatible array type) throws CCE */ public void testToArray1_BadArg() { + LinkedTransferQueue q = populatedQueue(SIZE); try { - LinkedTransferQueue q = populatedQueue(SIZE); Object o[] = q.toArray(new String[10]); shouldThrow(); - } catch (ArrayStoreException success) { - } + } catch (ArrayStoreException success) {} } /** @@ -619,8 +598,8 @@ public class LinkedTransferQueueTest ext it.remove(); it = q.iterator(); - assertEquals(it.next(), one); - assertEquals(it.next(), three); + assertSame(it.next(), one); + assertSame(it.next(), three); assertFalse(it.hasNext()); } @@ -678,15 +657,14 @@ public class LinkedTransferQueueTest ext ExecutorService executor = Executors.newFixedThreadPool(2); executor.execute(new CheckedRunnable() { - void realRun() { - threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, - MILLISECONDS)); + public void realRun() { + assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); }}); executor.execute(new CheckedRunnable() { - void realRun() throws InterruptedException { + public void realRun() throws InterruptedException { Thread.sleep(SMALL_DELAY_MS); - threadAssertEquals(one, q.take()); + assertSame(one, q.take()); }}); joinPool(executor); @@ -700,15 +678,14 @@ public class LinkedTransferQueueTest ext ExecutorService executor = Executors.newFixedThreadPool(2); executor.execute(new CheckedRunnable() { - 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() { - void realRun() throws InterruptedException { + public void realRun() throws InterruptedException { Thread.sleep(SMALL_DELAY_MS); q.put(one); }}); @@ -748,8 +725,7 @@ public class LinkedTransferQueueTest ext try { q.drainTo(null); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** @@ -760,8 +736,7 @@ public class LinkedTransferQueueTest ext try { q.drainTo(q); shouldThrow(); - } catch (IllegalArgumentException success) { - } + } catch (IllegalArgumentException success) {} } /** @@ -796,7 +771,7 @@ public class LinkedTransferQueueTest ext public void testDrainToWithActivePut() throws InterruptedException { final LinkedTransferQueue q = populatedQueue(SIZE); Thread t = newStartedThread(new CheckedRunnable() { - void realRun() { + public void realRun() { q.put(SIZE + 1); }}); ArrayList l = new ArrayList(); @@ -817,8 +792,7 @@ public class LinkedTransferQueueTest ext try { q.drainTo(null, SIZE); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** @@ -829,8 +803,7 @@ public class LinkedTransferQueueTest ext try { q.drainTo(q, SIZE); shouldThrow(); - } catch (IllegalArgumentException success) { - } + } catch (IllegalArgumentException success) {} } /** @@ -865,16 +838,16 @@ public class LinkedTransferQueueTest ext assertFalse(q.hasWaitingConsumer()); Thread t = newStartedThread(new CheckedRunnable() { - void realRun() throws InterruptedException { + public void realRun() throws InterruptedException { Thread.sleep(SMALL_DELAY_MS); - threadAssertTrue(q.hasWaitingConsumer()); - threadAssertEquals(q.getWaitingConsumerCount(), 1); - threadAssertTrue(q.offer(new Object())); - threadAssertFalse(q.hasWaitingConsumer()); - threadAssertEquals(q.getWaitingConsumerCount(), 0); + assertTrue(q.hasWaitingConsumer()); + assertEquals(q.getWaitingConsumerCount(), 1); + assertTrue(q.offer(one)); + assertFalse(q.hasWaitingConsumer()); + assertEquals(q.getWaitingConsumerCount(), 0); }}); - assertTrue(q.poll(LONG_DELAY_MS, MILLISECONDS) != null); + assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); assertEquals(q.getWaitingConsumerCount(), 0); assertFalse(q.hasWaitingConsumer()); t.join(); @@ -900,9 +873,9 @@ public class LinkedTransferQueueTest ext = new LinkedTransferQueue(); Thread t = newStartedThread(new CheckedRunnable() { - void realRun() throws InterruptedException { + public void realRun() throws InterruptedException { q.transfer(SIZE); - threadAssertTrue(q.isEmpty()); + assertTrue(q.isEmpty()); }}); Thread.sleep(SHORT_DELAY_MS); @@ -920,16 +893,16 @@ public class LinkedTransferQueueTest ext = new LinkedTransferQueue(); Thread first = newStartedThread(new CheckedRunnable() { - void realRun() throws InterruptedException { + public void realRun() throws InterruptedException { Integer i = SIZE + 1; q.transfer(i); - threadAssertTrue(!q.contains(i)); - threadAssertEquals(1, q.size()); + assertTrue(!q.contains(i)); + assertEquals(1, q.size()); }}); Thread interruptedThread = newStartedThread( new CheckedInterruptedRunnable() { - void realRun() throws InterruptedException { + public void realRun() throws InterruptedException { while (q.size() == 0) Thread.yield(); q.transfer(SIZE); @@ -955,15 +928,15 @@ public class LinkedTransferQueueTest ext final LinkedTransferQueue q = new LinkedTransferQueue(); Thread t = newStartedThread(new CheckedRunnable() { - void realRun() throws InterruptedException { + public void realRun() throws InterruptedException { q.transfer(four); - threadAssertFalse(q.contains(four)); - threadAssertEquals(three, q.poll()); + assertFalse(q.contains(four)); + assertSame(three, q.poll()); }}); Thread.sleep(SHORT_DELAY_MS); assertTrue(q.offer(three)); - assertEquals(four, q.poll()); + assertSame(four, q.poll()); t.join(); } @@ -976,7 +949,7 @@ public class LinkedTransferQueueTest ext = new LinkedTransferQueue(); Thread t = newStartedThread(new CheckedRunnable() { - void realRun() throws InterruptedException { + public void realRun() throws InterruptedException { q.transfer(SIZE); checkEmpty(q); }}); @@ -1018,16 +991,16 @@ public class LinkedTransferQueueTest ext final LinkedTransferQueue q = new LinkedTransferQueue(); Thread t = newStartedThread(new CheckedRunnable() { - void realRun() { + public void realRun() { while (! q.hasWaitingConsumer()) Thread.yield(); - threadAssertTrue(q.hasWaitingConsumer()); - threadAssertTrue(q.isEmpty()); - threadAssertTrue(q.size() == 0); - threadAssertTrue(q.tryTransfer(hotPotato)); + assertTrue(q.hasWaitingConsumer()); + assertTrue(q.isEmpty()); + assertEquals(q.size(), 0); + assertTrue(q.tryTransfer(hotPotato)); }}); - assertTrue(q.poll(MEDIUM_DELAY_MS, MILLISECONDS) == hotPotato); + assertSame(hotPotato, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); checkEmpty(q); t.join(); } @@ -1041,16 +1014,16 @@ public class LinkedTransferQueueTest ext final LinkedTransferQueue q = new LinkedTransferQueue(); Thread t = newStartedThread(new CheckedRunnable() { - void realRun() { + public void realRun() { while (! q.hasWaitingConsumer()) Thread.yield(); - threadAssertTrue(q.hasWaitingConsumer()); - threadAssertTrue(q.isEmpty()); - threadAssertTrue(q.size() == 0); - threadAssertTrue(q.tryTransfer(hotPotato)); + assertTrue(q.hasWaitingConsumer()); + assertTrue(q.isEmpty()); + assertEquals(q.size(), 0); + assertTrue(q.tryTransfer(hotPotato)); }}); - assertTrue(q.take() == hotPotato); + assertSame(q.take(), hotPotato); checkEmpty(q); t.join(); } @@ -1063,7 +1036,7 @@ public class LinkedTransferQueueTest ext final LinkedTransferQueue q = new LinkedTransferQueue(); Thread toInterrupt = newStartedThread(new CheckedInterruptedRunnable() { - void realRun() throws InterruptedException { + public void realRun() throws InterruptedException { q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); }}); @@ -1079,10 +1052,9 @@ public class LinkedTransferQueueTest ext final LinkedTransferQueue q = new LinkedTransferQueue(); Thread t = newStartedThread(new CheckedRunnable() { - void realRun() throws InterruptedException { - threadAssertFalse - (q.tryTransfer(new Object(), - SHORT_DELAY_MS, MILLISECONDS)); + public void realRun() throws InterruptedException { + assertFalse(q.tryTransfer(new Object(), + SHORT_DELAY_MS, MILLISECONDS)); }}); Thread.sleep(SMALL_DELAY_MS); @@ -1099,16 +1071,15 @@ public class LinkedTransferQueueTest ext assertTrue(q.offer(four)); Thread t = newStartedThread(new CheckedRunnable() { - void realRun() throws InterruptedException { - threadAssertTrue(q.tryTransfer(five, - MEDIUM_DELAY_MS, MILLISECONDS)); - threadAssertTrue(q.isEmpty()); + public void realRun() throws InterruptedException { + assertTrue(q.tryTransfer(five, MEDIUM_DELAY_MS, MILLISECONDS)); + assertTrue(q.isEmpty()); }}); Thread.sleep(SHORT_DELAY_MS); assertEquals(2, q.size()); - assertEquals(four, q.poll()); - assertEquals(five, q.poll()); + assertSame(four, q.poll()); + assertSame(five, q.poll()); checkEmpty(q); t.join(); } @@ -1123,7 +1094,7 @@ public class LinkedTransferQueueTest ext assertEquals(1, q.size()); assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS)); assertEquals(1, q.size()); - assertEquals(four, q.poll()); + assertSame(four, q.poll()); assertNull(q.poll()); checkEmpty(q); }