--- jsr166/src/test/tck/ArrayBlockingQueueTest.java 2010/08/25 01:44:48 1.30 +++ jsr166/src/test/tck/ArrayBlockingQueueTest.java 2010/11/05 00:17:22 1.41 @@ -14,19 +14,35 @@ 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()); } + public static Test suite() { - return new TestSuite(ArrayBlockingQueueTest.class); + return newTestSuite(ArrayBlockingQueueTest.class, + new Fair().testSuite(), + new NonFair().testSuite()); } /** * Create a queue of given size containing consecutive * Integers 0 ... n. */ - private ArrayBlockingQueue populatedQueue(int n) { - ArrayBlockingQueue q = new ArrayBlockingQueue(n); + private ArrayBlockingQueue populatedQueue(int n) { + ArrayBlockingQueue q = new ArrayBlockingQueue(n); assertTrue(q.isEmpty()); for (int i = 0; i < n; i++) assertTrue(q.offer(new Integer(i))); @@ -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 { @@ -270,7 +286,7 @@ public class ArrayBlockingQueueTest exte } /** - * put(null) throws NPE + * put(null) throws NPE */ public void testPutNull() throws InterruptedException { try { @@ -377,22 +393,6 @@ public class ArrayBlockingQueueTest exte } /** - * take blocks interruptibly when empty - */ - public void testTakeFromEmpty() throws InterruptedException { - final ArrayBlockingQueue q = new ArrayBlockingQueue(2); - Thread t = new ThreadShouldThrow(InterruptedException.class) { - public void realRun() throws InterruptedException { - q.take(); - }}; - - t.start(); - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); - } - - /** * Take removes existing elements until empty, then blocks interruptibly */ public void testBlockingTake() throws InterruptedException { @@ -427,7 +427,7 @@ public class ArrayBlockingQueueTest exte } /** - * timed pool with zero timeout succeeds when non-empty, else times out + * timed poll with zero timeout succeeds when non-empty, else times out */ public void testTimedPoll0() throws InterruptedException { ArrayBlockingQueue q = populatedQueue(SIZE); @@ -438,7 +438,7 @@ public class ArrayBlockingQueueTest exte } /** - * timed pool with nonzero timeout succeeds when non-empty, else times out + * timed poll with nonzero timeout succeeds when non-empty, else times out */ public void testTimedPoll() throws InterruptedException { ArrayBlockingQueue q = populatedQueue(SIZE); @@ -472,30 +472,6 @@ 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() { @@ -631,44 +607,45 @@ public class ArrayBlockingQueueTest exte } /** - * toArray contains all elements + * toArray contains all elements in FIFO order */ - public void testToArray() throws InterruptedException { + public void testToArray() { ArrayBlockingQueue q = populatedQueue(SIZE); Object[] o = q.toArray(); for (int i = 0; i < o.length; i++) - assertEquals(o[i], q.take()); + assertSame(o[i], q.poll()); } /** - * toArray(a) contains all elements + * toArray(a) contains all elements in FIFO order */ - public void testToArray2() throws InterruptedException { - ArrayBlockingQueue q = populatedQueue(SIZE); + public void testToArray2() { + ArrayBlockingQueue q = populatedQueue(SIZE); Integer[] ints = new Integer[SIZE]; - ints = (Integer[])q.toArray(ints); + Integer[] array = q.toArray(ints); + assertSame(ints, array); for (int i = 0; i < ints.length; i++) - assertEquals(ints[i], q.take()); + assertSame(ints[i], q.poll()); } /** - * toArray(null) throws NPE + * toArray(null) throws NullPointerException */ - public void testToArray_BadArg() { + public void testToArray_NullArg() { ArrayBlockingQueue q = populatedQueue(SIZE); try { - Object o[] = q.toArray(null); + q.toArray(null); shouldThrow(); } catch (NullPointerException success) {} } /** - * toArray with incompatible array type throws CCE + * toArray(incompatible array type) throws ArrayStoreException */ public void testToArray1_BadArg() { ArrayBlockingQueue q = populatedQueue(SIZE); try { - Object o[] = q.toArray(new String[10]); + q.toArray(new String[10]); shouldThrow(); } catch (ArrayStoreException success) {} } @@ -904,7 +881,7 @@ public class ArrayBlockingQueueTest exte } /** - * drainTo(c, n) empties first max {n, size} elements of queue into c + * drainTo(c, n) empties first min(n, size) elements of queue into c */ public void testDrainToN() { ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE*2); @@ -913,7 +890,7 @@ public class ArrayBlockingQueueTest exte assertTrue(q.offer(new Integer(j))); ArrayList l = new ArrayList(); q.drainTo(l, i); - int k = (i < SIZE)? i : SIZE; + int k = (i < SIZE) ? i : SIZE; assertEquals(l.size(), k); assertEquals(q.size(), SIZE-k); for (int j = 0; j < k; ++j)