--- jsr166/src/test/tck/ArrayBlockingQueueTest.java 2011/04/14 22:55:08 1.44 +++ jsr166/src/test/tck/ArrayBlockingQueueTest.java 2015/02/22 04:34:44 1.62 @@ -6,24 +6,33 @@ * Pat Fisher, Mike Judd. */ - -import junit.framework.*; -import java.util.*; -import java.util.concurrent.*; import static java.util.concurrent.TimeUnit.MILLISECONDS; -import java.io.*; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Iterator; +import java.util.NoSuchElementException; +import java.util.Queue; +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Executors; +import java.util.concurrent.ExecutorService; + +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); } } @@ -38,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) { @@ -64,7 +73,7 @@ public class ArrayBlockingQueueTest exte */ public void testConstructor2() { try { - ArrayBlockingQueue q = new ArrayBlockingQueue(0); + new ArrayBlockingQueue(0); shouldThrow(); } catch (IllegalArgumentException success) {} } @@ -74,7 +83,7 @@ public class ArrayBlockingQueueTest exte */ public void testConstructor3() { try { - ArrayBlockingQueue q = new ArrayBlockingQueue(1, true, null); + new ArrayBlockingQueue(1, true, null); shouldThrow(); } catch (NullPointerException success) {} } @@ -83,9 +92,9 @@ public class ArrayBlockingQueueTest exte * Initializing from Collection of null elements throws NPE */ public void testConstructor4() { + Collection elements = Arrays.asList(new Integer[SIZE]); try { - Integer[] ints = new Integer[SIZE]; - ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints)); + new ArrayBlockingQueue(SIZE, false, elements); shouldThrow(); } catch (NullPointerException success) {} } @@ -94,11 +103,12 @@ public class ArrayBlockingQueueTest exte * Initializing from Collection with some null elements throws NPE */ public void testConstructor5() { + Integer[] ints = new Integer[SIZE]; + for (int i = 0; i < SIZE-1; ++i) + ints[i] = i; + Collection elements = Arrays.asList(ints); try { - Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE-1; ++i) - ints[i] = new Integer(i); - ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints)); + new ArrayBlockingQueue(SIZE, false, elements); shouldThrow(); } catch (NullPointerException success) {} } @@ -107,11 +117,12 @@ public class ArrayBlockingQueueTest exte * Initializing from too large collection throws IAE */ public void testConstructor6() { + Integer[] ints = new Integer[SIZE]; + for (int i = 0; i < SIZE; ++i) + ints[i] = i; + Collection elements = Arrays.asList(ints); try { - Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE; ++i) - ints[i] = new Integer(i); - ArrayBlockingQueue q = new ArrayBlockingQueue(1, false, Arrays.asList(ints)); + new ArrayBlockingQueue(SIZE - 1, false, elements); shouldThrow(); } catch (IllegalArgumentException success) {} } @@ -122,8 +133,9 @@ public class ArrayBlockingQueueTest exte public void testConstructor7() { Integer[] ints = new Integer[SIZE]; for (int i = 0; i < SIZE; ++i) - ints[i] = new Integer(i); - ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, true, Arrays.asList(ints)); + ints[i] = i; + Collection elements = Arrays.asList(ints); + ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, true, elements); for (int i = 0; i < SIZE; ++i) assertEquals(ints[i], q.poll()); } @@ -161,28 +173,6 @@ public class ArrayBlockingQueueTest exte } /** - * offer(null) throws NPE - */ - public void testOfferNull() { - try { - ArrayBlockingQueue q = new ArrayBlockingQueue(1); - q.offer(null); - shouldThrow(); - } catch (NullPointerException success) {} - } - - /** - * add(null) throws NPE - */ - public void testAddNull() { - try { - ArrayBlockingQueue q = new ArrayBlockingQueue(1); - q.add(null); - shouldThrow(); - } catch (NullPointerException success) {} - } - - /** * Offer succeeds if not full; fails if full */ public void testOffer() { @@ -207,17 +197,6 @@ public class ArrayBlockingQueueTest exte } /** - * addAll(null) throws NPE - */ - public void testAddAll1() { - try { - ArrayBlockingQueue q = new ArrayBlockingQueue(1); - q.addAll(null); - shouldThrow(); - } catch (NullPointerException success) {} - } - - /** * addAll(this) throws IAE */ public void testAddAllSelf() { @@ -228,19 +207,6 @@ public class ArrayBlockingQueueTest exte } catch (IllegalArgumentException success) {} } - - /** - * addAll of a collection with null elements throws NPE - */ - public void testAddAll2() { - try { - ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); - Integer[] ints = new Integer[SIZE]; - q.addAll(Arrays.asList(ints)); - shouldThrow(); - } catch (NullPointerException success) {} - } - /** * addAll of a collection with any null elements throws NPE after * possibly adding some elements @@ -286,25 +252,14 @@ public class ArrayBlockingQueueTest exte } /** - * put(null) throws NPE - */ - public void testPutNull() throws InterruptedException { - try { - ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); - q.put(null); - shouldThrow(); - } catch (NullPointerException success) {} - } - - /** * all elements successfully put are contained */ 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()); } @@ -314,50 +269,69 @@ public class ArrayBlockingQueueTest exte */ public void testBlockingPut() throws InterruptedException { final ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); - Thread t = new Thread(new CheckedRunnable() { + final CountDownLatch pleaseInterrupt = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { for (int i = 0; i < SIZE; ++i) q.put(i); assertEquals(SIZE, q.size()); assertEquals(0, q.remainingCapacity()); + + Thread.currentThread().interrupt(); try { q.put(99); shouldThrow(); } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + + pleaseInterrupt.countDown(); + try { + q.put(99); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); }}); - t.start(); - Thread.sleep(SHORT_DELAY_MS); + await(pleaseInterrupt); + assertThreadStaysAlive(t); t.interrupt(); - t.join(); + awaitTermination(t); assertEquals(SIZE, q.size()); assertEquals(0, q.remainingCapacity()); } /** - * put blocks waiting for take when full + * put blocks interruptibly waiting for take when full */ public void testPutWithTake() throws InterruptedException { final int capacity = 2; final ArrayBlockingQueue q = new ArrayBlockingQueue(capacity); - Thread t = new Thread(new CheckedRunnable() { + final CountDownLatch pleaseTake = new CountDownLatch(1); + final CountDownLatch pleaseInterrupt = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { - for (int i = 0; i < capacity + 1; i++) + for (int i = 0; i < capacity; i++) q.put(i); + pleaseTake.countDown(); + q.put(86); + + pleaseInterrupt.countDown(); try { q.put(99); shouldThrow(); } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); }}); - t.start(); - Thread.sleep(SHORT_DELAY_MS); - assertEquals(q.remainingCapacity(), 0); + await(pleaseTake); + assertEquals(0, q.remainingCapacity()); assertEquals(0, q.take()); - Thread.sleep(SHORT_DELAY_MS); + + await(pleaseInterrupt); + assertThreadStaysAlive(t); t.interrupt(); - t.join(); - assertEquals(q.remainingCapacity(), 0); + awaitTermination(t); + assertEquals(0, q.remainingCapacity()); } /** @@ -365,21 +339,25 @@ public class ArrayBlockingQueueTest exte */ public void testTimedOffer() throws InterruptedException { final ArrayBlockingQueue q = new ArrayBlockingQueue(2); - Thread t = new Thread(new CheckedRunnable() { + final CountDownLatch pleaseInterrupt = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { q.put(new Object()); q.put(new Object()); - assertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, MILLISECONDS)); + long startTime = System.nanoTime(); + assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); + pleaseInterrupt.countDown(); try { - q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); + q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (InterruptedException success) {} }}); - t.start(); - Thread.sleep(SHORT_DELAY_MS); + await(pleaseInterrupt); + assertThreadStaysAlive(t); t.interrupt(); - t.join(); + awaitTermination(t); } /** @@ -397,24 +375,34 @@ public class ArrayBlockingQueueTest exte */ public void testBlockingTake() throws InterruptedException { final ArrayBlockingQueue q = populatedQueue(SIZE); - Thread t = new Thread(new CheckedRunnable() { + final CountDownLatch pleaseInterrupt = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { for (int i = 0; i < SIZE; ++i) { assertEquals(i, q.take()); } + + Thread.currentThread().interrupt(); + try { + q.take(); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + + pleaseInterrupt.countDown(); try { q.take(); shouldThrow(); } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); }}); - t.start(); - Thread.sleep(SHORT_DELAY_MS); + await(pleaseInterrupt); + assertThreadStaysAlive(t); t.interrupt(); - t.join(); + awaitTermination(t); } - /** * poll succeeds unless empty */ @@ -435,6 +423,7 @@ public class ArrayBlockingQueueTest exte assertEquals(i, q.poll(0, MILLISECONDS)); } assertNull(q.poll(0, MILLISECONDS)); + checkEmpty(q); } /** @@ -443,9 +432,14 @@ public class ArrayBlockingQueueTest exte public void testTimedPoll() throws InterruptedException { ArrayBlockingQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); - } - assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); + long startTime = System.nanoTime(); + assertEquals(i, q.poll(LONG_DELAY_MS, MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); + } + long startTime = System.nanoTime(); + assertNull(q.poll(timeoutMillis(), MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); + checkEmpty(q); } /** @@ -523,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() { @@ -608,43 +587,98 @@ public class ArrayBlockingQueueTest exte assertTrue(q.removeAll(p)); 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)); } } } - /** - * toArray contains all elements in FIFO order - */ - public void testToArray() { - ArrayBlockingQueue q = populatedQueue(SIZE); + void checkToArray(ArrayBlockingQueue q) { + int size = q.size(); Object[] o = q.toArray(); - for (int i = 0; i < o.length; i++) - assertSame(o[i], q.poll()); + 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(a) contains all elements in FIFO order + * toArray() 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()); + public void testToArray() { + 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(null) throws NullPointerException + * toArray(a) contains all elements in FIFO order */ - public void testToArray_NullArg() { - ArrayBlockingQueue q = populatedQueue(SIZE); - try { - q.toArray(null); - shouldThrow(); - } catch (NullPointerException success) {} + public void testToArray2() { + 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()); + } } /** @@ -658,16 +692,30 @@ public class ArrayBlockingQueueTest exte } catch (ArrayStoreException success) {} } - /** * iterator iterates through all elements */ 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()); } /** @@ -722,7 +770,6 @@ public class ArrayBlockingQueueTest exte assertEquals(0, q.size()); } - /** * toString contains toStrings of elements */ @@ -730,11 +777,10 @@ public class ArrayBlockingQueueTest exte ArrayBlockingQueue q = populatedQueue(SIZE); String s = q.toString(); for (int i = 0; i < SIZE; ++i) { - assertTrue(s.indexOf(String.valueOf(i)) >= 0); + assertTrue(s.contains(String.valueOf(i))); } } - /** * offer transfers elements across Executor tasks */ @@ -743,16 +789,19 @@ public class ArrayBlockingQueueTest exte 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)); - assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); + threadsStarted.await(); + assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); assertEquals(0, q.remainingCapacity()); }}); executor.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { - Thread.sleep(SMALL_DELAY_MS); + threadsStarted.await(); + assertEquals(0, q.remainingCapacity()); assertSame(one, q.take()); }}); @@ -760,21 +809,23 @@ public class ArrayBlockingQueueTest exte } /** - * poll retrieves elements across Executor threads + * timed poll retrieves elements across Executor threads */ 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()); - assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); - assertTrue(q.isEmpty()); + threadsStarted.await(); + assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); + checkEmpty(q); }}); executor.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { - Thread.sleep(SMALL_DELAY_MS); + threadsStarted.await(); q.put(one); }}); @@ -785,41 +836,18 @@ public class ArrayBlockingQueueTest exte * A deserialized serialized queue has same elements in same order */ public void testSerialization() throws Exception { - ArrayBlockingQueue q = populatedQueue(SIZE); - - ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); - ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); - out.writeObject(q); - out.close(); - - ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); - ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); - ArrayBlockingQueue r = (ArrayBlockingQueue)in.readObject(); - assertEquals(q.size(), r.size()); - while (!q.isEmpty()) - assertEquals(q.remove(), r.remove()); - } + Queue x = populatedQueue(SIZE); + Queue y = serialClone(x); - /** - * drainTo(null) throws NPE - */ - public void testDrainToNull() { - ArrayBlockingQueue q = populatedQueue(SIZE); - try { - q.drainTo(null); - shouldThrow(); - } catch (NullPointerException success) {} - } - - /** - * drainTo(this) throws IAE - */ - public void testDrainToSelf() { - ArrayBlockingQueue q = populatedQueue(SIZE); - try { - q.drainTo(q); - shouldThrow(); - } catch (IllegalArgumentException success) {} + assertNotSame(x, y); + assertEquals(x.size(), y.size()); + assertEquals(x.toString(), y.toString()); + assertTrue(Arrays.equals(x.toArray(), y.toArray())); + while (!x.isEmpty()) { + assertFalse(y.isEmpty()); + assertEquals(x.remove(), y.remove()); + } + assertTrue(y.isEmpty()); } /** @@ -829,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); @@ -840,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)); } @@ -867,28 +895,6 @@ public class ArrayBlockingQueueTest exte } /** - * drainTo(null, n) throws NPE - */ - public void testDrainToNullN() { - ArrayBlockingQueue q = populatedQueue(SIZE); - try { - q.drainTo(null, 0); - shouldThrow(); - } catch (NullPointerException success) {} - } - - /** - * drainTo(this, n) throws IAE - */ - public void testDrainToSelfN() { - ArrayBlockingQueue q = populatedQueue(SIZE); - try { - q.drainTo(q, 0); - shouldThrow(); - } catch (IllegalArgumentException success) {} - } - - /** * drainTo(c, n) empties first min(n, size) elements of queue into c */ public void testDrainToN() { @@ -899,12 +905,26 @@ public class ArrayBlockingQueueTest exte 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)); + } + } }