--- jsr166/src/test/tck/ArrayBlockingQueueTest.java 2009/11/22 00:17:37 1.25 +++ jsr166/src/test/tck/ArrayBlockingQueueTest.java 2010/10/09 19:30:34 1.33 @@ -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()); } /** @@ -145,7 +161,7 @@ public class ArrayBlockingQueueTest exte } /** - * offer(null) throws NPE + * offer(null) throws NPE */ public void testOfferNull() { try { @@ -156,7 +172,7 @@ public class ArrayBlockingQueueTest exte } /** - * add(null) throws NPE + * add(null) throws NPE */ public void testAddNull() { try { @@ -191,7 +207,7 @@ public class ArrayBlockingQueueTest exte } /** - * addAll(null) throws NPE + * addAll(null) throws NPE */ public void testAddAll1() { try { @@ -214,7 +230,7 @@ public class ArrayBlockingQueueTest exte /** - * addAll of a collection with null elements throws NPE + * addAll of a collection with null elements throws NPE */ public void testAddAll2() { try { @@ -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 */ @@ -267,7 +286,7 @@ public class ArrayBlockingQueueTest exte } /** - * put(null) throws NPE + * put(null) throws NPE */ public void testPutNull() throws InterruptedException { try { @@ -319,30 +338,26 @@ public class ArrayBlockingQueueTest exte * 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); } /** @@ -473,39 +488,15 @@ public class ArrayBlockingQueueTest exte } /** - * 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 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(); - } - - - /** * 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()); } @@ -516,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(); @@ -531,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(); @@ -561,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))); } } @@ -632,7 +623,7 @@ public class ArrayBlockingQueueTest exte } /** - * toArray contains all elements + * toArray contains all elements */ public void testToArray() throws InterruptedException { ArrayBlockingQueue q = populatedQueue(SIZE); @@ -656,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) {} @@ -667,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) {} } @@ -689,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); @@ -700,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()); } @@ -718,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); } @@ -727,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); @@ -762,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); - } /** @@ -785,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() {