--- jsr166/src/test/tck/LinkedBlockingQueueTest.java 2003/09/25 11:02:41 1.5 +++ jsr166/src/test/tck/LinkedBlockingQueueTest.java 2009/11/21 02:33:20 1.16 @@ -1,23 +1,25 @@ /* - * 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.*; import java.util.*; import java.util.concurrent.*; +import static java.util.concurrent.TimeUnit.MILLISECONDS; 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() { - return new TestSuite(LinkedBlockingQueueTest.class); + return new TestSuite(LinkedBlockingQueueTest.class); } @@ -28,14 +30,14 @@ public class LinkedBlockingQueueTest ext 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))); + for (int i = 0; i < n; i++) + assertTrue(q.offer(new Integer(i))); assertFalse(q.isEmpty()); assertEquals(0, q.remainingCapacity()); - assertEquals(n, q.size()); + assertEquals(n, q.size()); return q; } - + /** * A new queue has the indicated capacity, or Integer.MAX_VALUE if * none given @@ -144,11 +146,22 @@ public class LinkedBlockingQueueTest ext * offer(null) throws NPE */ public void testOfferNull() { - try { + 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) { } } /** @@ -164,15 +177,15 @@ public class LinkedBlockingQueueTest ext * add succeeds if not full; throws ISE if full */ public void testAdd() { - try { + 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){ - } + } catch (IllegalStateException success) { + } } /** @@ -186,6 +199,19 @@ 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 */ @@ -249,15 +275,15 @@ public class LinkedBlockingQueueTest ext * put(null) throws NPE */ public void testPutNull() { - try { + try { LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); q.put(null); shouldThrow(); - } - catch (NullPointerException success){ - } + } + catch (NullPointerException success) { + } catch (InterruptedException ie) { - unexpectedException(); + unexpectedException(); } } @@ -275,7 +301,7 @@ public class LinkedBlockingQueueTest ext assertEquals(0, q.remainingCapacity()); } catch (InterruptedException ie) { - unexpectedException(); + unexpectedException(); } } @@ -294,18 +320,18 @@ public class LinkedBlockingQueueTest ext } q.put(new Integer(SIZE)); threadShouldThrow(); - } catch (InterruptedException ie){ + } catch (InterruptedException ie) { threadAssertEquals(added, SIZE); - } + } }}); t.start(); - try { - Thread.sleep(SHORT_DELAY_MS); + try { + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); } catch (InterruptedException ie) { - unexpectedException(); + unexpectedException(); } } @@ -326,8 +352,8 @@ public class LinkedBlockingQueueTest ext ++added; q.put(new Object()); ++added; - threadShouldThrow(); - } catch (InterruptedException e){ + threadShouldThrow(); + } catch (InterruptedException e) { threadAssertTrue(added >= 2); } } @@ -338,7 +364,7 @@ public class LinkedBlockingQueueTest ext q.take(); t.interrupt(); t.join(); - } catch (Exception e){ + } catch (Exception e) { unexpectedException(); } } @@ -353,19 +379,19 @@ public class LinkedBlockingQueueTest ext try { q.put(new Object()); q.put(new Object()); - threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); - q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); - threadShouldThrow(); - } catch (InterruptedException success){} + 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){ + } catch (Exception e) { unexpectedException(); } } @@ -374,14 +400,14 @@ public class LinkedBlockingQueueTest ext * take retrieves elements in FIFO order */ public void testTake() { - try { + try { LinkedBlockingQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { assertEquals(i, ((Integer)q.take()).intValue()); } - } catch (InterruptedException e){ - unexpectedException(); - } + } catch (InterruptedException e) { + unexpectedException(); + } } /** @@ -393,8 +419,8 @@ public class LinkedBlockingQueueTest ext public void run() { try { q.take(); - threadShouldThrow(); - } catch (InterruptedException success){ } + threadShouldThrow(); + } catch (InterruptedException success) { } } }); try { @@ -402,7 +428,7 @@ public class LinkedBlockingQueueTest ext Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); - } catch (Exception e){ + } catch (Exception e) { unexpectedException(); } } @@ -420,17 +446,17 @@ public class LinkedBlockingQueueTest ext } q.take(); threadShouldThrow(); - } catch (InterruptedException success){ - } + } catch (InterruptedException success) { + } }}); t.start(); - try { - Thread.sleep(SHORT_DELAY_MS); + try { + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); } catch (InterruptedException ie) { - unexpectedException(); + unexpectedException(); } } @@ -443,7 +469,7 @@ public class LinkedBlockingQueueTest ext for (int i = 0; i < SIZE; ++i) { assertEquals(i, ((Integer)q.poll()).intValue()); } - assertNull(q.poll()); + assertNull(q.poll()); } /** @@ -453,12 +479,12 @@ public class LinkedBlockingQueueTest ext try { LinkedBlockingQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue()); + assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue()); } - assertNull(q.poll(0, TimeUnit.MILLISECONDS)); - } catch (InterruptedException e){ - unexpectedException(); - } + assertNull(q.poll(0, MILLISECONDS)); + } catch (InterruptedException e) { + unexpectedException(); + } } /** @@ -468,12 +494,12 @@ public class LinkedBlockingQueueTest ext try { LinkedBlockingQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); + assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); } - assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); - } catch (InterruptedException e){ - unexpectedException(); - } + assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); + } catch (InterruptedException e) { + unexpectedException(); + } } /** @@ -486,20 +512,20 @@ public class LinkedBlockingQueueTest ext try { LinkedBlockingQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); + threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); } - threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); - } catch (InterruptedException success){ - } + threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); + } catch (InterruptedException success) { + } }}); t.start(); - try { - Thread.sleep(SHORT_DELAY_MS); + try { + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); } catch (InterruptedException ie) { - unexpectedException(); + unexpectedException(); } } @@ -512,23 +538,23 @@ public class LinkedBlockingQueueTest ext Thread t = new Thread(new Runnable() { public void run() { try { - threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); - q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); - q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); - threadShouldThrow(); - } catch (InterruptedException success) { } + threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); + q.poll(LONG_DELAY_MS, MILLISECONDS); + q.poll(LONG_DELAY_MS, MILLISECONDS); + threadShouldThrow(); + } catch (InterruptedException success) { } } }); try { t.start(); Thread.sleep(SMALL_DELAY_MS); - assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); + assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); t.interrupt(); t.join(); - } catch (Exception e){ + } catch (Exception e) { unexpectedException(); } - } + } /** * peek returns next element, or null if empty @@ -541,7 +567,7 @@ public class LinkedBlockingQueueTest ext assertTrue(q.peek() == null || i != ((Integer)q.peek()).intValue()); } - assertNull(q.peek()); + assertNull(q.peek()); } /** @@ -571,8 +597,8 @@ public class LinkedBlockingQueueTest ext try { q.remove(); shouldThrow(); - } catch (NoSuchElementException success){ - } + } catch (NoSuchElementException success) { + } } /** @@ -589,7 +615,24 @@ 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 */ @@ -613,6 +656,7 @@ public class LinkedBlockingQueueTest ext assertEquals(SIZE, q.remainingCapacity()); q.add(one); assertFalse(q.isEmpty()); + assertTrue(q.contains(one)); q.clear(); assertTrue(q.isEmpty()); } @@ -671,13 +715,13 @@ public class LinkedBlockingQueueTest ext */ 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(); - } + Object[] o = q.toArray(); + try { + for (int i = 0; i < o.length; i++) + assertEquals(o[i], q.take()); + } catch (InterruptedException e) { + unexpectedException(); + } } /** @@ -685,29 +729,52 @@ public class LinkedBlockingQueueTest ext */ 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++) - assertEquals(ints[i], q.take()); - } catch (InterruptedException e){ - unexpectedException(); - } + 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(); + } + } + + /** + * 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()){ - assertEquals(it.next(), q.take()); - } - } catch (InterruptedException e){ - unexpectedException(); - } + Iterator it = q.iterator(); + try { + while (it.hasNext()) { + assertEquals(it.next(), q.take()); + } + } catch (InterruptedException e) { + unexpectedException(); + } } /** @@ -722,7 +789,7 @@ public class LinkedBlockingQueueTest ext Iterator it = q.iterator(); it.next(); it.remove(); - + it = q.iterator(); assertEquals(it.next(), one); assertEquals(it.next(), three); @@ -777,7 +844,7 @@ public class LinkedBlockingQueueTest ext for (int i = 0; i < SIZE; ++i) { assertTrue(s.indexOf(String.valueOf(i)) >= 0); } - } + } /** @@ -792,7 +859,7 @@ public class LinkedBlockingQueueTest ext public void run() { threadAssertFalse(q.offer(three)); try { - threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); + threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); threadAssertEquals(0, q.remainingCapacity()); } catch (InterruptedException e) { @@ -812,7 +879,7 @@ public class LinkedBlockingQueueTest ext } } }); - + joinPool(executor); } @@ -826,7 +893,7 @@ public class LinkedBlockingQueueTest ext public void run() { threadAssertNull(q.poll()); try { - threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); + threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); threadAssertTrue(q.isEmpty()); } catch (InterruptedException e) { @@ -846,7 +913,7 @@ public class LinkedBlockingQueueTest ext } } }); - + joinPool(executor); } @@ -866,11 +933,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) ; + } + } + }