--- jsr166/src/test/tck/ArrayBlockingQueueTest.java 2011/05/31 16:16:23 1.49 +++ jsr166/src/test/tck/ArrayBlockingQueueTest.java 2015/10/05 23:06:12 1.69 @@ -6,9 +6,10 @@ * Pat Fisher, Mike Judd. */ -import junit.framework.*; -import java.util.Arrays; +import static java.util.concurrent.TimeUnit.MILLISECONDS; + import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Iterator; import java.util.NoSuchElementException; @@ -18,24 +19,25 @@ import java.util.concurrent.BlockingQueu import java.util.concurrent.CountDownLatch; import java.util.concurrent.Executors; import java.util.concurrent.ExecutorService; -import static java.util.concurrent.TimeUnit.MILLISECONDS; + +import junit.framework.Test; public class ArrayBlockingQueueTest extends JSR166TestCase { public static class Fair extends BlockingQueueTest { protected BlockingQueue emptyCollection() { - return new ArrayBlockingQueue(20, true); + return new ArrayBlockingQueue(SIZE, true); } } public static class NonFair extends BlockingQueueTest { protected BlockingQueue emptyCollection() { - return new ArrayBlockingQueue(20, false); + return new ArrayBlockingQueue(SIZE, false); } } public static void main(String[] args) { - junit.textui.TestRunner.run(suite()); + main(suite(), args); } public static Test suite() { @@ -45,7 +47,7 @@ public class ArrayBlockingQueueTest exte } /** - * Create a queue of given size containing consecutive + * Returns a new queue of given size containing consecutive * Integers 0 ... n. */ private ArrayBlockingQueue populatedQueue(int n) { @@ -102,11 +104,11 @@ public class ArrayBlockingQueueTest exte */ public void testConstructor5() { Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE-1; ++i) + for (int i = 0; i < SIZE - 1; ++i) ints[i] = i; Collection elements = Arrays.asList(ints); try { - new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints)); + new ArrayBlockingQueue(SIZE, false, elements); shouldThrow(); } catch (NullPointerException success) {} } @@ -157,16 +159,16 @@ public class ArrayBlockingQueueTest exte * remainingCapacity decreases on add, increases on remove */ public void testRemainingCapacity() { - ArrayBlockingQueue q = populatedQueue(SIZE); + BlockingQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { assertEquals(i, q.remainingCapacity()); - assertEquals(SIZE-i, q.size()); - q.remove(); + assertEquals(SIZE, q.size() + q.remainingCapacity()); + assertEquals(i, q.remove()); } for (int i = 0; i < SIZE; ++i) { - assertEquals(SIZE-i, q.remainingCapacity()); - assertEquals(i, q.size()); - q.add(new Integer(i)); + assertEquals(SIZE - i, q.remainingCapacity()); + assertEquals(SIZE, q.size() + q.remainingCapacity()); + assertTrue(q.add(i)); } } @@ -183,12 +185,12 @@ public class ArrayBlockingQueueTest exte * add succeeds if not full; throws ISE if full */ public void testAdd() { + ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); + for (int i = 0; i < SIZE; ++i) { + assertTrue(q.add(new Integer(i))); + } + assertEquals(0, q.remainingCapacity()); try { - ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); - for (int i = 0; i < SIZE; ++i) { - assertTrue(q.add(new Integer(i))); - } - assertEquals(0, q.remainingCapacity()); q.add(new Integer(SIZE)); shouldThrow(); } catch (IllegalStateException success) {} @@ -198,8 +200,8 @@ public class ArrayBlockingQueueTest exte * addAll(this) throws IAE */ public void testAddAllSelf() { + ArrayBlockingQueue q = populatedQueue(SIZE); try { - ArrayBlockingQueue q = populatedQueue(SIZE); q.addAll(q); shouldThrow(); } catch (IllegalArgumentException success) {} @@ -210,11 +212,11 @@ public class ArrayBlockingQueueTest exte * possibly adding some elements */ public void testAddAll3() { + ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); + Integer[] ints = new Integer[SIZE]; + for (int i = 0; i < SIZE - 1; ++i) + ints[i] = new Integer(i); try { - ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); - Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE-1; ++i) - ints[i] = new Integer(i); q.addAll(Arrays.asList(ints)); shouldThrow(); } catch (NullPointerException success) {} @@ -224,11 +226,11 @@ public class ArrayBlockingQueueTest exte * addAll throws ISE if not enough room */ public void testAddAll4() { + ArrayBlockingQueue q = new ArrayBlockingQueue(1); + Integer[] ints = new Integer[SIZE]; + for (int i = 0; i < SIZE; ++i) + ints[i] = new Integer(i); try { - ArrayBlockingQueue q = new ArrayBlockingQueue(1); - Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE; ++i) - ints[i] = new Integer(i); q.addAll(Arrays.asList(ints)); shouldThrow(); } catch (IllegalStateException success) {} @@ -255,9 +257,9 @@ public class ArrayBlockingQueueTest exte public void testPut() throws InterruptedException { ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - Integer I = new Integer(i); - q.put(I); - assertTrue(q.contains(I)); + Integer x = new Integer(i); + q.put(x); + assertTrue(q.contains(x)); } assertEquals(0, q.remainingCapacity()); } @@ -322,14 +324,14 @@ public class ArrayBlockingQueueTest exte }}); await(pleaseTake); - assertEquals(q.remainingCapacity(), 0); + assertEquals(0, q.remainingCapacity()); assertEquals(0, q.take()); await(pleaseInterrupt); assertThreadStaysAlive(t); t.interrupt(); awaitTermination(t); - assertEquals(q.remainingCapacity(), 0); + assertEquals(0, q.remainingCapacity()); } /** @@ -450,24 +452,24 @@ public class ArrayBlockingQueueTest exte Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { for (int i = 0; i < SIZE; ++i) { - long t0 = System.nanoTime(); - assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); - assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); + long startTime = System.nanoTime(); + assertEquals(i, (int) q.poll(2*LONG_DELAY_MS, MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); } - long t0 = System.nanoTime(); + long startTime = System.nanoTime(); aboutToWait.countDown(); try { - q.poll(MEDIUM_DELAY_MS, MILLISECONDS); + q.poll(2*LONG_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (InterruptedException success) { - assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); } }}); - aboutToWait.await(); - waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); + await(aboutToWait); + waitForThreadToEnterWaitState(t, LONG_DELAY_MS); t.interrupt(); - awaitTermination(t, MEDIUM_DELAY_MS); + awaitTermination(t, LONG_DELAY_MS); checkEmpty(q); } @@ -515,21 +517,6 @@ public class ArrayBlockingQueueTest exte } /** - * remove(x) removes x and returns true if present - */ - public void testRemoveElement() { - ArrayBlockingQueue q = populatedQueue(SIZE); - for (int i = 1; i < SIZE; i+=2) { - assertTrue(q.remove(new Integer(i))); - } - for (int i = 0; i < SIZE; i+=2) { - assertTrue(q.remove(new Integer(i))); - assertFalse(q.remove(new Integer(i+1))); - } - assertTrue(q.isEmpty()); - } - - /** * contains(x) reports true when elements added but not yet removed */ public void testContains() { @@ -585,7 +572,7 @@ public class ArrayBlockingQueueTest exte assertTrue(changed); assertTrue(q.containsAll(p)); - assertEquals(SIZE-i, q.size()); + assertEquals(SIZE - i, q.size()); p.remove(); } } @@ -598,34 +585,100 @@ public class ArrayBlockingQueueTest exte ArrayBlockingQueue q = populatedQueue(SIZE); ArrayBlockingQueue p = populatedQueue(i); assertTrue(q.removeAll(p)); - assertEquals(SIZE-i, q.size()); + assertEquals(SIZE - i, q.size()); for (int j = 0; j < i; ++j) { - Integer I = (Integer)(p.remove()); - assertFalse(q.contains(I)); + Integer x = (Integer)(p.remove()); + assertFalse(q.contains(x)); } } } + void checkToArray(ArrayBlockingQueue q) { + int size = q.size(); + Object[] o = q.toArray(); + assertEquals(size, o.length); + Iterator it = q.iterator(); + for (int i = 0; i < size; i++) { + Integer x = (Integer) it.next(); + assertEquals((Integer)o[0] + i, (int) x); + assertSame(o[i], x); + } + } + /** - * toArray contains all elements in FIFO order + * toArray() contains all elements in FIFO order */ public void testToArray() { - ArrayBlockingQueue q = populatedQueue(SIZE); - Object[] o = q.toArray(); - for (int i = 0; i < o.length; i++) - assertSame(o[i], q.poll()); + ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); + for (int i = 0; i < SIZE; i++) { + checkToArray(q); + q.add(i); + } + // Provoke wraparound + for (int i = 0; i < SIZE; i++) { + checkToArray(q); + assertEquals(i, q.poll()); + checkToArray(q); + q.add(SIZE + i); + } + for (int i = 0; i < SIZE; i++) { + checkToArray(q); + assertEquals(SIZE + i, q.poll()); + } + } + + void checkToArray2(ArrayBlockingQueue q) { + int size = q.size(); + Integer[] a1 = (size == 0) ? null : new Integer[size - 1]; + Integer[] a2 = new Integer[size]; + Integer[] a3 = new Integer[size + 2]; + if (size > 0) Arrays.fill(a1, 42); + Arrays.fill(a2, 42); + Arrays.fill(a3, 42); + Integer[] b1 = (size == 0) ? null : (Integer[]) q.toArray(a1); + Integer[] b2 = (Integer[]) q.toArray(a2); + Integer[] b3 = (Integer[]) q.toArray(a3); + assertSame(a2, b2); + assertSame(a3, b3); + Iterator it = q.iterator(); + for (int i = 0; i < size; i++) { + Integer x = (Integer) it.next(); + assertSame(b1[i], x); + assertEquals(b1[0] + i, (int) x); + assertSame(b2[i], x); + assertSame(b3[i], x); + } + assertNull(a3[size]); + assertEquals(42, (int) a3[size + 1]); + if (size > 0) { + assertNotSame(a1, b1); + assertEquals(size, b1.length); + for (int i = 0; i < a1.length; i++) { + assertEquals(42, (int) a1[i]); + } + } } /** * toArray(a) contains all elements in FIFO order */ public void testToArray2() { - ArrayBlockingQueue q = populatedQueue(SIZE); - Integer[] ints = new Integer[SIZE]; - Integer[] array = q.toArray(ints); - assertSame(ints, array); - for (int i = 0; i < ints.length; i++) - assertSame(ints[i], q.poll()); + ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); + for (int i = 0; i < SIZE; i++) { + checkToArray2(q); + q.add(i); + } + // Provoke wraparound + for (int i = 0; i < SIZE; i++) { + checkToArray2(q); + assertEquals(i, q.poll()); + checkToArray2(q); + q.add(SIZE + i); + } + for (int i = 0; i < SIZE; i++) { + checkToArray2(q); + assertEquals(SIZE + i, q.poll()); + } } /** @@ -645,9 +698,24 @@ public class ArrayBlockingQueueTest exte public void testIterator() throws InterruptedException { ArrayBlockingQueue q = populatedQueue(SIZE); Iterator it = q.iterator(); - while (it.hasNext()) { + int i; + for (i = 0; it.hasNext(); i++) + assertTrue(q.contains(it.next())); + assertEquals(i, SIZE); + assertIteratorExhausted(it); + + it = q.iterator(); + for (i = 0; it.hasNext(); i++) assertEquals(it.next(), q.take()); - } + assertEquals(i, SIZE); + assertIteratorExhausted(it); + } + + /** + * iterator of empty collection has no elements + */ + public void testEmptyIterator() { + assertIteratorExhausted(new ArrayBlockingQueue(SIZE).iterator()); } /** @@ -720,24 +788,24 @@ public class ArrayBlockingQueueTest exte final ArrayBlockingQueue q = new ArrayBlockingQueue(2); q.add(one); q.add(two); - ExecutorService executor = Executors.newFixedThreadPool(2); final CheckedBarrier threadsStarted = new CheckedBarrier(2); - executor.execute(new CheckedRunnable() { - public void realRun() throws InterruptedException { - assertFalse(q.offer(three)); - threadsStarted.await(); - assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); - assertEquals(0, q.remainingCapacity()); - }}); - - executor.execute(new CheckedRunnable() { - public void realRun() throws InterruptedException { - threadsStarted.await(); - assertEquals(0, q.remainingCapacity()); - assertSame(one, q.take()); - }}); - - joinPool(executor); + final ExecutorService executor = Executors.newFixedThreadPool(2); + try (PoolCleaner cleaner = cleaner(executor)) { + executor.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { + assertFalse(q.offer(three)); + threadsStarted.await(); + assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); + assertEquals(0, q.remainingCapacity()); + }}); + + executor.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { + threadsStarted.await(); + assertEquals(0, q.remainingCapacity()); + assertSame(one, q.take()); + }}); + } } /** @@ -746,22 +814,22 @@ public class ArrayBlockingQueueTest exte public void testPollInExecutor() { final ArrayBlockingQueue q = new ArrayBlockingQueue(2); final CheckedBarrier threadsStarted = new CheckedBarrier(2); - ExecutorService executor = Executors.newFixedThreadPool(2); - executor.execute(new CheckedRunnable() { - public void realRun() throws InterruptedException { - assertNull(q.poll()); - threadsStarted.await(); - assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); - checkEmpty(q); - }}); - - executor.execute(new CheckedRunnable() { - public void realRun() throws InterruptedException { - threadsStarted.await(); - q.put(one); - }}); - - joinPool(executor); + final ExecutorService executor = Executors.newFixedThreadPool(2); + try (PoolCleaner cleaner = cleaner(executor)) { + executor.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { + assertNull(q.poll()); + threadsStarted.await(); + assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); + checkEmpty(q); + }}); + + executor.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { + threadsStarted.await(); + q.put(one); + }}); + } } /** @@ -771,7 +839,7 @@ public class ArrayBlockingQueueTest exte Queue x = populatedQueue(SIZE); Queue y = serialClone(x); - assertTrue(x != y); + assertNotSame(x, y); assertEquals(x.size(), y.size()); assertEquals(x.toString(), y.toString()); assertTrue(Arrays.equals(x.toArray(), y.toArray())); @@ -789,8 +857,8 @@ public class ArrayBlockingQueueTest exte ArrayBlockingQueue q = populatedQueue(SIZE); ArrayList l = new ArrayList(); q.drainTo(l); - assertEquals(q.size(), 0); - assertEquals(l.size(), SIZE); + assertEquals(0, q.size()); + assertEquals(SIZE, l.size()); for (int i = 0; i < SIZE; ++i) assertEquals(l.get(i), new Integer(i)); q.add(zero); @@ -800,8 +868,8 @@ public class ArrayBlockingQueueTest exte assertTrue(q.contains(one)); l.clear(); q.drainTo(l); - assertEquals(q.size(), 0); - assertEquals(l.size(), 2); + assertEquals(0, q.size()); + assertEquals(2, l.size()); for (int i = 0; i < 2; ++i) assertEquals(l.get(i), new Integer(i)); } @@ -813,7 +881,7 @@ public class ArrayBlockingQueueTest exte final ArrayBlockingQueue q = populatedQueue(SIZE); Thread t = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { - q.put(new Integer(SIZE+1)); + q.put(new Integer(SIZE + 1)); }}); t.start(); @@ -830,19 +898,33 @@ public class ArrayBlockingQueueTest exte * drainTo(c, n) empties first min(n, size) elements of queue into c */ public void testDrainToN() { - ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE*2); + ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE * 2); for (int i = 0; i < SIZE + 2; ++i) { for (int j = 0; j < SIZE; j++) assertTrue(q.offer(new Integer(j))); ArrayList l = new ArrayList(); q.drainTo(l, i); int k = (i < SIZE) ? i : SIZE; - assertEquals(l.size(), k); - assertEquals(q.size(), SIZE-k); + assertEquals(k, l.size()); + assertEquals(SIZE - k, q.size()); for (int j = 0; j < k; ++j) assertEquals(l.get(j), new Integer(j)); - while (q.poll() != null) ; + do {} while (q.poll() != null); } } + /** + * remove(null), contains(null) always return false + */ + public void testNeverContainsNull() { + Collection[] qs = { + new ArrayBlockingQueue(10), + populatedQueue(2), + }; + + for (Collection q : qs) { + assertFalse(q.contains(null)); + assertFalse(q.remove(null)); + } + } }