--- jsr166/src/test/tck/LinkedTransferQueueTest.java 2009/07/31 23:02:49 1.1 +++ jsr166/src/test/tck/LinkedTransferQueueTest.java 2009/12/01 06:28:43 1.23 @@ -1,4 +1,3 @@ - /* * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at @@ -14,13 +13,15 @@ import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.ArrayList; import java.util.Arrays; -import java.util.ConcurrentModificationException; import java.util.Iterator; +import java.util.List; import java.util.NoSuchElementException; import java.util.concurrent.*; +import static java.util.concurrent.TimeUnit.MILLISECONDS; import junit.framework.Test; import junit.framework.TestSuite; +@SuppressWarnings({"unchecked", "rawtypes"}) public class LinkedTransferQueueTest extends JSR166TestCase { public static void main(String[] args) { @@ -31,138 +32,163 @@ public class LinkedTransferQueueTest ext return new TestSuite(LinkedTransferQueueTest.class); } - /* - *Constructor builds new queue with size being zero and empty being true + void checkEmpty(LinkedTransferQueue q) throws InterruptedException { + assertTrue(q.isEmpty()); + assertEquals(0, q.size()); + assertNull(q.peek()); + assertNull(q.poll()); + assertNull(q.poll(0, MILLISECONDS)); + assertEquals(q.toString(), "[]"); + assertTrue(Arrays.equals(q.toArray(), new Object[0])); + assertFalse(q.iterator().hasNext()); + try { + q.element(); + shouldThrow(); + } catch (NoSuchElementException success) {} + try { + q.iterator().next(); + shouldThrow(); + } catch (NoSuchElementException success) {} + try { + q.remove(); + shouldThrow(); + } catch (NoSuchElementException success) {} + } + + /** + * Constructor builds new queue with size being zero and empty + * being true */ public void testConstructor1() { assertEquals(0, new LinkedTransferQueue().size()); assertTrue(new LinkedTransferQueue().isEmpty()); } - /* - * Initizialing constructor with null collection throws NPE + /** + * Initializing constructor with null collection throws + * NullPointerException */ public void testConstructor2() { try { new LinkedTransferQueue(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 testConstructor3() { try { Integer[] ints = new Integer[SIZE]; - LinkedTransferQueue q = new LinkedTransferQueue(Arrays.asList(ints)); + new LinkedTransferQueue(Arrays.asList(ints)); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } - /* + + /** * Initializing constructor with a collection containing some null elements - * throws NPE + * throws NullPointerException */ - public void testConstructor4() { try { Integer[] ints = new Integer[SIZE]; for (int i = 0; i < SIZE - 1; ++i) { - ints[i] = new Integer(i); + ints[i] = i; } - LinkedTransferQueue q = new LinkedTransferQueue(Arrays.asList(ints)); + new LinkedTransferQueue(Arrays.asList(ints)); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } - /* + /** * Queue contains all elements of the collection it is initialized by */ public void testConstructor5() { - try { - Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE; ++i) { - ints[i] = new Integer(i); - } - LinkedTransferQueue q = new LinkedTransferQueue(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] = i; + } + List intList = Arrays.asList(ints); + LinkedTransferQueue q + = new LinkedTransferQueue(intList); + assertEquals(q.size(), intList.size()); + assertEquals(q.toString(), intList.toString()); + assertTrue(Arrays.equals(q.toArray(), + intList.toArray())); + assertTrue(Arrays.equals(q.toArray(new Object[0]), + intList.toArray(new Object[0]))); + assertTrue(Arrays.equals(q.toArray(new Object[SIZE]), + intList.toArray(new Object[SIZE]))); + for (int i = 0; i < SIZE; ++i) { + assertEquals(ints[i], q.poll()); } } /** - * Remaining capacity never decrease nor increase on add or remove + * remainingCapacity() always returns Integer.MAX_VALUE */ public void testRemainingCapacity() { - LinkedTransferQueue q = populatedQueue(SIZE); - int remainingCapacity = q.remainingCapacity(); + LinkedTransferQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(remainingCapacity, q.remainingCapacity()); + assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); assertEquals(SIZE - i, q.size()); q.remove(); } for (int i = 0; i < SIZE; ++i) { - assertEquals(remainingCapacity, q.remainingCapacity()); + assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); assertEquals(i, q.size()); - q.add(new Integer(i)); + q.add(i); } } /** - * offer(null) throws NPE + * offer(null) throws NullPointerException */ public void testOfferNull() { try { LinkedTransferQueue q = new LinkedTransferQueue(); q.offer(null); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** - * add(null) throws NPE + * add(null) throws NullPointerException */ public void testAddNull() { try { LinkedTransferQueue q = new LinkedTransferQueue(); q.add(null); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** - * addAll(null) throws NPE + * addAll(null) throws NullPointerException */ public void testAddAll1() { try { LinkedTransferQueue q = new LinkedTransferQueue(); q.addAll(null); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** - * addAll(this) throws IAE + * addAll(this) throws IllegalArgumentException */ public void testAddAllSelf() { try { LinkedTransferQueue q = populatedQueue(SIZE); q.addAll(q); shouldThrow(); - } catch (IllegalArgumentException success) { - } + } catch (IllegalArgumentException success) {} } /** - * addAll of a collection with null elements throws NPE + * addAll of a collection with null elements throws NullPointerException */ public void testAddAll2() { try { @@ -170,364 +196,319 @@ public class LinkedTransferQueueTest ext Integer[] ints = new Integer[SIZE]; q.addAll(Arrays.asList(ints)); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** - * addAll of a collection with any null elements throws NPE after - * possibly adding some elements + * addAll of a collection with any null elements throws + * NullPointerException after possibly adding some elements */ public void testAddAll3() { try { LinkedTransferQueue q = new LinkedTransferQueue(); Integer[] ints = new Integer[SIZE]; for (int i = 0; i < SIZE - 1; ++i) { - ints[i] = new Integer(i); + ints[i] = i; } q.addAll(Arrays.asList(ints)); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException 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); - } - LinkedTransferQueue q = new LinkedTransferQueue(); - 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] = i; + } + LinkedTransferQueue q = new LinkedTransferQueue(); + 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 + * put(null) throws NullPointerException */ - public void testPutNull() { + public void testPutNull() throws InterruptedException { try { LinkedTransferQueue q = new LinkedTransferQueue(); q.put(null); shouldThrow(); - } catch (NullPointerException success) { - } catch (Exception ie) { - unexpectedException(); - } + } catch (NullPointerException success) {} } /** * all elements successfully put are contained */ public void testPut() { - try { - LinkedTransferQueue q = new LinkedTransferQueue(); - for (int i = 0; i < SIZE; ++i) { - Integer I = new Integer(i); - q.put(I); - assertTrue(q.contains(I)); - } - } catch (Exception ie) { - unexpectedException(); + LinkedTransferQueue q = new LinkedTransferQueue(); + for (int i = 0; i < SIZE; ++i) { + assertEquals(q.size(), i); + q.put(i); + assertTrue(q.contains(i)); } } /** * take retrieves elements in FIFO order */ - public void testTake() { - try { - LinkedTransferQueue 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 { + LinkedTransferQueue q = populatedQueue(SIZE); + for (int i = 0; i < SIZE; ++i) { + assertEquals(i, (int) q.take()); } } /** * take blocks interruptibly when empty */ - public void testTakeFromEmpty() { + public void testTakeFromEmpty() throws InterruptedException { final LinkedTransferQueue q = new LinkedTransferQueue(); - Thread t = new Thread(new Runnable() { - - public void run() { - try { - q.take(); - threadShouldThrow(); - } catch (InterruptedException success) { - } - } - }); - try { - t.start(); - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); - } catch (Exception e) { - unexpectedException(); - } + Thread t = newStartedThread(new CheckedInterruptedRunnable() { + public void realRun() throws InterruptedException { + q.take(); + }}); + Thread.sleep(SHORT_DELAY_MS); + t.interrupt(); + t.join(); } - /* + + /** * Take removes existing elements until empty, then blocks interruptibly */ - - public void testBlockingTake() { - Thread t = new Thread(new Runnable() { - - public void run() { + public void testBlockingTake() throws InterruptedException { + final LinkedTransferQueue q = populatedQueue(SIZE); + Thread t = new Thread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + for (int i = 0; i < SIZE; ++i) { + assertEquals(i, (int) q.take()); + } try { - LinkedTransferQueue q = populatedQueue(SIZE); - for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer) q.take()).intValue()); - } q.take(); - threadShouldThrow(); - } catch (InterruptedException success) { - } - } - }); + shouldThrow(); + } catch (InterruptedException success) {} + }}); + t.start(); - try { - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); - } catch (InterruptedException ie) { - unexpectedException(); - } + Thread.sleep(SHORT_DELAY_MS); + t.interrupt(); + t.join(); + checkEmpty(q); } /** * poll succeeds unless empty */ - public void testPoll() { - LinkedTransferQueue q = populatedQueue(SIZE); + public void testPoll() throws InterruptedException { + LinkedTransferQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer) q.poll()).intValue()); + assertEquals(i, (int) q.poll()); } assertNull(q.poll()); + checkEmpty(q); } /** * timed pool with zero timeout succeeds when non-empty, else times out */ - public void testTimedPoll0() { - try { - LinkedTransferQueue q = populatedQueue(SIZE); - for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer) q.poll(0, TimeUnit.MILLISECONDS)).intValue()); - } - assertNull(q.poll(0, TimeUnit.MILLISECONDS)); - } catch (InterruptedException e) { - unexpectedException(); + public void testTimedPoll0() throws InterruptedException { + LinkedTransferQueue q = populatedQueue(SIZE); + for (int i = 0; i < SIZE; ++i) { + assertEquals(i, (int) q.poll(0, MILLISECONDS)); } + assertNull(q.poll(0, MILLISECONDS)); + checkEmpty(q); } /** * timed pool with nonzero timeout succeeds when non-empty, else times out */ - public void testTimedPoll() { - try { - LinkedTransferQueue q = populatedQueue(SIZE); - for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer) q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); - } - assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); - } catch (InterruptedException e) { - unexpectedException(); + public void testTimedPoll() throws InterruptedException { + LinkedTransferQueue q = populatedQueue(SIZE); + for (int i = 0; i < SIZE; ++i) { + long t0 = System.nanoTime(); + assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); + long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024); + assertTrue(millisElapsed < SMALL_DELAY_MS); } + assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); + 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 { - LinkedTransferQueue q = populatedQueue(SIZE); - for (int i = 0; i < SIZE; ++i) { - threadAssertEquals(i, ((Integer) q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); - } - threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); - } catch (InterruptedException success) { + public void testInterruptedTimedPoll() throws InterruptedException { + final LinkedTransferQueue q = populatedQueue(SIZE); + 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)); + long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024); + assertTrue(millisElapsed < SMALL_DELAY_MS); } - } - }); - t.start(); - try { - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); - } catch (InterruptedException ie) { - unexpectedException(); - } + try { + q.poll(LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + }}); + + Thread.sleep(SMALL_DELAY_MS); + t.interrupt(); + t.join(); + checkEmpty(q); } /** - * timed poll before a delayed offer fails; after offer succeeds; - * on interruption throws + * timed poll before a delayed offer fails; after offer succeeds; + * on interruption throws */ - public void testTimedPollWithOffer() { + public void testTimedPollWithOffer() throws InterruptedException { final LinkedTransferQueue q = new LinkedTransferQueue(); - Thread t = new Thread(new Runnable() { - - public void run() { + Thread t = new Thread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); + assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); 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) { - } - } - }); - try { - t.start(); - Thread.sleep(SMALL_DELAY_MS); - assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); - t.interrupt(); - t.join(); - } catch (Exception e) { - unexpectedException(); - } + q.poll(LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + }}); + + t.start(); + Thread.sleep(SMALL_DELAY_MS); + assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); + t.interrupt(); + t.join(); } /** * peek returns next element, or null if empty */ - public void testPeek() { - LinkedTransferQueue q = populatedQueue(SIZE); + public void testPeek() throws InterruptedException { + LinkedTransferQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer) q.peek()).intValue()); - q.poll(); + assertEquals(i, (int) q.peek()); + assertEquals(i, (int) q.poll()); assertTrue(q.peek() == null || - i != ((Integer) q.peek()).intValue()); + i != (int) q.peek()); } assertNull(q.peek()); + checkEmpty(q); } /** - * element returns next element, or throws NSEE if empty + * element returns next element, or throws NoSuchElementException if empty */ - public void testElement() { - LinkedTransferQueue q = populatedQueue(SIZE); + public void testElement() throws InterruptedException { + LinkedTransferQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer) q.element()).intValue()); - q.poll(); + assertEquals(i, (int) q.element()); + assertEquals(i, (int) q.poll()); } try { q.element(); shouldThrow(); - } catch (NoSuchElementException success) { - } + } catch (NoSuchElementException success) {} + checkEmpty(q); } /** - * remove removes next element, or throws NSEE if empty + * remove removes next element, or throws NoSuchElementException if empty */ - public void testRemove() { - LinkedTransferQueue q = populatedQueue(SIZE); + public void testRemove() throws InterruptedException { + LinkedTransferQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer) q.remove()).intValue()); + assertEquals(i, (int) q.remove()); } try { q.remove(); shouldThrow(); - } catch (NoSuchElementException success) { - } + } catch (NoSuchElementException success) {} + checkEmpty(q); } /** * remove(x) removes x and returns true if present */ - public void testRemoveElement() { + public void testRemoveElement() throws InterruptedException { LinkedTransferQueue q = populatedQueue(SIZE); for (int i = 1; i < SIZE; i += 2) { - assertTrue(q.remove(new Integer(i))); + assertTrue(q.remove(i)); } for (int i = 0; i < SIZE; i += 2) { - assertTrue(q.remove(new Integer(i))); - assertFalse(q.remove(new Integer(i + 1))); + assertTrue(q.remove(i)); + assertFalse(q.remove(i + 1)); } - assertTrue(q.isEmpty()); + checkEmpty(q); } /** * An add following remove(x) succeeds */ - public void testRemoveElementAndAdd() { - try { - LinkedTransferQueue q = new LinkedTransferQueue(); - 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 { + LinkedTransferQueue q = new LinkedTransferQueue(); + assertTrue(q.add(one)); + assertTrue(q.add(two)); + assertTrue(q.remove(one)); + assertTrue(q.remove(two)); + assertTrue(q.add(three)); + assertSame(q.take(), three); } /** * contains(x) reports true when elements added but not yet removed */ public void testContains() { - LinkedTransferQueue q = populatedQueue(SIZE); + LinkedTransferQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertTrue(q.contains(new Integer(i))); - q.poll(); - assertFalse(q.contains(new Integer(i))); + assertTrue(q.contains(i)); + assertEquals(i, (int) q.poll()); + assertFalse(q.contains(i)); } } /** * clear removes all elements */ - public void testClear() { + public void testClear() throws InterruptedException { LinkedTransferQueue q = populatedQueue(SIZE); - int remainingCapacity = q.remainingCapacity(); q.clear(); - assertTrue(q.isEmpty()); - assertEquals(0, q.size()); - assertEquals(remainingCapacity, q.remainingCapacity()); + checkEmpty(q); + assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); q.add(one); assertFalse(q.isEmpty()); + assertEquals(1, q.size()); assertTrue(q.contains(one)); q.clear(); - assertTrue(q.isEmpty()); + checkEmpty(q); } /** * containsAll(c) is true when c contains a subset of elements */ public void testContainsAll() { - LinkedTransferQueue q = populatedQueue(SIZE); - LinkedTransferQueue p = new LinkedTransferQueue(); + LinkedTransferQueue q = populatedQueue(SIZE); + LinkedTransferQueue p = new LinkedTransferQueue(); for (int i = 0; i < SIZE; ++i) { assertTrue(q.containsAll(p)); assertFalse(p.containsAll(q)); - p.add(new Integer(i)); + p.add(i); } assertTrue(p.containsAll(q)); } /** - * retainAll(c) retains only those elements of c and reports true if changed + * retainAll(c) retains only those elements of c and reports true + * if changed */ public void testRetainAll() { LinkedTransferQueue q = populatedQueue(SIZE); @@ -546,7 +527,8 @@ public class LinkedTransferQueueTest ext } /** - * removeAll(c) removes only those elements of c and reports true if changed + * removeAll(c) removes only those elements of c and reports true + * if changed */ public void testRemoveAll() { for (int i = 1; i < SIZE; ++i) { @@ -555,84 +537,71 @@ public class LinkedTransferQueueTest ext 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)); + assertFalse(q.contains(p.remove())); } } } /** - * toArray contains all elements + * toArray() contains all elements */ - public void testToArray() { + public void testToArray() throws InterruptedException { LinkedTransferQueue 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(); + for (int i = 0; i < o.length; i++) { + assertEquals(o[i], q.take()); } } /** * toArray(a) contains all elements */ - public void testToArray2() { - LinkedTransferQueue q = populatedQueue(SIZE); + public void testToArray2() throws InterruptedException { + LinkedTransferQueue 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(); + ints = q.toArray(ints); + for (int i = 0; i < ints.length; i++) { + assertEquals(ints[i], q.take()); } } /** - * toArray(null) throws NPE + * toArray(null) throws NullPointerException */ public void testToArray_BadArg() { + LinkedTransferQueue q = populatedQueue(SIZE); try { - LinkedTransferQueue q = populatedQueue(SIZE); Object o[] = q.toArray(null); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** - * toArray with incompatible array type throws CCE + * toArray(incompatible array type) throws CCE */ public void testToArray1_BadArg() { + LinkedTransferQueue q = populatedQueue(SIZE); try { - LinkedTransferQueue q = populatedQueue(SIZE); Object o[] = 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 { LinkedTransferQueue q = populatedQueue(SIZE); Iterator it = q.iterator(); - try { - while (it.hasNext()) { - assertEquals(it.next(), q.take()); - } - } catch (InterruptedException e) { - unexpectedException(); + int i = 0; + while (it.hasNext()) { + assertEquals(it.next(), i++); } + assertEquals(i, SIZE); } /** - * iterator.remove removes current element + * iterator.remove() removes current element */ public void testIteratorRemove() { final LinkedTransferQueue q = new LinkedTransferQueue(); @@ -654,16 +623,16 @@ public class LinkedTransferQueueTest ext * iterator ordering is FIFO */ public void testIteratorOrdering() { - final LinkedTransferQueue q = new LinkedTransferQueue(); - int remainingCapacity = q.remainingCapacity(); + final LinkedTransferQueue q + = new LinkedTransferQueue(); + assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); q.add(one); q.add(two); q.add(three); - assertEquals(remainingCapacity, q.remainingCapacity()); + assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); int k = 0; - for (Iterator it = q.iterator(); it.hasNext();) { - int i = ((Integer) (it.next())).intValue(); - assertEquals(++k, i); + for (Integer n : q) { + assertEquals(++k, (int) n); } assertEquals(3, k); } @@ -676,13 +645,9 @@ public class LinkedTransferQueueTest ext 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()); } @@ -706,62 +671,40 @@ public class LinkedTransferQueueTest ext q.add(one); q.add(two); ExecutorService executor = Executors.newFixedThreadPool(2); - executor.execute(new Runnable() { - public void run() { - try { - threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); - } catch (Exception e) { - threadUnexpectedException(); - } - } - }); - - executor.execute(new Runnable() { - - public void run() { - try { - Thread.sleep(SMALL_DELAY_MS); - threadAssertEquals(one, q.take()); - } catch (InterruptedException e) { - threadUnexpectedException(); - } - } - }); + executor.execute(new CheckedRunnable() { + public void realRun() { + assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); + }}); + + executor.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { + Thread.sleep(SMALL_DELAY_MS); + assertEquals(one, q.take()); + }}); joinPool(executor); } /** - * poll retrieves elements across Executor threads + * timed poll retrieves elements across Executor threads */ public void testPollInExecutor() { final LinkedTransferQueue q = new LinkedTransferQueue(); ExecutorService executor = Executors.newFixedThreadPool(2); - executor.execute(new Runnable() { - - public void run() { - threadAssertNull(q.poll()); - try { - threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.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()); + assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); + assertTrue(q.isEmpty()); + }}); + + executor.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { + Thread.sleep(SMALL_DELAY_MS); + q.put(one); + }}); joinPool(executor); } @@ -769,50 +712,47 @@ public class LinkedTransferQueueTest ext /** * A deserialized serialized queue has same elements in same order */ - public void testSerialization() { + public void testSerialization() throws Exception { LinkedTransferQueue 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)); - LinkedTransferQueue r = (LinkedTransferQueue) in.readObject(); - - assertEquals(q.size(), r.size()); - while (!q.isEmpty()) { - assertEquals(q.remove(), r.remove()); - } - } catch (Exception e) { - unexpectedException(); + 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)); + LinkedTransferQueue r = (LinkedTransferQueue) in.readObject(); + + assertEquals(q.size(), r.size()); + while (!q.isEmpty()) { + assertEquals(q.remove(), r.remove()); } } /** - * drainTo(null) throws NPE + * drainTo(null) throws NullPointerException */ public void testDrainToNull() { LinkedTransferQueue q = populatedQueue(SIZE); try { q.drainTo(null); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** - * drainTo(this) throws IAE + * drainTo(this) throws IllegalArgumentException */ public void testDrainToSelf() { LinkedTransferQueue q = populatedQueue(SIZE); try { q.drainTo(q); shouldThrow(); - } catch (IllegalArgumentException success) { - } + } catch (IllegalArgumentException success) {} } /** @@ -825,7 +765,7 @@ public class LinkedTransferQueueTest ext assertEquals(q.size(), 0); assertEquals(l.size(), SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(l.get(i), new Integer(i)); + assertEquals(l.get(i), i); } q.add(zero); q.add(one); @@ -837,62 +777,49 @@ public class LinkedTransferQueueTest ext assertEquals(q.size(), 0); assertEquals(l.size(), 2); for (int i = 0; i < 2; ++i) { - assertEquals(l.get(i), new Integer(i)); + assertEquals(l.get(i), i); } } /** - * drainTo empties full queue, unblocking a waiting put. + * drainTo(c) empties full queue, unblocking a waiting put. */ - public void testDrainToWithActivePut() { + public void testDrainToWithActivePut() throws InterruptedException { final LinkedTransferQueue q = populatedQueue(SIZE); - Thread t = new Thread(new Runnable() { - - public void run() { - try { - q.put(new Integer(SIZE + 1)); - } catch (Exception 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(); + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() { + q.put(SIZE + 1); + }}); + ArrayList l = new ArrayList(); + q.drainTo(l); + assertTrue(l.size() >= SIZE); + for (int i = 0; i < SIZE; ++i) { + assertEquals(l.get(i), i); } + t.join(); + assertTrue(q.size() + l.size() >= SIZE); } /** - * drainTo(null, n) throws NPE + * drainTo(null, n) throws NullPointerException */ public void testDrainToNullN() { LinkedTransferQueue q = populatedQueue(SIZE); try { - q.drainTo(null, 0); + q.drainTo(null, SIZE); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** - * drainTo(this, n) throws IAE + * drainTo(this, n) throws IllegalArgumentException */ public void testDrainToSelfN() { LinkedTransferQueue q = populatedQueue(SIZE); try { - q.drainTo(q, 0); + q.drainTo(q, SIZE); shouldThrow(); - } catch (IllegalArgumentException success) { - } + } catch (IllegalArgumentException success) {} } /** @@ -902,7 +829,7 @@ public class LinkedTransferQueueTest ext LinkedTransferQueue q = new LinkedTransferQueue(); for (int i = 0; i < SIZE + 2; ++i) { for (int j = 0; j < SIZE; j++) { - assertTrue(q.offer(new Integer(j))); + assertTrue(q.offer(j)); } ArrayList l = new ArrayList(); q.drainTo(l, i); @@ -910,338 +837,293 @@ public class LinkedTransferQueueTest ext assertEquals(l.size(), k); assertEquals(q.size(), SIZE - k); for (int j = 0; j < k; ++j) { - assertEquals(l.get(j), new Integer(j)); + assertEquals(l.get(j), j); } - while (q.poll() != null); + while (q.poll() != null) + ; } } - /* - * poll and take should decrement the waiting consumer count + /** + * timed poll() or take() increments the waiting consumer count; + * offer(e) decrements the waiting consumer count */ - public void testWaitingConsumer() { - try { - final LinkedTransferQueue q = new LinkedTransferQueue(); - final ConsumerObserver waiting = new ConsumerObserver(); - new Thread(new Runnable() { + public void testWaitingConsumer() throws InterruptedException { + final LinkedTransferQueue q = new LinkedTransferQueue(); + assertEquals(q.getWaitingConsumerCount(), 0); + assertFalse(q.hasWaitingConsumer()); - public void run() { - try { - threadAssertTrue(q.hasWaitingConsumer()); - waiting.setWaitingConsumer(q.getWaitingConsumerCount()); - threadAssertTrue(q.offer(new Object())); - } catch (Exception ex) { - threadUnexpectedException(); - } + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + Thread.sleep(SMALL_DELAY_MS); + assertTrue(q.hasWaitingConsumer()); + assertEquals(q.getWaitingConsumerCount(), 1); + assertTrue(q.offer(one)); + assertFalse(q.hasWaitingConsumer()); + assertEquals(q.getWaitingConsumerCount(), 0); + }}); - } - }).start(); - assertTrue(q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS) != null); - assertTrue(q.getWaitingConsumerCount() < waiting.getWaitingConsumers()); - } catch (Exception ex) { - this.unexpectedException(); - } + assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); + assertEquals(q.getWaitingConsumerCount(), 0); + assertFalse(q.hasWaitingConsumer()); + t.join(); } - /* - * Inserts null into transfer throws NPE - */ - public void testTransfer1() { + /** + * transfer(null) throws NullPointerException + */ + public void testTransfer1() throws InterruptedException { try { LinkedTransferQueue q = new LinkedTransferQueue(); q.transfer(null); shouldThrow(); - } catch (NullPointerException ex) { - } catch (Exception ex) { - this.unexpectedException(); - } + } catch (NullPointerException success) {} } - /* - * transfer attempts to insert into the queue then wait until that - * object is removed via take or poll. + /** + * transfer waits until a poll occurs. The transfered element + * is returned by this associated poll. */ - public void testTransfer2() { - final LinkedTransferQueue q = new LinkedTransferQueue(); - new Thread(new Runnable() { + public void testTransfer2() throws InterruptedException { + final LinkedTransferQueue q + = new LinkedTransferQueue(); - public void run() { - try { - q.transfer(new Integer(SIZE)); - threadAssertTrue(q.isEmpty()); - } catch (Exception ex) { - threadUnexpectedException(); - } - } - }).start(); + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + q.transfer(SIZE); + threadAssertTrue(q.isEmpty()); + }}); - try { - Thread.sleep(SHORT_DELAY_MS); - assertEquals(1, q.size()); - q.poll(); - assertTrue(q.isEmpty()); - } catch (Exception ex) { - this.unexpectedException(); - } - } - /* - * transfer will attempt to transfer in fifo order and continue waiting if - * the element being transfered is not polled or taken - */ - - public void testTransfer3() { - final LinkedTransferQueue q = new LinkedTransferQueue(); - new Thread(new Runnable() { - public void run() { - try { - Integer i; - q.transfer((i = new Integer(SIZE + 1))); - threadAssertTrue(!q.contains(i)); - threadAssertEquals(1, q.size()); - } catch (Exception ex) { - threadUnexpectedException(); - } - } - }).start(); - Thread interruptedThread = - new Thread(new Runnable() { - public void run() { - try { - q.transfer(new Integer(SIZE)); - threadShouldThrow(); - } catch (InterruptedException ex) { - } - } - }); - interruptedThread.start(); - try { - Thread.sleep(LONG_DELAY_MS); - assertEquals(2, q.size()); - q.poll(); - Thread.sleep(LONG_DELAY_MS); - interruptedThread.interrupt(); - assertEquals(1, q.size()); - } catch (Exception ex) { - this.unexpectedException(); - } + Thread.sleep(SHORT_DELAY_MS); + assertEquals(1, q.size()); + assertEquals(SIZE, (int) q.poll()); + assertTrue(q.isEmpty()); + t.join(); + } + + /** + * transfer waits until a poll occurs, and then transfers in fifo order + */ + public void testTransfer3() throws InterruptedException { + final LinkedTransferQueue q + = new LinkedTransferQueue(); + + Thread first = newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + Integer i = SIZE + 1; + q.transfer(i); + threadAssertTrue(!q.contains(i)); + threadAssertEquals(1, q.size()); + }}); + + Thread interruptedThread = newStartedThread( + new CheckedInterruptedRunnable() { + public void realRun() throws InterruptedException { + while (q.size() == 0) + Thread.yield(); + q.transfer(SIZE); + }}); + + while (q.size() < 2) + Thread.yield(); + assertEquals(2, q.size()); + assertEquals(SIZE + 1, (int) q.poll()); + first.join(); + assertEquals(1, q.size()); + interruptedThread.interrupt(); + interruptedThread.join(); + assertEquals(0, q.size()); + assertTrue(q.isEmpty()); } /** - * transfer will wait as long as a poll or take occurs if one does occur - * the waiting is finished and the thread that tries to poll/take - * wins in retrieving the element + * transfer waits until a poll occurs, at which point the polling + * thread returns the element */ - public void testTransfer4() { + public void testTransfer4() throws InterruptedException { final LinkedTransferQueue q = new LinkedTransferQueue(); - new Thread(new Runnable() { - public void run() { - try { - q.transfer(new Integer(four)); - threadAssertFalse(q.contains(new Integer(four))); - threadAssertEquals(new Integer(three), q.poll()); - } catch (Exception ex) { - threadUnexpectedException(); - } - } - }).start(); - try { - Thread.sleep(MEDIUM_DELAY_MS); - assertTrue(q.offer(three)); - assertEquals(new Integer(four), q.poll()); - } catch (Exception ex) { - this.unexpectedException(); - } + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + q.transfer(four); + threadAssertFalse(q.contains(four)); + threadAssertEquals(three, q.poll()); + }}); + + Thread.sleep(SHORT_DELAY_MS); + assertTrue(q.offer(three)); + assertEquals(four, q.poll()); + t.join(); } - /* - * Insert null into trTransfer throws NPE + + /** + * transfer waits until a take occurs. The transfered element + * is returned by this associated take. */ + public void testTransfer5() throws InterruptedException { + final LinkedTransferQueue q + = new LinkedTransferQueue(); + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + q.transfer(SIZE); + checkEmpty(q); + }}); + + Thread.sleep(SHORT_DELAY_MS); + assertEquals(SIZE, (int) q.take()); + checkEmpty(q); + t.join(); + } + + /** + * tryTransfer(null) throws NullPointerException + */ public void testTryTransfer1() { try { final LinkedTransferQueue q = new LinkedTransferQueue(); q.tryTransfer(null); - this.shouldThrow(); - } catch (NullPointerException ex) { - } catch (Exception ex) { - this.unexpectedException(); - } + shouldThrow(); + } catch (NullPointerException success) {} } - /* - * tryTransfer returns false and does not enqueue if there are no consumers - * waiting to poll or take. - */ - public void testTryTransfer2() { - try { - final LinkedTransferQueue q = new LinkedTransferQueue(); - assertFalse(q.tryTransfer(new Object())); - assertEquals(0, q.size()); - } catch (Exception ex) { - this.unexpectedException(); - } + /** + * tryTransfer returns false and does not enqueue if there are no + * consumers waiting to poll or take. + */ + public void testTryTransfer2() throws InterruptedException { + final LinkedTransferQueue q = new LinkedTransferQueue(); + assertFalse(q.tryTransfer(new Object())); + assertFalse(q.hasWaitingConsumer()); + checkEmpty(q); } - /* - * if there is a consumer waiting poll or take tryTransfer returns - * true while enqueueing object + + /** + * If there is a consumer waiting in timed poll, tryTransfer + * returns true while successfully transfering object. */ + public void testTryTransfer3() throws InterruptedException { + final Object hotPotato = new Object(); + final LinkedTransferQueue q = new LinkedTransferQueue(); - public void testTryTransfer3() { - try { - final LinkedTransferQueue q = new LinkedTransferQueue(); - new Thread(new Runnable() { + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() { + while (! q.hasWaitingConsumer()) + Thread.yield(); + threadAssertTrue(q.hasWaitingConsumer()); + threadAssertTrue(q.isEmpty()); + threadAssertTrue(q.size() == 0); + threadAssertTrue(q.tryTransfer(hotPotato)); + }}); + + assertSame(hotPotato, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); + checkEmpty(q); + t.join(); + } - public void run() { - try { - threadAssertTrue(q.hasWaitingConsumer()); - threadAssertTrue(q.tryTransfer(new Object())); - } catch (Exception ex) { - threadUnexpectedException(); - } + /** + * If there is a consumer waiting in take, tryTransfer returns + * true while successfully transfering object. + */ + public void testTryTransfer4() throws InterruptedException { + final Object hotPotato = new Object(); + final LinkedTransferQueue q = new LinkedTransferQueue(); - } - }).start(); - assertTrue(q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS) != null); - assertTrue(q.isEmpty()); - } catch (Exception ex) { - this.unexpectedException(); - } + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() { + while (! q.hasWaitingConsumer()) + Thread.yield(); + threadAssertTrue(q.hasWaitingConsumer()); + threadAssertTrue(q.isEmpty()); + threadAssertTrue(q.size() == 0); + threadAssertTrue(q.tryTransfer(hotPotato)); + }}); + + assertSame(q.take(), hotPotato); + checkEmpty(q); + t.join(); } - /* - * tryTransfer waits the amount given if interrupted, show an - * interrupted exception + /** + * tryTransfer waits the amount given if interrupted, and + * throws interrupted exception */ - public void testTryTransfer4() { + public void testTryTransfer5() throws InterruptedException { final LinkedTransferQueue q = new LinkedTransferQueue(); - Thread toInterrupt = new Thread(new Runnable() { - public void run() { - try { - q.tryTransfer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); - threadShouldThrow(); - } catch (InterruptedException ex) { - } - } - }); - try { - toInterrupt.start(); - Thread.sleep(SMALL_DELAY_MS); - toInterrupt.interrupt(); - } catch (Exception ex) { - this.unexpectedException(); - } + Thread toInterrupt = newStartedThread(new CheckedInterruptedRunnable() { + public void realRun() throws InterruptedException { + q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); + }}); + + Thread.sleep(SMALL_DELAY_MS); + toInterrupt.interrupt(); + toInterrupt.join(); } - /* + /** * tryTransfer gives up after the timeout and return false */ - public void testTryTransfer5() { + public void testTryTransfer6() throws InterruptedException { final LinkedTransferQueue q = new LinkedTransferQueue(); - try { - new Thread(new Runnable() { - public void run() { - try { - threadAssertFalse(q.tryTransfer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); - } catch (InterruptedException ex) { - threadUnexpectedException(); - } - } - }).start(); - Thread.sleep(LONG_DELAY_MS); - assertTrue(q.isEmpty()); - } catch (Exception ex) { - this.unexpectedException(); - } + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + assertFalse(q.tryTransfer(new Object(), + SHORT_DELAY_MS, MILLISECONDS)); + }}); + + Thread.sleep(SMALL_DELAY_MS); + checkEmpty(q); + t.join(); } - /* + /** * tryTransfer waits for any elements previously in to be removed * before transfering to a poll or take */ - public void testTryTransfer6() { + public void testTryTransfer7() throws InterruptedException { final LinkedTransferQueue q = new LinkedTransferQueue(); - q.offer(new Integer(four)); - new Thread(new Runnable() { + assertTrue(q.offer(four)); - public void run() { - try { - threadAssertTrue(q.tryTransfer(new Integer(five), LONG_DELAY_MS, TimeUnit.MILLISECONDS)); - threadAssertTrue(q.isEmpty()); - } catch (InterruptedException ex) { - threadUnexpectedException(); - } - } - }).start(); - try { - Thread.sleep(SHORT_DELAY_MS); - assertEquals(2, q.size()); - assertEquals(new Integer(four), q.poll()); - assertEquals(new Integer(five), q.poll()); - assertTrue(q.isEmpty()); - } catch (Exception ex) { - this.unexpectedException(); - } + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + assertTrue(q.tryTransfer(five, MEDIUM_DELAY_MS, MILLISECONDS)); + assertTrue(q.isEmpty()); + }}); + + Thread.sleep(SHORT_DELAY_MS); + assertEquals(2, q.size()); + assertEquals(four, q.poll()); + assertEquals(five, q.poll()); + checkEmpty(q); + t.join(); } - /* - * tryTransfer attempts to enqueue into the q and fails returning false not - * enqueueing and the successing poll is null + /** + * tryTransfer attempts to enqueue into the q and fails returning + * false not enqueueing and the successive poll is null */ - public void testTryTransfer7() { + public void testTryTransfer8() throws InterruptedException { final LinkedTransferQueue q = new LinkedTransferQueue(); - q.offer(new Integer(four)); - new Thread(new Runnable() { - - public void run() { - try { - threadAssertFalse(q.tryTransfer(new Integer(five), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); - threadAssertTrue(q.isEmpty()); - } catch (InterruptedException ex) { - threadUnexpectedException(); - } - } - }).start(); - try { - assertEquals(1, q.size()); - assertEquals(new Integer(four), q.poll()); - Thread.sleep(MEDIUM_DELAY_MS); - assertNull(q.poll()); - } catch (Exception ex) { - this.unexpectedException(); - } + assertTrue(q.offer(four)); + assertEquals(1, q.size()); + assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS)); + assertEquals(1, q.size()); + assertEquals(four, q.poll()); + assertNull(q.poll()); + checkEmpty(q); } - private LinkedTransferQueue populatedQueue( - int n) { - LinkedTransferQueue q = new LinkedTransferQueue(); + private LinkedTransferQueue populatedQueue(int n) { + LinkedTransferQueue q = new LinkedTransferQueue(); assertTrue(q.isEmpty()); - int remainingCapacity = q.remainingCapacity(); - for (int i = 0; i < - n; i++) { + for (int i = 0; i < n; i++) { + assertEquals(i, q.size()); assertTrue(q.offer(i)); + assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); } - assertFalse(q.isEmpty()); - assertEquals(remainingCapacity, q.remainingCapacity()); - assertEquals(n, q.size()); return q; } - - private static class ConsumerObserver { - - private int waitingConsumers; - - private ConsumerObserver() { - } - - private void setWaitingConsumer(int i) { - this.waitingConsumers = i; - } - - private int getWaitingConsumers() { - return waitingConsumers; - } - } }