--- jsr166/src/test/tck/LinkedBlockingDequeTest.java 2010/09/29 12:33:48 1.23 +++ jsr166/src/test/tck/LinkedBlockingDequeTest.java 2011/03/15 19:47:06 1.35 @@ -1,7 +1,7 @@ /* * 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/ */ import junit.framework.*; @@ -11,20 +11,36 @@ import static java.util.concurrent.TimeU import java.io.*; public class LinkedBlockingDequeTest extends JSR166TestCase { + + public static class Unbounded extends BlockingQueueTest { + protected BlockingQueue emptyCollection() { + return new LinkedBlockingDeque(); + } + } + + public static class Bounded extends BlockingQueueTest { + protected BlockingQueue emptyCollection() { + return new LinkedBlockingDeque(20); + } + } + public static void main(String[] args) { junit.textui.TestRunner.run(suite()); } public static Test suite() { - return new TestSuite(LinkedBlockingDequeTest.class); + return newTestSuite(LinkedBlockingDequeTest.class, + new Unbounded().testSuite(), + new Bounded().testSuite()); } /** * Create a deque of given size containing consecutive * Integers 0 ... n. */ - private LinkedBlockingDeque populatedDeque(int n) { - LinkedBlockingDeque q = new LinkedBlockingDeque(n); + private LinkedBlockingDeque populatedDeque(int n) { + LinkedBlockingDeque q = + new LinkedBlockingDeque(n); assertTrue(q.isEmpty()); for (int i = 0; i < n; i++) assertTrue(q.offer(new Integer(i))); @@ -93,7 +109,7 @@ public class LinkedBlockingDequeTest ext } /** - * pollFirst succeeds unless empty + * pollFirst succeeds unless empty */ public void testPollFirst() { LinkedBlockingDeque q = populatedDeque(SIZE); @@ -104,7 +120,7 @@ public class LinkedBlockingDequeTest ext } /** - * pollLast succeeds unless empty + * pollLast succeeds unless empty */ public void testPollLast() { LinkedBlockingDeque q = populatedDeque(SIZE); @@ -115,7 +131,7 @@ public class LinkedBlockingDequeTest ext } /** - * peekFirst returns next element, or null if empty + * peekFirst returns next element, or null if empty */ public void testPeekFirst() { LinkedBlockingDeque q = populatedDeque(SIZE); @@ -129,7 +145,7 @@ public class LinkedBlockingDequeTest ext } /** - * peek returns next element, or null if empty + * peek returns next element, or null if empty */ public void testPeek() { LinkedBlockingDeque q = populatedDeque(SIZE); @@ -143,7 +159,7 @@ public class LinkedBlockingDequeTest ext } /** - * peekLast returns next element, or null if empty + * peekLast returns next element, or null if empty */ public void testPeekLast() { LinkedBlockingDeque q = populatedDeque(SIZE); @@ -173,7 +189,7 @@ public class LinkedBlockingDequeTest ext } /** - * getLast() returns last element, or throws NSEE if empty + * getLast() returns last element, or throws NSEE if empty */ public void testLastElement() { LinkedBlockingDeque q = populatedDeque(SIZE); @@ -219,7 +235,7 @@ public class LinkedBlockingDequeTest ext } /** - * remove removes next element, or throws NSEE if empty + * remove removes next element, or throws NSEE if empty */ public void testRemove() { LinkedBlockingDeque q = populatedDeque(SIZE); @@ -442,7 +458,7 @@ public class LinkedBlockingDequeTest ext /** - * pop removes next element, or throws NSEE if empty + * pop removes next element, or throws NSEE if empty */ public void testPop() { LinkedBlockingDeque q = populatedDeque(SIZE); @@ -667,22 +683,6 @@ public class LinkedBlockingDequeTest ext } /** - * take blocks interruptibly when empty - */ - public void testTakeFromEmpty() throws InterruptedException { - final LinkedBlockingDeque q = new LinkedBlockingDeque(2); - Thread t = new ThreadShouldThrow(InterruptedException.class) { - public void realRun() throws InterruptedException { - q.take(); - }}; - - t.start(); - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); - } - - /** * Take removes existing elements until empty, then blocks interruptibly */ public void testBlockingTake() throws InterruptedException { @@ -743,48 +743,32 @@ public class LinkedBlockingDequeTest ext * returning timeout status */ public void testInterruptedTimedPoll() throws InterruptedException { - Thread t = new Thread(new CheckedRunnable() { + final BlockingQueue q = populatedDeque(SIZE); + final CountDownLatch aboutToWait = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { - LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); + long t0 = System.nanoTime(); + assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); + assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); } + long t0 = System.nanoTime(); + aboutToWait.countDown(); try { - q.poll(SMALL_DELAY_MS, MILLISECONDS); - shouldThrow(); - } catch (InterruptedException success) {} - }}); - - t.start(); - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); - } - - /** - * timed poll before a delayed offer fails; after offer succeeds; - * on interruption throws - */ - public void testTimedPollWithOffer() throws InterruptedException { - final LinkedBlockingDeque q = new LinkedBlockingDeque(2); - Thread t = new Thread(new CheckedRunnable() { - public void realRun() throws InterruptedException { - try { - assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); - assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); - q.poll(LONG_DELAY_MS, MILLISECONDS); + q.poll(MEDIUM_DELAY_MS, MILLISECONDS); shouldThrow(); - } catch (InterruptedException success) {} + } catch (InterruptedException success) { + assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); + } }}); - t.start(); - Thread.sleep(SMALL_DELAY_MS); - assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); + aboutToWait.await(); + waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); t.interrupt(); - t.join(); + awaitTermination(t, MEDIUM_DELAY_MS); + checkEmpty(q); } - /** * putFirst(null) throws NPE */ @@ -976,8 +960,8 @@ public class LinkedBlockingDequeTest ext } /** - * timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds; - * on interruption throws + * timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds; + * on interruption throws */ public void testTimedPollFirstWithOfferFirst() throws InterruptedException { final LinkedBlockingDeque q = new LinkedBlockingDeque(2); @@ -1188,8 +1172,8 @@ public class LinkedBlockingDequeTest ext } /** - * timed poll before a delayed offerLast fails; after offerLast succeeds; - * on interruption throws + * timed poll before a delayed offerLast fails; after offerLast succeeds; + * on interruption throws */ public void testTimedPollWithOfferLast() throws InterruptedException { final LinkedBlockingDeque q = new LinkedBlockingDeque(2); @@ -1232,11 +1216,17 @@ public class LinkedBlockingDequeTest ext public void testRemoveElement() { LinkedBlockingDeque q = populatedDeque(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()); } @@ -1319,44 +1309,45 @@ public class LinkedBlockingDequeTest ext } /** - * toArray contains all elements + * toArray contains all elements in FIFO order */ public void testToArray() throws InterruptedException{ LinkedBlockingDeque q = populatedDeque(SIZE); Object[] o = q.toArray(); for (int i = 0; i < o.length; i++) - assertEquals(o[i], q.take()); + assertSame(o[i], q.poll()); } /** - * toArray(a) contains all elements + * toArray(a) contains all elements in FIFO order */ - public void testToArray2() throws InterruptedException { - LinkedBlockingDeque q = populatedDeque(SIZE); + public void testToArray2() { + LinkedBlockingDeque q = populatedDeque(SIZE); Integer[] ints = new Integer[SIZE]; - ints = (Integer[])q.toArray(ints); + Integer[] array = q.toArray(ints); + assertSame(ints, array); for (int i = 0; i < ints.length; i++) - assertEquals(ints[i], q.take()); + assertSame(ints[i], q.remove()); } /** - * toArray(null) throws NPE + * toArray(null) throws NullPointerException */ - public void testToArray_BadArg() { + public void testToArray_NullArg() { LinkedBlockingDeque q = populatedDeque(SIZE); try { - Object o[] = q.toArray(null); + q.toArray(null); shouldThrow(); } catch (NullPointerException success) {} } /** - * toArray with incompatible array type throws CCE + * toArray(incompatible array type) throws ArrayStoreException */ public void testToArray1_BadArg() { LinkedBlockingDeque q = populatedDeque(SIZE); try { - Object o[] = q.toArray(new String[10]); + q.toArray(new String[10]); shouldThrow(); } catch (ArrayStoreException success) {} } @@ -1426,7 +1417,7 @@ public class LinkedBlockingDequeTest ext /** - * Descending iterator iterates through all elements + * Descending iterator iterates through all elements */ public void testDescendingIterator() { LinkedBlockingDeque q = populatedDeque(SIZE); @@ -1445,7 +1436,7 @@ public class LinkedBlockingDequeTest ext } /** - * Descending iterator ordering is reverse FIFO + * Descending iterator ordering is reverse FIFO */ public void testDescendingIteratorOrdering() { final LinkedBlockingDeque q = new LinkedBlockingDeque(); @@ -1654,7 +1645,7 @@ public class LinkedBlockingDequeTest ext } /** - * drainTo(c, n) empties first max {n, size} elements of deque into c + * drainTo(c, n) empties first min(n, size) elements of queue into c */ public void testDrainToN() { LinkedBlockingDeque q = new LinkedBlockingDeque(); @@ -1663,7 +1654,7 @@ public class LinkedBlockingDequeTest 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)