--- jsr166/src/test/tck/ArrayBlockingQueueTest.java 2009/11/21 17:38:05 1.20 +++ jsr166/src/test/tck/ArrayBlockingQueueTest.java 2010/10/06 07:49:22 1.32 @@ -14,11 +14,27 @@ import static java.util.concurrent.TimeU import java.io.*; public class ArrayBlockingQueueTest extends JSR166TestCase { + + public static class Fair extends BlockingQueueTest { + protected BlockingQueue emptyCollection() { + return new ArrayBlockingQueue(20, true); + } + } + + public static class NonFair extends BlockingQueueTest { + protected BlockingQueue emptyCollection() { + return new ArrayBlockingQueue(20, false); + } + } + public static void main(String[] args) { - junit.textui.TestRunner.run (suite()); + junit.textui.TestRunner.run(suite()); } + public static Test suite() { - return new TestSuite(ArrayBlockingQueueTest.class); + return newTestSuite(ArrayBlockingQueueTest.class, + new Fair().testSuite(), + new NonFair().testSuite()); } /** @@ -224,6 +240,7 @@ public class ArrayBlockingQueueTest exte shouldThrow(); } catch (NullPointerException success) {} } + /** * addAll of a collection with any null elements throws NPE after * possibly adding some elements @@ -238,6 +255,7 @@ public class ArrayBlockingQueueTest exte shouldThrow(); } catch (NullPointerException success) {} } + /** * addAll throws ISE if not enough room */ @@ -251,6 +269,7 @@ public class ArrayBlockingQueueTest exte shouldThrow(); } catch (IllegalStateException success) {} } + /** * Queue contains all elements, in traversal order, of successful addAll */ @@ -296,53 +315,49 @@ public class ArrayBlockingQueueTest exte public void testBlockingPut() throws InterruptedException { final ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); Thread t = new Thread(new CheckedRunnable() { - public void realRun() { - int added = 0; + public void realRun() throws InterruptedException { + for (int i = 0; i < SIZE; ++i) + q.put(i); + assertEquals(SIZE, q.size()); + assertEquals(0, q.remainingCapacity()); try { - for (int i = 0; i < SIZE; ++i) { - q.put(new Integer(i)); - ++added; - } - q.put(new Integer(SIZE)); - threadShouldThrow(); - } catch (InterruptedException success) { - threadAssertEquals(added, SIZE); - }}}); + q.put(99); + shouldThrow(); + } catch (InterruptedException success) {} + }}); t.start(); - Thread.sleep(MEDIUM_DELAY_MS); + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); + assertEquals(SIZE, q.size()); + assertEquals(0, q.remainingCapacity()); } /** * put blocks waiting for take when full */ public void testPutWithTake() throws InterruptedException { - final ArrayBlockingQueue q = new ArrayBlockingQueue(2); + final int capacity = 2; + final ArrayBlockingQueue q = new ArrayBlockingQueue(capacity); Thread t = new Thread(new CheckedRunnable() { - public void realRun() { - int added = 0; + public void realRun() throws InterruptedException { + for (int i = 0; i < capacity + 1; i++) + q.put(i); try { - q.put(new Object()); - ++added; - q.put(new Object()); - ++added; - q.put(new Object()); - ++added; - q.put(new Object()); - ++added; - threadShouldThrow(); - } catch (InterruptedException success) { - threadAssertTrue(added >= 2); - } + q.put(99); + shouldThrow(); + } catch (InterruptedException success) {} }}); t.start(); Thread.sleep(SHORT_DELAY_MS); - q.take(); + assertEquals(q.remainingCapacity(), 0); + assertEquals(0, q.take()); + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); + assertEquals(q.remainingCapacity(), 0); } /** @@ -350,13 +365,16 @@ public class ArrayBlockingQueueTest exte */ public void testTimedOffer() throws InterruptedException { final ArrayBlockingQueue q = new ArrayBlockingQueue(2); - Thread t = new ThreadShouldThrow(InterruptedException.class) { + Thread t = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { q.put(new Object()); q.put(new Object()); - threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, MILLISECONDS)); - q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); - }}; + assertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, MILLISECONDS)); + try { + q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + }}); t.start(); Thread.sleep(SHORT_DELAY_MS); @@ -370,7 +388,7 @@ public class ArrayBlockingQueueTest exte public void testTake() throws InterruptedException { ArrayBlockingQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.take()).intValue()); + assertEquals(i, q.take()); } } @@ -394,19 +412,22 @@ public class ArrayBlockingQueueTest exte * Take removes existing elements until empty, then blocks interruptibly */ public void testBlockingTake() throws InterruptedException { - Thread t = new ThreadShouldThrow(InterruptedException.class) { + final ArrayBlockingQueue q = populatedQueue(SIZE); + Thread t = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { - ArrayBlockingQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - threadAssertEquals(i, ((Integer)q.take()).intValue()); + assertEquals(i, q.take()); } - q.take(); - }}; + try { + q.take(); + shouldThrow(); + } catch (InterruptedException success) {} + }}); t.start(); - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); + Thread.sleep(SHORT_DELAY_MS); + t.interrupt(); + t.join(); } @@ -416,7 +437,7 @@ public class ArrayBlockingQueueTest exte public void testPoll() { ArrayBlockingQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.poll()).intValue()); + assertEquals(i, q.poll()); } assertNull(q.poll()); } @@ -427,7 +448,7 @@ public class ArrayBlockingQueueTest exte public void testTimedPoll0() throws InterruptedException { ArrayBlockingQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue()); + assertEquals(i, q.poll(0, MILLISECONDS)); } assertNull(q.poll(0, MILLISECONDS)); } @@ -438,7 +459,7 @@ public class ArrayBlockingQueueTest exte public void testTimedPoll() throws InterruptedException { ArrayBlockingQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); + assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); } assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); } @@ -448,52 +469,34 @@ public class ArrayBlockingQueueTest exte * returning timeout status */ public void testInterruptedTimedPoll() throws InterruptedException { - Thread t = new ThreadShouldThrow(InterruptedException.class) { + Thread t = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { ArrayBlockingQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); + assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));; } - q.poll(MEDIUM_DELAY_MS, MILLISECONDS); - }}; - - t.start(); - Thread.sleep(SMALL_DELAY_MS); - t.interrupt(); - t.join(); - } - - /** - * timed poll before a delayed offer fails; after offer succeeds; - * on interruption throws - */ - public void testTimedPollWithOffer() throws InterruptedException { - final ArrayBlockingQueue q = new ArrayBlockingQueue(2); - Thread t = new ThreadShouldThrow(InterruptedException.class) { - public void realRun() throws InterruptedException { - threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); - q.poll(LONG_DELAY_MS, MILLISECONDS); - q.poll(LONG_DELAY_MS, MILLISECONDS); - }}; + try { + q.poll(SMALL_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + }}); t.start(); - Thread.sleep(SMALL_DELAY_MS); - assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); } - /** * peek returns next element, or null if empty */ public void testPeek() { ArrayBlockingQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.peek()).intValue()); - q.poll(); + assertEquals(i, q.peek()); + assertEquals(i, q.poll()); assertTrue(q.peek() == null || - i != ((Integer)q.peek()).intValue()); + !q.peek().equals(i)); } assertNull(q.peek()); } @@ -504,8 +507,8 @@ public class ArrayBlockingQueueTest exte public void testElement() { ArrayBlockingQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.element()).intValue()); - q.poll(); + assertEquals(i, q.element()); + assertEquals(i, q.poll()); } try { q.element(); @@ -519,7 +522,7 @@ public class ArrayBlockingQueueTest exte public void testRemove() { ArrayBlockingQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.remove()).intValue()); + assertEquals(i, q.remove()); } try { q.remove(); @@ -549,7 +552,7 @@ public class ArrayBlockingQueueTest exte ArrayBlockingQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { assertTrue(q.contains(new Integer(i))); - q.poll(); + assertEquals(i, q.poll()); assertFalse(q.contains(new Integer(i))); } } @@ -644,8 +647,8 @@ public class ArrayBlockingQueueTest exte * toArray(null) throws NPE */ public void testToArray_BadArg() { + ArrayBlockingQueue q = populatedQueue(SIZE); try { - ArrayBlockingQueue q = populatedQueue(SIZE); Object o[] = q.toArray(null); shouldThrow(); } catch (NullPointerException success) {} @@ -655,9 +658,9 @@ public class ArrayBlockingQueueTest exte * toArray with incompatible array type throws CCE */ public void testToArray1_BadArg() { + ArrayBlockingQueue q = populatedQueue(SIZE); try { - ArrayBlockingQueue q = populatedQueue(SIZE); - Object o[] = q.toArray(new String[10] ); + Object o[] = q.toArray(new String[10]); shouldThrow(); } catch (ArrayStoreException success) {} } @@ -677,7 +680,7 @@ public class ArrayBlockingQueueTest exte /** * iterator.remove removes current element */ - public void testIteratorRemove () { + public void testIteratorRemove() { final ArrayBlockingQueue q = new ArrayBlockingQueue(3); q.add(two); q.add(one); @@ -688,8 +691,8 @@ public class ArrayBlockingQueueTest exte it.remove(); it = q.iterator(); - assertEquals(it.next(), one); - assertEquals(it.next(), three); + assertSame(it.next(), one); + assertSame(it.next(), three); assertFalse(it.hasNext()); } @@ -706,8 +709,7 @@ public class ArrayBlockingQueueTest exte int k = 0; for (Iterator it = q.iterator(); it.hasNext();) { - int i = ((Integer)(it.next())).intValue(); - assertEquals(++k, i); + assertEquals(++k, it.next()); } assertEquals(3, k); } @@ -715,7 +717,7 @@ public class ArrayBlockingQueueTest exte /** * Modifications do not cause iterators to fail */ - public void testWeaklyConsistentIteration () { + public void testWeaklyConsistentIteration() { final ArrayBlockingQueue q = new ArrayBlockingQueue(3); q.add(one); q.add(two); @@ -750,19 +752,18 @@ public class ArrayBlockingQueueTest exte ExecutorService executor = Executors.newFixedThreadPool(2); executor.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { - threadAssertFalse(q.offer(three)); - threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); - threadAssertEquals(0, q.remainingCapacity()); + assertFalse(q.offer(three)); + assertTrue(q.offer(three, 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); - } /** @@ -773,9 +774,9 @@ public class ArrayBlockingQueueTest exte 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() {