--- jsr166/src/test/tck/LinkedBlockingQueueTest.java 2003/09/20 18:20:08 1.4 +++ jsr166/src/test/tck/LinkedBlockingQueueTest.java 2009/11/16 04:57:10 1.13 @@ -1,8 +1,9 @@ /* - * Written by members of JCP JSR-166 Expert Group and released to the - * public domain. Use, modify, and redistribute this code in any way - * without acknowledgement. Other contributors include Andrew Wright, - * Jeffrey Hayes, Pat Fischer, Mike Judd. + * 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 + * Other contributors include Andrew Wright, Jeffrey Hayes, + * Pat Fisher, Mike Judd. */ import junit.framework.*; @@ -13,7 +14,7 @@ import java.io.*; public class LinkedBlockingQueueTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run (suite()); + junit.textui.TestRunner.run (suite()); } public static Test suite() { @@ -28,23 +29,25 @@ public class LinkedBlockingQueueTest ext private LinkedBlockingQueue populatedQueue(int n) { LinkedBlockingQueue q = new LinkedBlockingQueue(n); assertTrue(q.isEmpty()); - for(int i = 0; i < n; i++) + for (int i = 0; i < n; i++) assertTrue(q.offer(new Integer(i))); assertFalse(q.isEmpty()); assertEquals(0, q.remainingCapacity()); assertEquals(n, q.size()); return q; } - + /** - * + * A new queue has the indicated capacity, or Integer.MAX_VALUE if + * none given */ public void testConstructor1() { assertEquals(SIZE, new LinkedBlockingQueue(SIZE).remainingCapacity()); + assertEquals(Integer.MAX_VALUE, new LinkedBlockingQueue().remainingCapacity()); } /** - * + * Constructor throws IAE if capacity argument nonpositive */ public void testConstructor2() { try { @@ -55,10 +58,9 @@ public class LinkedBlockingQueueTest ext } /** - * + * Initializing from null Collection throws NPE */ public void testConstructor3() { - try { LinkedBlockingQueue q = new LinkedBlockingQueue(null); shouldThrow(); @@ -67,7 +69,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * Initializing from Collection of null elements throws NPE */ public void testConstructor4() { try { @@ -79,7 +81,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * Initializing from Collection with some null elements throws NPE */ public void testConstructor5() { try { @@ -93,7 +95,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * Queue contains all elements of collection used to initialize */ public void testConstructor6() { try { @@ -108,7 +110,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * Queue transitions from empty to full when elements added */ public void testEmptyFull() { LinkedBlockingQueue q = new LinkedBlockingQueue(2); @@ -123,7 +125,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * remainingCapacity decreases on add, increases on remove */ public void testRemainingCapacity() { LinkedBlockingQueue q = populatedQueue(SIZE); @@ -140,18 +142,29 @@ public class LinkedBlockingQueueTest ext } /** - * + * offer(null) throws NPE */ public void testOfferNull() { try { LinkedBlockingQueue q = new LinkedBlockingQueue(1); q.offer(null); shouldThrow(); - } catch (NullPointerException success) { } + } 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() { LinkedBlockingQueue q = new LinkedBlockingQueue(1); @@ -160,7 +173,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * add succeeds if not full; throws ISE if full */ public void testAdd() { try { @@ -171,11 +184,11 @@ public class LinkedBlockingQueueTest ext assertEquals(0, q.remainingCapacity()); q.add(new Integer(SIZE)); } catch (IllegalStateException success){ - } + } } /** - * + * addAll(null) throws NPE */ public void testAddAll1() { try { @@ -185,8 +198,21 @@ public class LinkedBlockingQueueTest ext } catch (NullPointerException success) {} } + + /** + * addAll(this) throws IAE + */ + public void testAddAllSelf() { + try { + LinkedBlockingQueue q = populatedQueue(SIZE); + q.addAll(q); + shouldThrow(); + } + catch (IllegalArgumentException success) {} + } + /** - * + * addAll of a collection with null elements throws NPE */ public void testAddAll2() { try { @@ -198,7 +224,8 @@ public class LinkedBlockingQueueTest ext catch (NullPointerException success) {} } /** - * + * addAll of a collection with any null elements throws NPE after + * possibly adding some elements */ public void testAddAll3() { try { @@ -212,7 +239,7 @@ public class LinkedBlockingQueueTest ext catch (NullPointerException success) {} } /** - * + * addAll throws ISE if not enough room */ public void testAddAll4() { try { @@ -226,7 +253,7 @@ public class LinkedBlockingQueueTest ext catch (IllegalStateException success) {} } /** - * + * Queue contains all elements, in traversal order, of successful addAll */ public void testAddAll5() { try { @@ -244,23 +271,23 @@ public class LinkedBlockingQueueTest ext } /** - * + * 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 { @@ -278,7 +305,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * put blocks interruptibly if full */ public void testBlockingPut() { Thread t = new Thread(new Runnable() { @@ -294,11 +321,11 @@ public class LinkedBlockingQueueTest ext threadShouldThrow(); } catch (InterruptedException ie){ threadAssertEquals(added, SIZE); - } + } }}); t.start(); - try { - Thread.sleep(SHORT_DELAY_MS); + try { + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); } @@ -308,7 +335,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * put blocks waiting for take when full */ public void testPutWithTake() { final LinkedBlockingQueue q = new LinkedBlockingQueue(2); @@ -342,7 +369,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * timed offer times out if full and elements not taken */ public void testTimedOffer() { final LinkedBlockingQueue q = new LinkedBlockingQueue(2); @@ -357,7 +384,7 @@ public class LinkedBlockingQueueTest ext } catch (InterruptedException success){} } }); - + try { t.start(); Thread.sleep(SMALL_DELAY_MS); @@ -369,7 +396,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * take retrieves elements in FIFO order */ public void testTake() { try { @@ -379,11 +406,11 @@ public class LinkedBlockingQueueTest ext } } catch (InterruptedException e){ unexpectedException(); - } + } } /** - * + * take blocks interruptibly when empty */ public void testTakeFromEmpty() { final LinkedBlockingQueue q = new LinkedBlockingQueue(2); @@ -392,7 +419,7 @@ public class LinkedBlockingQueueTest ext try { q.take(); threadShouldThrow(); - } catch (InterruptedException success){ } + } catch (InterruptedException success){ } } }); try { @@ -406,7 +433,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * Take removes existing elements until empty, then blocks interruptibly */ public void testBlockingTake() { Thread t = new Thread(new Runnable() { @@ -419,11 +446,11 @@ public class LinkedBlockingQueueTest ext q.take(); threadShouldThrow(); } catch (InterruptedException success){ - } + } }}); t.start(); - try { - Thread.sleep(SHORT_DELAY_MS); + try { + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); } @@ -434,7 +461,7 @@ public class LinkedBlockingQueueTest ext /** - * + * poll succeeds unless empty */ public void testPoll() { LinkedBlockingQueue q = populatedQueue(SIZE); @@ -445,7 +472,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * timed pool with zero timeout succeeds when non-empty, else times out */ public void testTimedPoll0() { try { @@ -456,11 +483,11 @@ public class LinkedBlockingQueueTest ext assertNull(q.poll(0, TimeUnit.MILLISECONDS)); } catch (InterruptedException e){ unexpectedException(); - } + } } /** - * + * timed pool with nonzero timeout succeeds when non-empty, else times out */ public void testTimedPoll() { try { @@ -471,11 +498,12 @@ public class LinkedBlockingQueueTest ext assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); } catch (InterruptedException e){ unexpectedException(); - } + } } /** - * + * Interrupted timed poll throws InterruptedException instead of + * returning timeout status */ public void testInterruptedTimedPoll() { Thread t = new Thread(new Runnable() { @@ -487,11 +515,11 @@ public class LinkedBlockingQueueTest ext } threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); } catch (InterruptedException success){ - } + } }}); t.start(); - try { - Thread.sleep(SHORT_DELAY_MS); + try { + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); } @@ -501,7 +529,8 @@ public class LinkedBlockingQueueTest ext } /** - * + * timed poll before a delayed offer fails; after offer succeeds; + * on interruption throws */ public void testTimedPollWithOffer() { final LinkedBlockingQueue q = new LinkedBlockingQueue(2); @@ -512,7 +541,7 @@ public class LinkedBlockingQueueTest ext q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); threadShouldThrow(); - } catch (InterruptedException success) { } + } catch (InterruptedException success) { } } }); try { @@ -524,11 +553,10 @@ public class LinkedBlockingQueueTest ext } catch (Exception e){ unexpectedException(); } - } - + } /** - * + * peek returns next element, or null if empty */ public void testPeek() { LinkedBlockingQueue q = populatedQueue(SIZE); @@ -542,7 +570,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * element returns next element, or throws NSEE if empty */ public void testElement() { LinkedBlockingQueue q = populatedQueue(SIZE); @@ -558,7 +586,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * remove removes next element, or throws NSEE if empty */ public void testRemove() { LinkedBlockingQueue q = populatedQueue(SIZE); @@ -569,11 +597,11 @@ public class LinkedBlockingQueueTest ext q.remove(); shouldThrow(); } catch (NoSuchElementException success){ - } + } } /** - * + * remove(x) removes x and returns true if present */ public void testRemoveElement() { LinkedBlockingQueue q = populatedQueue(SIZE); @@ -586,9 +614,26 @@ public class LinkedBlockingQueueTest ext } assertTrue(q.isEmpty()); } - + + /** + * 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(); + } + } + /** - * + * contains(x) reports true when elements added but not yet removed */ public void testContains() { LinkedBlockingQueue q = populatedQueue(SIZE); @@ -600,7 +645,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * clear removes all elements */ public void testClear() { LinkedBlockingQueue q = populatedQueue(SIZE); @@ -610,12 +655,13 @@ public class LinkedBlockingQueueTest ext assertEquals(SIZE, q.remainingCapacity()); q.add(one); assertFalse(q.isEmpty()); + assertTrue(q.contains(one)); q.clear(); assertTrue(q.isEmpty()); } /** - * + * containsAll(c) is true when c contains a subset of elements */ public void testContainsAll() { LinkedBlockingQueue q = populatedQueue(SIZE); @@ -629,7 +675,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * retainAll(c) retains only those elements of c and reports true if changed */ public void testRetainAll() { LinkedBlockingQueue q = populatedQueue(SIZE); @@ -648,7 +694,7 @@ public class LinkedBlockingQueueTest ext } /** - * + * removeAll(c) removes only those elements of c and reports true if changed */ public void testRemoveAll() { for (int i = 1; i < SIZE; ++i) { @@ -663,84 +709,118 @@ public class LinkedBlockingQueueTest ext } } - /** - * + * toArray contains all elements */ public void testToArray() { LinkedBlockingQueue q = populatedQueue(SIZE); Object[] o = q.toArray(); try { - for(int i = 0; i < o.length; i++) + for (int i = 0; i < o.length; i++) assertEquals(o[i], q.take()); } catch (InterruptedException e){ unexpectedException(); - } + } } /** - * + * toArray(a) contains all elements */ public void testToArray2() { LinkedBlockingQueue q = populatedQueue(SIZE); Integer[] ints = new Integer[SIZE]; ints = (Integer[])q.toArray(ints); try { - for(int i = 0; i < ints.length; i++) + for (int i = 0; i < ints.length; i++) assertEquals(ints[i], q.take()); } catch (InterruptedException e){ unexpectedException(); - } + } + } + + /** + * 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 + */ + public void testToArray1_BadArg() { + try { + LinkedBlockingQueue q = populatedQueue(SIZE); + Object o[] = q.toArray(new String[10] ); + shouldThrow(); + } catch (ArrayStoreException success){} + } + + + /** + * iterator iterates through all elements */ public void testIterator() { LinkedBlockingQueue q = populatedQueue(SIZE); Iterator it = q.iterator(); try { - while(it.hasNext()){ + while (it.hasNext()){ assertEquals(it.next(), q.take()); } } catch (InterruptedException e){ unexpectedException(); - } + } } /** - * + * iterator.remove removes current element */ - public void testIteratorOrdering() { - + public void testIteratorRemove () { final LinkedBlockingQueue q = new LinkedBlockingQueue(3); + q.add(two); + q.add(one); + q.add(three); + + Iterator it = q.iterator(); + it.next(); + it.remove(); + + it = q.iterator(); + assertEquals(it.next(), one); + assertEquals(it.next(), three); + assertFalse(it.hasNext()); + } + + /** + * iterator ordering is FIFO + */ + public void testIteratorOrdering() { + final LinkedBlockingQueue q = new LinkedBlockingQueue(3); q.add(one); q.add(two); q.add(three); - assertEquals(0, q.remainingCapacity()); - int k = 0; for (Iterator it = q.iterator(); it.hasNext();) { int i = ((Integer)(it.next())).intValue(); assertEquals(++k, i); } - assertEquals(3, k); } /** - * + * Modifications do not cause iterators to fail */ 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(); @@ -750,13 +830,12 @@ public class LinkedBlockingQueueTest ext catch (ConcurrentModificationException e) { unexpectedException(); } - assertEquals(0, q.size()); } /** - * + * toString contains toStrings of elements */ public void testToString() { LinkedBlockingQueue q = populatedQueue(SIZE); @@ -764,21 +843,17 @@ public class LinkedBlockingQueueTest ext for (int i = 0; i < SIZE; ++i) { assertTrue(s.indexOf(String.valueOf(i)) >= 0); } - } + } /** - * + * offer transfers elements across Executor tasks */ public void testOfferInExecutor() { - final LinkedBlockingQueue q = new LinkedBlockingQueue(2); - q.add(one); q.add(two); - ExecutorService executor = Executors.newFixedThreadPool(2); - executor.execute(new Runnable() { public void run() { threadAssertFalse(q.offer(three)); @@ -803,20 +878,16 @@ public class LinkedBlockingQueueTest ext } } }); - - joinPool(executor); + joinPool(executor); } /** - * + * poll retrieves elements across Executor threads */ public void testPollInExecutor() { - final LinkedBlockingQueue q = new LinkedBlockingQueue(2); - ExecutorService executor = Executors.newFixedThreadPool(2); - executor.execute(new Runnable() { public void run() { threadAssertNull(q.poll()); @@ -841,12 +912,12 @@ public class LinkedBlockingQueueTest ext } } }); - + joinPool(executor); } /** - * + * A deserialized serialized queue has same elements in same order */ public void testSerialization() { LinkedBlockingQueue q = populatedQueue(SIZE); @@ -861,11 +932,130 @@ public class LinkedBlockingQueueTest ext ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); LinkedBlockingQueue r = (LinkedBlockingQueue)in.readObject(); assertEquals(q.size(), r.size()); - while (!q.isEmpty()) + while (!q.isEmpty()) assertEquals(q.remove(), r.remove()); - } catch(Exception e){ + } 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) { + } + } + + /** + * drainTo(c) empties queue into another collection c + */ + public void testDrainTo() { + LinkedBlockingQueue q = populatedQueue(SIZE); + ArrayList l = new ArrayList(); + q.drainTo(l); + assertEquals(q.size(), 0); + assertEquals(l.size(), SIZE); + for (int i = 0; i < SIZE; ++i) + assertEquals(l.get(i), new Integer(i)); + q.add(zero); + q.add(one); + assertFalse(q.isEmpty()); + assertTrue(q.contains(zero)); + assertTrue(q.contains(one)); + l.clear(); + q.drainTo(l); + assertEquals(q.size(), 0); + assertEquals(l.size(), 2); + for (int i = 0; i < 2; ++i) + assertEquals(l.get(i), new Integer(i)); + } + + /** + * drainTo empties full queue, unblocking a waiting put. + */ + public void testDrainToWithActivePut() { + 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) { + } + } + + /** + * drainTo(this, n) throws IAE + */ + public void testDrainToSelfN() { + LinkedBlockingQueue q = populatedQueue(SIZE); + try { + q.drainTo(q, 0); + shouldThrow(); + } catch (IllegalArgumentException success) { + } + } + + /** + * drainTo(c, n) empties first max {n, size} elements of queue into c + */ + public void testDrainToN() { + LinkedBlockingQueue q = new LinkedBlockingQueue(); + 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); + for (int j = 0; j < k; ++j) + assertEquals(l.get(j), new Integer(j)); + while (q.poll() != null) ; + } + } + }