--- jsr166/src/test/tck/LinkedBlockingQueueTest.java 2009/11/21 02:33:20 1.16 +++ jsr166/src/test/tck/LinkedBlockingQueueTest.java 2011/05/30 22:43:20 1.45 @@ -1,34 +1,56 @@ /* * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at - * http://creativecommons.org/licenses/publicdomain + * http://creativecommons.org/publicdomain/zero/1.0/ * Other contributors include Andrew Wright, Jeffrey Hayes, * Pat Fisher, Mike Judd. */ import junit.framework.*; -import java.util.*; -import java.util.concurrent.*; +import java.util.Arrays; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; +import java.util.NoSuchElementException; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.Executors; +import java.util.concurrent.ExecutorService; import static java.util.concurrent.TimeUnit.MILLISECONDS; import java.io.*; public class LinkedBlockingQueueTest extends JSR166TestCase { + public static class Unbounded extends BlockingQueueTest { + protected BlockingQueue emptyCollection() { + return new LinkedBlockingQueue(); + } + } + + public static class Bounded extends BlockingQueueTest { + protected BlockingQueue emptyCollection() { + return new LinkedBlockingQueue(20); + } + } + public static void main(String[] args) { - junit.textui.TestRunner.run (suite()); + junit.textui.TestRunner.run(suite()); } public static Test suite() { - return new TestSuite(LinkedBlockingQueueTest.class); + return newTestSuite(LinkedBlockingQueueTest.class, + new Unbounded().testSuite(), + new Bounded().testSuite()); } - /** * Create a queue of given size containing consecutive * Integers 0 ... n. */ - private LinkedBlockingQueue populatedQueue(int n) { - LinkedBlockingQueue q = new LinkedBlockingQueue(n); + private LinkedBlockingQueue populatedQueue(int n) { + LinkedBlockingQueue q = + new LinkedBlockingQueue(n); assertTrue(q.isEmpty()); for (int i = 0; i < n; i++) assertTrue(q.offer(new Integer(i))); @@ -48,66 +70,61 @@ public class LinkedBlockingQueueTest ext } /** - * Constructor throws IAE if capacity argument nonpositive + * Constructor throws IllegalArgumentException if capacity argument nonpositive */ public void testConstructor2() { try { - LinkedBlockingQueue q = new LinkedBlockingQueue(0); + new LinkedBlockingQueue(0); shouldThrow(); - } - catch (IllegalArgumentException success) {} + } catch (IllegalArgumentException success) {} } /** - * Initializing from null Collection throws NPE + * Initializing from null Collection throws NullPointerException */ public void testConstructor3() { try { - LinkedBlockingQueue q = new LinkedBlockingQueue(null); + new LinkedBlockingQueue(null); shouldThrow(); - } - catch (NullPointerException success) {} + } catch (NullPointerException success) {} } /** - * Initializing from Collection of null elements throws NPE + * Initializing from Collection of null elements throws NullPointerException */ public void testConstructor4() { + Collection elements = Arrays.asList(new Integer[SIZE]); try { - Integer[] ints = new Integer[SIZE]; - LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints)); + new LinkedBlockingQueue(elements); shouldThrow(); - } - catch (NullPointerException success) {} + } catch (NullPointerException success) {} } /** - * Initializing from Collection with some null elements throws NPE + * Initializing from Collection with some null elements throws + * NullPointerException */ public void testConstructor5() { + Integer[] ints = new Integer[SIZE]; + for (int i = 0; i < SIZE-1; ++i) + ints[i] = new Integer(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); - LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints)); + new LinkedBlockingQueue(elements); shouldThrow(); - } - catch (NullPointerException success) {} + } catch (NullPointerException success) {} } /** * Queue contains all elements of collection used to initialize */ public void testConstructor6() { - try { - Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE; ++i) - ints[i] = new Integer(i); - LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints)); - for (int i = 0; i < SIZE; ++i) - assertEquals(ints[i], q.poll()); - } - finally {} + Integer[] ints = new Integer[SIZE]; + for (int i = 0; i < SIZE; ++i) + ints[i] = new Integer(i); + LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints)); + for (int i = 0; i < SIZE; ++i) + assertEquals(ints[i], q.poll()); } /** @@ -143,28 +160,6 @@ public class LinkedBlockingQueueTest ext } /** - * offer(null) throws NPE - */ - public void testOfferNull() { - try { - LinkedBlockingQueue q = new LinkedBlockingQueue(1); - q.offer(null); - shouldThrow(); - } catch (NullPointerException success) { } - } - - /** - * add(null) throws NPE - */ - public void testAddNull() { - try { - LinkedBlockingQueue q = new LinkedBlockingQueue(1); - q.add(null); - shouldThrow(); - } catch (NullPointerException success) { } - } - - /** * Offer succeeds if not full; fails if full */ public void testOffer() { @@ -174,199 +169,157 @@ public class LinkedBlockingQueueTest ext } /** - * add succeeds if not full; throws ISE if full + * add succeeds if not full; throws IllegalStateException if full */ public void testAdd() { + LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); + for (int i = 0; i < SIZE; ++i) + assertTrue(q.add(new Integer(i))); + assertEquals(0, q.remainingCapacity()); try { - LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); - for (int i = 0; i < SIZE; ++i) { - assertTrue(q.add(new Integer(i))); - } - assertEquals(0, q.remainingCapacity()); q.add(new Integer(SIZE)); - } catch (IllegalStateException success) { - } - } - - /** - * addAll(null) throws NPE - */ - public void testAddAll1() { - try { - LinkedBlockingQueue q = new LinkedBlockingQueue(1); - q.addAll(null); shouldThrow(); - } - catch (NullPointerException success) {} + } catch (IllegalStateException success) {} } /** - * addAll(this) throws IAE + * addAll(this) throws IllegalArgumentException */ public void testAddAllSelf() { + LinkedBlockingQueue q = populatedQueue(SIZE); try { - LinkedBlockingQueue q = populatedQueue(SIZE); q.addAll(q); shouldThrow(); - } - catch (IllegalArgumentException success) {} + } catch (IllegalArgumentException success) {} } /** - * addAll of a collection with null elements throws NPE - */ - public void testAddAll2() { - try { - LinkedBlockingQueue q = new LinkedBlockingQueue(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 */ public void testAddAll3() { + LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); + Integer[] ints = new Integer[SIZE]; + for (int i = 0; i < SIZE-1; ++i) + ints[i] = new Integer(i); + Collection elements = Arrays.asList(ints); try { - LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); - Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE-1; ++i) - ints[i] = new Integer(i); - q.addAll(Arrays.asList(ints)); + q.addAll(elements); shouldThrow(); - } - catch (NullPointerException success) {} + } catch (NullPointerException success) {} } + /** - * addAll throws ISE if not enough room + * addAll throws IllegalStateException if not enough room */ public void testAddAll4() { + LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE - 1); + Integer[] ints = new Integer[SIZE]; + for (int i = 0; i < SIZE; ++i) + ints[i] = new Integer(i); + Collection elements = Arrays.asList(ints); try { - LinkedBlockingQueue q = new LinkedBlockingQueue(1); - Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE; ++i) - ints[i] = new Integer(i); - q.addAll(Arrays.asList(ints)); + q.addAll(elements); shouldThrow(); - } - catch (IllegalStateException success) {} + } catch (IllegalStateException success) {} } + /** * Queue contains all elements, in traversal order, of successful addAll */ public void testAddAll5() { - try { - Integer[] empty = new Integer[0]; - Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE; ++i) - ints[i] = new Integer(i); - LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); - assertFalse(q.addAll(Arrays.asList(empty))); - assertTrue(q.addAll(Arrays.asList(ints))); - for (int i = 0; i < SIZE; ++i) - assertEquals(ints[i], q.poll()); - } - finally {} + Integer[] empty = new Integer[0]; + Integer[] ints = new Integer[SIZE]; + for (int i = 0; i < SIZE; ++i) + ints[i] = new Integer(i); + LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); + assertFalse(q.addAll(Arrays.asList(empty))); + assertTrue(q.addAll(Arrays.asList(ints))); + for (int i = 0; i < SIZE; ++i) + assertEquals(ints[i], q.poll()); } /** - * put(null) throws NPE - */ - public void testPutNull() { - try { - LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); - q.put(null); - shouldThrow(); - } - catch (NullPointerException success) { - } - catch (InterruptedException ie) { - unexpectedException(); - } - } - - /** * all elements successfully put are contained */ - public void testPut() { - try { - LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); - for (int i = 0; i < SIZE; ++i) { - Integer I = new Integer(i); - q.put(I); - assertTrue(q.contains(I)); - } - assertEquals(0, q.remainingCapacity()); - } - catch (InterruptedException ie) { - unexpectedException(); + public void testPut() throws InterruptedException { + LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); + for (int i = 0; i < SIZE; ++i) { + Integer I = new Integer(i); + q.put(I); + assertTrue(q.contains(I)); } + assertEquals(0, q.remainingCapacity()); } /** * put blocks interruptibly if full */ - public void testBlockingPut() { - Thread t = new Thread(new Runnable() { - public void run() { - int added = 0; - try { - LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); - for (int i = 0; i < SIZE; ++i) { - q.put(new Integer(i)); - ++added; - } - q.put(new Integer(SIZE)); - threadShouldThrow(); - } catch (InterruptedException ie) { - threadAssertEquals(added, SIZE); - } - }}); - t.start(); - try { - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); - } - catch (InterruptedException ie) { - unexpectedException(); - } + public void testBlockingPut() throws InterruptedException { + final LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); + 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()); + }}); + + await(pleaseInterrupt); + assertThreadStaysAlive(t); + t.interrupt(); + 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() { + public void testPutWithTake() throws InterruptedException { + final int capacity = 2; final LinkedBlockingQueue q = new LinkedBlockingQueue(2); - Thread t = new Thread(new Runnable() { - public void run() { - int added = 0; - try { - q.put(new Object()); - ++added; - q.put(new Object()); - ++added; - q.put(new Object()); - ++added; - q.put(new Object()); - ++added; - threadShouldThrow(); - } catch (InterruptedException e) { - threadAssertTrue(added >= 2); - } - } - }); - try { - t.start(); - Thread.sleep(SHORT_DELAY_MS); - q.take(); - t.interrupt(); - t.join(); - } catch (Exception e) { - unexpectedException(); - } + 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; i++) + q.put(i); + pleaseTake.countDown(); + q.put(86); + + pleaseInterrupt.countDown(); + try { + q.put(99); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + }}); + + await(pleaseTake); + assertEquals(q.remainingCapacity(), 0); + assertEquals(0, q.take()); + + await(pleaseInterrupt); + assertThreadStaysAlive(t); + t.interrupt(); + awaitTermination(t); + assertEquals(q.remainingCapacity(), 0); } /** @@ -374,92 +327,69 @@ public class LinkedBlockingQueueTest ext */ public void testTimedOffer() { final LinkedBlockingQueue q = new LinkedBlockingQueue(2); - Thread t = new Thread(new Runnable() { - public void run() { - try { - q.put(new Object()); - q.put(new Object()); - threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); - q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); - threadShouldThrow(); - } catch (InterruptedException success) {} - } - }); - - try { - t.start(); - Thread.sleep(SMALL_DELAY_MS); - t.interrupt(); - t.join(); - } catch (Exception e) { - unexpectedException(); - } + final CountDownLatch pleaseInterrupt = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + q.put(new Object()); + q.put(new Object()); + long startTime = System.nanoTime(); + assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); + pleaseInterrupt.countDown(); + try { + q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + }}); + + await(pleaseInterrupt); + assertThreadStaysAlive(t); + t.interrupt(); + awaitTermination(t); } /** * take retrieves elements in FIFO order */ - public void testTake() { - try { - LinkedBlockingQueue q = populatedQueue(SIZE); - for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.take()).intValue()); - } - } catch (InterruptedException e) { - unexpectedException(); + public void testTake() throws InterruptedException { + LinkedBlockingQueue q = populatedQueue(SIZE); + for (int i = 0; i < SIZE; ++i) { + assertEquals(i, q.take()); } } /** - * take blocks interruptibly when empty + * Take removes existing elements until empty, then blocks interruptibly */ - public void testTakeFromEmpty() { - final LinkedBlockingQueue q = new LinkedBlockingQueue(2); - Thread t = new Thread(new Runnable() { - public void run() { - try { - q.take(); - threadShouldThrow(); - } catch (InterruptedException success) { } + public void testBlockingTake() throws InterruptedException { + final BlockingQueue q = populatedQueue(SIZE); + 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()); } - }); - try { - t.start(); - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); - } catch (Exception e) { - unexpectedException(); - } - } - /** - * Take removes existing elements until empty, then blocks interruptibly - */ - public void testBlockingTake() { - Thread t = new Thread(new Runnable() { - public void run() { - try { - LinkedBlockingQueue q = populatedQueue(SIZE); - for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.take()).intValue()); - } - q.take(); - threadShouldThrow(); - } catch (InterruptedException success) { - } - }}); - t.start(); - try { - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); - } - catch (InterruptedException ie) { - unexpectedException(); - } - } + 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()); + }}); + + await(pleaseInterrupt); + assertThreadStaysAlive(t); + t.interrupt(); + awaitTermination(t); + } /** * poll succeeds unless empty @@ -467,93 +397,67 @@ public class LinkedBlockingQueueTest ext public void testPoll() { LinkedBlockingQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.poll()).intValue()); + assertEquals(i, q.poll()); } assertNull(q.poll()); } /** - * 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() { - try { - LinkedBlockingQueue q = populatedQueue(SIZE); - for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue()); - } - assertNull(q.poll(0, MILLISECONDS)); - } catch (InterruptedException e) { - unexpectedException(); + public void testTimedPoll0() throws InterruptedException { + LinkedBlockingQueue q = populatedQueue(SIZE); + for (int i = 0; i < SIZE; ++i) { + assertEquals(i, q.poll(0, MILLISECONDS)); } + assertNull(q.poll(0, MILLISECONDS)); } /** - * 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() { - try { - LinkedBlockingQueue q = populatedQueue(SIZE); - for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); - } - assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); - } catch (InterruptedException e) { - unexpectedException(); - } + public void testTimedPoll() throws InterruptedException { + LinkedBlockingQueue q = populatedQueue(SIZE); + for (int i = 0; i < SIZE; ++i) { + long startTime = System.nanoTime(); + assertEquals(i, (int) 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); } /** * Interrupted timed poll throws InterruptedException instead of * returning timeout status */ - public void testInterruptedTimedPoll() { - Thread t = new Thread(new Runnable() { - public void run() { - try { - LinkedBlockingQueue q = populatedQueue(SIZE); - for (int i = 0; i < SIZE; ++i) { - threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); - } - threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); - } catch (InterruptedException success) { - } - }}); - t.start(); - try { - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); - } - catch (InterruptedException ie) { - unexpectedException(); - } - } - - /** - * timed poll before a delayed offer fails; after offer succeeds; - * on interruption throws - */ - public void testTimedPollWithOffer() { - final LinkedBlockingQueue q = new LinkedBlockingQueue(2); - Thread t = new Thread(new Runnable() { - public void run() { - try { - threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); - q.poll(LONG_DELAY_MS, MILLISECONDS); - q.poll(LONG_DELAY_MS, MILLISECONDS); - threadShouldThrow(); - } catch (InterruptedException success) { } + public void testInterruptedTimedPoll() throws InterruptedException { + final BlockingQueue q = populatedQueue(SIZE); + final CountDownLatch aboutToWait = new CountDownLatch(1); + 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); } - }); - try { - t.start(); - Thread.sleep(SMALL_DELAY_MS); - assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); - t.interrupt(); - t.join(); - } catch (Exception e) { - unexpectedException(); - } + long t0 = System.nanoTime(); + aboutToWait.countDown(); + try { + q.poll(MEDIUM_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) { + assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); + } + }}); + + aboutToWait.await(); + waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); + t.interrupt(); + awaitTermination(t, MEDIUM_DELAY_MS); + checkEmpty(q); } /** @@ -562,10 +466,10 @@ public class LinkedBlockingQueueTest ext public void testPeek() { LinkedBlockingQueue 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()); } @@ -576,14 +480,13 @@ public class LinkedBlockingQueueTest ext public void testElement() { LinkedBlockingQueue 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(); shouldThrow(); - } - catch (NoSuchElementException success) {} + } catch (NoSuchElementException success) {} } /** @@ -592,13 +495,12 @@ public class LinkedBlockingQueueTest ext public void testRemove() { LinkedBlockingQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.remove()).intValue()); + assertEquals(i, q.remove()); } try { q.remove(); shouldThrow(); - } catch (NoSuchElementException success) { - } + } catch (NoSuchElementException success) {} } /** @@ -607,11 +509,17 @@ public class LinkedBlockingQueueTest ext public void testRemoveElement() { LinkedBlockingQueue q = populatedQueue(SIZE); for (int i = 1; i < SIZE; i+=2) { - assertTrue(q.remove(new Integer(i))); + assertTrue(q.contains(i)); + assertTrue(q.remove(i)); + assertFalse(q.contains(i)); + assertTrue(q.contains(i-1)); } for (int i = 0; i < SIZE; i+=2) { - assertTrue(q.remove(new Integer(i))); - assertFalse(q.remove(new Integer(i+1))); + assertTrue(q.contains(i)); + assertTrue(q.remove(i)); + assertFalse(q.contains(i)); + assertFalse(q.remove(i+1)); + assertFalse(q.contains(i+1)); } assertTrue(q.isEmpty()); } @@ -619,18 +527,14 @@ public class LinkedBlockingQueueTest ext /** * An add following remove(x) succeeds */ - public void testRemoveElementAndAdd() { - try { - LinkedBlockingQueue q = new LinkedBlockingQueue(); - assertTrue(q.add(new Integer(1))); - assertTrue(q.add(new Integer(2))); - assertTrue(q.remove(new Integer(1))); - assertTrue(q.remove(new Integer(2))); - assertTrue(q.add(new Integer(3))); - assertTrue(q.take() != null); - } catch (Exception e) { - unexpectedException(); - } + public void testRemoveElementAndAdd() throws InterruptedException { + LinkedBlockingQueue q = new LinkedBlockingQueue(); + assertTrue(q.add(new Integer(1))); + assertTrue(q.add(new Integer(2))); + assertTrue(q.remove(new Integer(1))); + assertTrue(q.remove(new Integer(2))); + assertTrue(q.add(new Integer(3))); + assertTrue(q.take() != null); } /** @@ -711,76 +615,53 @@ public class LinkedBlockingQueueTest ext } /** - * toArray contains all elements + * toArray contains all elements in FIFO order */ public void testToArray() { LinkedBlockingQueue q = populatedQueue(SIZE); Object[] o = q.toArray(); - try { for (int i = 0; i < o.length; i++) - assertEquals(o[i], q.take()); - } catch (InterruptedException e) { - unexpectedException(); - } + assertSame(o[i], q.poll()); } /** - * toArray(a) contains all elements + * toArray(a) contains all elements in FIFO order */ - public void testToArray2() { - LinkedBlockingQueue q = populatedQueue(SIZE); + public void testToArray2() throws InterruptedException { + LinkedBlockingQueue q = populatedQueue(SIZE); Integer[] ints = new Integer[SIZE]; - ints = (Integer[])q.toArray(ints); - try { - for (int i = 0; i < ints.length; i++) - assertEquals(ints[i], q.take()); - } catch (InterruptedException e) { - unexpectedException(); - } + Integer[] array = q.toArray(ints); + assertSame(ints, array); + for (int i = 0; i < ints.length; i++) + assertSame(ints[i], q.poll()); } /** - * toArray(null) throws NPE - */ - public void testToArray_BadArg() { - try { - LinkedBlockingQueue q = populatedQueue(SIZE); - Object o[] = q.toArray(null); - shouldThrow(); - } catch (NullPointerException success) {} - } - - /** - * toArray with incompatible array type throws CCE + * toArray(incompatible array type) throws ArrayStoreException */ public void testToArray1_BadArg() { + LinkedBlockingQueue q = populatedQueue(SIZE); try { - LinkedBlockingQueue q = populatedQueue(SIZE); - Object o[] = q.toArray(new String[10] ); + q.toArray(new String[10]); shouldThrow(); - } catch (ArrayStoreException success) {} + } catch (ArrayStoreException success) {} } - /** * iterator iterates through all elements */ - public void testIterator() { + public void testIterator() throws InterruptedException { LinkedBlockingQueue q = populatedQueue(SIZE); Iterator it = q.iterator(); - try { - while (it.hasNext()) { - assertEquals(it.next(), q.take()); - } - } catch (InterruptedException e) { - unexpectedException(); + while (it.hasNext()) { + assertEquals(it.next(), q.take()); } } /** * iterator.remove removes current element */ - public void testIteratorRemove () { + public void testIteratorRemove() { final LinkedBlockingQueue q = new LinkedBlockingQueue(3); q.add(two); q.add(one); @@ -791,12 +672,11 @@ public class LinkedBlockingQueueTest ext it.remove(); it = q.iterator(); - assertEquals(it.next(), one); - assertEquals(it.next(), three); + assertSame(it.next(), one); + assertSame(it.next(), three); assertFalse(it.hasNext()); } - /** * iterator ordering is FIFO */ @@ -808,8 +688,7 @@ public class LinkedBlockingQueueTest ext assertEquals(0, q.remainingCapacity()); 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); } @@ -817,24 +696,18 @@ public class LinkedBlockingQueueTest ext /** * Modifications do not cause iterators to fail */ - public void testWeaklyConsistentIteration () { + public void testWeaklyConsistentIteration() { final LinkedBlockingQueue q = new LinkedBlockingQueue(3); q.add(one); q.add(two); q.add(three); - try { - for (Iterator it = q.iterator(); it.hasNext();) { - q.remove(); - it.next(); - } - } - catch (ConcurrentModificationException e) { - unexpectedException(); + for (Iterator it = q.iterator(); it.hasNext();) { + q.remove(); + it.next(); } assertEquals(0, q.size()); } - /** * toString contains toStrings of elements */ @@ -842,11 +715,10 @@ public class LinkedBlockingQueueTest ext LinkedBlockingQueue 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 */ @@ -855,64 +727,44 @@ public class LinkedBlockingQueueTest ext q.add(one); q.add(two); ExecutorService executor = Executors.newFixedThreadPool(2); - executor.execute(new Runnable() { - public void run() { - threadAssertFalse(q.offer(three)); - try { - threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); - threadAssertEquals(0, q.remainingCapacity()); - } - catch (InterruptedException e) { - threadUnexpectedException(); - } - } - }); - - executor.execute(new Runnable() { - public void run() { - try { - Thread.sleep(SMALL_DELAY_MS); - threadAssertEquals(one, q.take()); - } - catch (InterruptedException e) { - threadUnexpectedException(); - } - } - }); + 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(); + assertSame(one, q.take()); + }}); joinPool(executor); } /** - * poll retrieves elements across Executor threads + * timed poll retrieves elements across Executor threads */ public void testPollInExecutor() { final LinkedBlockingQueue q = new LinkedBlockingQueue(2); + final CheckedBarrier threadsStarted = new CheckedBarrier(2); ExecutorService executor = Executors.newFixedThreadPool(2); - executor.execute(new Runnable() { - public void run() { - threadAssertNull(q.poll()); - try { - threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); - threadAssertTrue(q.isEmpty()); - } - catch (InterruptedException e) { - threadUnexpectedException(); - } - } - }); - - executor.execute(new Runnable() { - public void run() { - try { - Thread.sleep(SMALL_DELAY_MS); - q.put(one); - } - catch (InterruptedException e) { - threadUnexpectedException(); - } - } - }); + 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); } @@ -920,48 +772,20 @@ public class LinkedBlockingQueueTest ext /** * A deserialized serialized queue has same elements in same order */ - public void testSerialization() { + public void testSerialization() throws Exception { LinkedBlockingQueue q = populatedQueue(SIZE); - try { - 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)); - LinkedBlockingQueue r = (LinkedBlockingQueue)in.readObject(); - assertEquals(q.size(), r.size()); - while (!q.isEmpty()) - assertEquals(q.remove(), r.remove()); - } catch (Exception e) { - unexpectedException(); - } - } - - /** - * drainTo(null) throws NPE - */ - public void testDrainToNull() { - LinkedBlockingQueue q = populatedQueue(SIZE); - try { - q.drainTo(null); - shouldThrow(); - } catch (NullPointerException success) { - } - } - - /** - * drainTo(this) throws IAE - */ - public void testDrainToSelf() { - LinkedBlockingQueue q = populatedQueue(SIZE); - try { - q.drainTo(q); - shouldThrow(); - } catch (IllegalArgumentException success) { - } + 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)); + LinkedBlockingQueue r = (LinkedBlockingQueue)in.readObject(); + assertEquals(q.size(), r.size()); + while (!q.isEmpty()) + assertEquals(q.remove(), r.remove()); } /** @@ -991,57 +815,25 @@ public class LinkedBlockingQueueTest ext /** * drainTo empties full queue, unblocking a waiting put. */ - public void testDrainToWithActivePut() { + public void testDrainToWithActivePut() throws InterruptedException { final LinkedBlockingQueue q = populatedQueue(SIZE); - Thread t = new Thread(new Runnable() { - public void run() { - try { - q.put(new Integer(SIZE+1)); - } catch (InterruptedException ie) { - threadUnexpectedException(); - } - } - }); - try { - t.start(); - ArrayList l = new ArrayList(); - q.drainTo(l); - assertTrue(l.size() >= SIZE); - for (int i = 0; i < SIZE; ++i) - assertEquals(l.get(i), new Integer(i)); - t.join(); - assertTrue(q.size() + l.size() >= SIZE); - } catch (Exception e) { - unexpectedException(); - } - } - - /** - * drainTo(null, n) throws NPE - */ - public void testDrainToNullN() { - LinkedBlockingQueue q = populatedQueue(SIZE); - try { - q.drainTo(null, 0); - shouldThrow(); - } catch (NullPointerException success) { - } - } + Thread t = new Thread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + q.put(new Integer(SIZE+1)); + }}); - /** - * drainTo(this, n) throws IAE - */ - public void testDrainToSelfN() { - LinkedBlockingQueue q = populatedQueue(SIZE); - try { - q.drainTo(q, 0); - shouldThrow(); - } catch (IllegalArgumentException success) { - } + t.start(); + ArrayList l = new ArrayList(); + q.drainTo(l); + assertTrue(l.size() >= SIZE); + for (int i = 0; i < SIZE; ++i) + assertEquals(l.get(i), new Integer(i)); + t.join(); + assertTrue(q.size() + l.size() >= SIZE); } /** - * 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() { LinkedBlockingQueue q = new LinkedBlockingQueue(); @@ -1050,7 +842,7 @@ public class LinkedBlockingQueueTest ext 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)