--- jsr166/src/test/tck/LinkedTransferQueueTest.java 2009/08/15 00:35:01 1.12 +++ jsr166/src/test/tck/LinkedTransferQueueTest.java 2009/11/26 15:42:15 1.21 @@ -13,7 +13,6 @@ import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.ArrayList; import java.util.Arrays; -import java.util.ConcurrentModificationException; import java.util.Iterator; import java.util.List; import java.util.NoSuchElementException; @@ -45,18 +44,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) {} } /** @@ -76,8 +72,7 @@ public class LinkedTransferQueueTest ext try { new LinkedTransferQueue(null); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** @@ -89,8 +84,7 @@ public class LinkedTransferQueueTest ext Integer[] ints = new Integer[SIZE]; new LinkedTransferQueue(Arrays.asList(ints)); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** @@ -105,8 +99,7 @@ public class LinkedTransferQueueTest ext } new LinkedTransferQueue(Arrays.asList(ints)); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** @@ -158,8 +151,7 @@ public class LinkedTransferQueueTest ext LinkedTransferQueue q = new LinkedTransferQueue(); q.offer(null); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** @@ -170,8 +162,7 @@ public class LinkedTransferQueueTest ext LinkedTransferQueue q = new LinkedTransferQueue(); q.add(null); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** @@ -182,8 +173,7 @@ public class LinkedTransferQueueTest ext LinkedTransferQueue q = new LinkedTransferQueue(); q.addAll(null); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** @@ -194,8 +184,7 @@ public class LinkedTransferQueueTest ext LinkedTransferQueue q = populatedQueue(SIZE); q.addAll(q); shouldThrow(); - } catch (IllegalArgumentException success) { - } + } catch (IllegalArgumentException success) {} } /** @@ -207,8 +196,7 @@ public class LinkedTransferQueueTest ext Integer[] ints = new Integer[SIZE]; q.addAll(Arrays.asList(ints)); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** @@ -224,8 +212,7 @@ public class LinkedTransferQueueTest ext } q.addAll(Arrays.asList(ints)); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** @@ -284,7 +271,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); @@ -297,14 +284,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,8 +347,8 @@ public class LinkedTransferQueueTest ext */ public void testInterruptedTimedPoll() throws InterruptedException { final LinkedTransferQueue q = populatedQueue(SIZE); - Thread t = newStartedThread(new CheckedInterruptedRunnable() { - void realRun() throws InterruptedException { + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { for (int i = 0; i < SIZE; ++i) { long t0 = System.nanoTime(); threadAssertEquals(i, (int) q.poll(LONG_DELAY_MS, @@ -364,8 +356,12 @@ public class LinkedTransferQueueTest ext long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024); assertTrue(millisElapsed < SMALL_DELAY_MS); } - q.poll(LONG_DELAY_MS, MILLISECONDS); + try { + q.poll(LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} }}); + Thread.sleep(SMALL_DELAY_MS); t.interrupt(); t.join(); @@ -378,12 +374,17 @@ public class LinkedTransferQueueTest ext */ 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 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(); @@ -417,8 +418,7 @@ public class LinkedTransferQueueTest ext try { q.element(); shouldThrow(); - } catch (NoSuchElementException success) { - } + } catch (NoSuchElementException success) {} checkEmpty(q); } @@ -433,8 +433,7 @@ public class LinkedTransferQueueTest ext try { q.remove(); shouldThrow(); - } catch (NoSuchElementException success) { - } + } catch (NoSuchElementException success) {} checkEmpty(q); } @@ -545,7 +544,7 @@ public class LinkedTransferQueueTest ext } /** - * toArray contains all elements + * toArray() contains all elements */ public void testToArray() throws InterruptedException { LinkedTransferQueue q = populatedQueue(SIZE); @@ -571,24 +570,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 with incompatible array type throws CCE + * 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) {} } /** @@ -677,13 +674,13 @@ public class LinkedTransferQueueTest ext ExecutorService executor = Executors.newFixedThreadPool(2); executor.execute(new CheckedRunnable() { - void realRun() { + public void realRun() { threadAssertTrue(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()); }}); @@ -692,14 +689,14 @@ public class LinkedTransferQueueTest ext } /** - * poll retrieves elements across Executor threads + * timed poll retrieves elements across Executor threads */ public void testPollInExecutor() { final LinkedTransferQueue q = new LinkedTransferQueue(); ExecutorService executor = Executors.newFixedThreadPool(2); executor.execute(new CheckedRunnable() { - void realRun() throws InterruptedException { + public void realRun() throws InterruptedException { threadAssertNull(q.poll()); threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); @@ -707,7 +704,7 @@ public class LinkedTransferQueueTest ext }}); executor.execute(new CheckedRunnable() { - void realRun() throws InterruptedException { + public void realRun() throws InterruptedException { Thread.sleep(SMALL_DELAY_MS); q.put(one); }}); @@ -747,8 +744,7 @@ public class LinkedTransferQueueTest ext try { q.drainTo(null); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** @@ -759,8 +755,7 @@ public class LinkedTransferQueueTest ext try { q.drainTo(q); shouldThrow(); - } catch (IllegalArgumentException success) { - } + } catch (IllegalArgumentException success) {} } /** @@ -790,12 +785,12 @@ public class LinkedTransferQueueTest ext } /** - * drainTo empties full queue, unblocking a waiting put. + * drainTo(c) empties full queue, unblocking a waiting put. */ 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(); @@ -816,8 +811,7 @@ public class LinkedTransferQueueTest ext try { q.drainTo(null, SIZE); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** @@ -828,8 +822,7 @@ public class LinkedTransferQueueTest ext try { q.drainTo(q, SIZE); shouldThrow(); - } catch (IllegalArgumentException success) { - } + } catch (IllegalArgumentException success) {} } /** @@ -855,23 +848,27 @@ public class LinkedTransferQueueTest ext } /** - * poll and take decrement the waiting consumer count + * timed poll() or take() increments the waiting consumer count; + * offer(e) decrements the waiting consumer count */ public void testWaitingConsumer() throws InterruptedException { final LinkedTransferQueue q = new LinkedTransferQueue(); - final ConsumerObserver waiting = new ConsumerObserver(); + assertEquals(q.getWaitingConsumerCount(), 0); + 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()); - waiting.setWaitingConsumer(q.getWaitingConsumerCount()); + threadAssertEquals(q.getWaitingConsumerCount(), 1); threadAssertTrue(q.offer(new Object())); + threadAssertFalse(q.hasWaitingConsumer()); + threadAssertEquals(q.getWaitingConsumerCount(), 0); }}); assertTrue(q.poll(LONG_DELAY_MS, MILLISECONDS) != null); - assertTrue(q.getWaitingConsumerCount() - < waiting.getWaitingConsumers()); + assertEquals(q.getWaitingConsumerCount(), 0); + assertFalse(q.hasWaitingConsumer()); t.join(); } @@ -883,8 +880,7 @@ public class LinkedTransferQueueTest ext LinkedTransferQueue q = new LinkedTransferQueue(); q.transfer(null); shouldThrow(); - } catch (NullPointerException ex) { - } + } catch (NullPointerException success) {} } /** @@ -896,7 +892,7 @@ 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()); }}); @@ -916,7 +912,7 @@ 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)); @@ -925,7 +921,7 @@ public class LinkedTransferQueueTest ext Thread interruptedThread = newStartedThread( new CheckedInterruptedRunnable() { - void realRun() throws InterruptedException { + public void realRun() throws InterruptedException { while (q.size() == 0) Thread.yield(); q.transfer(SIZE); @@ -951,7 +947,7 @@ 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()); @@ -972,7 +968,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); }}); @@ -991,8 +987,7 @@ public class LinkedTransferQueueTest ext final LinkedTransferQueue q = new LinkedTransferQueue(); q.tryTransfer(null); shouldThrow(); - } catch (NullPointerException ex) { - } + } catch (NullPointerException success) {} } /** @@ -1015,7 +1010,7 @@ 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()); @@ -1038,7 +1033,7 @@ 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()); @@ -1060,7 +1055,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); }}); @@ -1076,7 +1071,7 @@ public class LinkedTransferQueueTest ext final LinkedTransferQueue q = new LinkedTransferQueue(); Thread t = newStartedThread(new CheckedRunnable() { - void realRun() throws InterruptedException { + public void realRun() throws InterruptedException { threadAssertFalse (q.tryTransfer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); @@ -1096,7 +1091,7 @@ public class LinkedTransferQueueTest ext assertTrue(q.offer(four)); Thread t = newStartedThread(new CheckedRunnable() { - void realRun() throws InterruptedException { + public void realRun() throws InterruptedException { threadAssertTrue(q.tryTransfer(five, MEDIUM_DELAY_MS, MILLISECONDS)); threadAssertTrue(q.isEmpty()); @@ -1121,8 +1116,8 @@ public class LinkedTransferQueueTest ext assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS)); assertEquals(1, q.size()); assertEquals(four, q.poll()); - checkEmpty(q); assertNull(q.poll()); + checkEmpty(q); } private LinkedTransferQueue populatedQueue(int n) { @@ -1136,20 +1131,4 @@ public class LinkedTransferQueueTest ext assertFalse(q.isEmpty()); return q; } - - private static class ConsumerObserver { - - private int waitingConsumers; - - private ConsumerObserver() { - } - - private void setWaitingConsumer(int i) { - this.waitingConsumers = i; - } - - private int getWaitingConsumers() { - return waitingConsumers; - } - } }