--- jsr166/src/test/tck/LinkedBlockingDequeTest.java 2009/11/21 21:12:55 1.15 +++ jsr166/src/test/tck/LinkedBlockingDequeTest.java 2011/05/27 20:07:24 1.39 @@ -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()); + 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,77 +109,77 @@ public class LinkedBlockingDequeTest ext } /** - * pollFirst succeeds unless empty + * pollFirst succeeds unless empty */ public void testPollFirst() { LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.pollFirst()).intValue()); + assertEquals(i, q.pollFirst()); } assertNull(q.pollFirst()); } /** - * pollLast succeeds unless empty + * pollLast succeeds unless empty */ public void testPollLast() { LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = SIZE-1; i >= 0; --i) { - assertEquals(i, ((Integer)q.pollLast()).intValue()); + assertEquals(i, q.pollLast()); } assertNull(q.pollLast()); } /** - * peekFirst returns next element, or null if empty + * peekFirst returns next element, or null if empty */ public void testPeekFirst() { LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.peekFirst()).intValue()); - q.pollFirst(); + assertEquals(i, q.peekFirst()); + assertEquals(i, q.pollFirst()); assertTrue(q.peekFirst() == null || - i != ((Integer)q.peekFirst()).intValue()); + !q.peekFirst().equals(i)); } assertNull(q.peekFirst()); } /** - * peek returns next element, or null if empty + * peek returns next element, or null if empty */ public void testPeek() { LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.peek()).intValue()); - q.pollFirst(); + assertEquals(i, q.peek()); + assertEquals(i, q.pollFirst()); assertTrue(q.peek() == null || - i != ((Integer)q.peek()).intValue()); + !q.peek().equals(i)); } assertNull(q.peek()); } /** - * peekLast returns next element, or null if empty + * peekLast returns next element, or null if empty */ public void testPeekLast() { LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = SIZE-1; i >= 0; --i) { - assertEquals(i, ((Integer)q.peekLast()).intValue()); - q.pollLast(); + assertEquals(i, q.peekLast()); + assertEquals(i, q.pollLast()); assertTrue(q.peekLast() == null || - i != ((Integer)q.peekLast()).intValue()); + !q.peekLast().equals(i)); } assertNull(q.peekLast()); } /** - * getFirst returns next getFirst, or throws NSEE if empty + * getFirst() returns first element, or throws NSEE if empty */ public void testFirstElement() { LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.getFirst()).intValue()); - q.pollFirst(); + assertEquals(i, q.getFirst()); + assertEquals(i, q.pollFirst()); } try { q.getFirst(); @@ -173,13 +189,13 @@ public class LinkedBlockingDequeTest ext } /** - * getLast returns next element, or throws NSEE if empty + * getLast() returns last element, or throws NSEE if empty */ public void testLastElement() { LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = SIZE-1; i >= 0; --i) { - assertEquals(i, ((Integer)q.getLast()).intValue()); - q.pollLast(); + assertEquals(i, q.getLast()); + assertEquals(i, q.pollLast()); } try { q.getLast(); @@ -189,12 +205,12 @@ public class LinkedBlockingDequeTest ext } /** - * removeFirst removes next element, or throws NSEE if empty + * removeFirst() removes first element, or throws NSEE if empty */ public void testRemoveFirst() { LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.removeFirst()).intValue()); + assertEquals(i, q.removeFirst()); } try { q.removeFirst(); @@ -204,12 +220,12 @@ public class LinkedBlockingDequeTest ext } /** - * removeLast removes last element, or throws NSEE if empty + * removeLast() removes last element, or throws NSEE if empty */ public void testRemoveLast() { LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = SIZE - 1; i >= 0; --i) { - assertEquals(i, ((Integer)q.removeLast()).intValue()); + assertEquals(i, q.removeLast()); } try { q.removeLast(); @@ -219,12 +235,12 @@ 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); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.remove()).intValue()); + assertEquals(i, q.remove()); } try { q.remove(); @@ -269,7 +285,7 @@ public class LinkedBlockingDequeTest ext LinkedBlockingDeque q = populatedDeque(3); q.pollLast(); q.addFirst(four); - assertEquals(four,q.peekFirst()); + assertSame(four, q.peekFirst()); } /** @@ -279,10 +295,9 @@ public class LinkedBlockingDequeTest ext LinkedBlockingDeque q = populatedDeque(3); q.pollLast(); q.addLast(four); - assertEquals(four,q.peekLast()); + assertSame(four, q.peekLast()); } - /** * A new deque has the indicated capacity, or Integer.MAX_VALUE if * none given @@ -437,17 +452,16 @@ public class LinkedBlockingDequeTest ext LinkedBlockingDeque q = populatedDeque(3); q.pollLast(); q.push(four); - assertEquals(four,q.peekFirst()); + assertSame(four, q.peekFirst()); } - /** - * 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); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.pop()).intValue()); + assertEquals(i, q.pop()); } try { q.pop(); @@ -455,7 +469,6 @@ public class LinkedBlockingDequeTest ext } catch (NoSuchElementException success) {} } - /** * Offer succeeds if not full; fails if full */ @@ -513,6 +526,7 @@ public class LinkedBlockingDequeTest ext shouldThrow(); } catch (NullPointerException success) {} } + /** * addAll of a collection with any null elements throws NPE after * possibly adding some elements @@ -527,6 +541,7 @@ public class LinkedBlockingDequeTest ext shouldThrow(); } catch (NullPointerException success) {} } + /** * addAll throws ISE if not enough room */ @@ -540,6 +555,7 @@ public class LinkedBlockingDequeTest ext shouldThrow(); } catch (IllegalStateException success) {} } + /** * Deque contains all elements, in traversal order, of successful addAll */ @@ -555,7 +571,6 @@ public class LinkedBlockingDequeTest ext assertEquals(ints[i], q.poll()); } - /** * put(null) throws NPE */ @@ -584,56 +599,70 @@ public class LinkedBlockingDequeTest ext * put blocks interruptibly if full */ public void testBlockingPut() throws InterruptedException { - Thread t = new Thread(new CheckedRunnable() { - public void realRun() { - int added = 0; + final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); + final CountDownLatch pleaseInterrupt = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + for (int i = 0; i < SIZE; ++i) + q.put(i); + assertEquals(SIZE, q.size()); + assertEquals(0, q.remainingCapacity()); + + Thread.currentThread().interrupt(); try { - LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); - for (int i = 0; i < SIZE; ++i) { - q.put(new Integer(i)); - ++added; - } - q.put(new Integer(SIZE)); - threadShouldThrow(); - } catch (InterruptedException success) { - threadAssertEquals(added, SIZE); - } + q.put(99); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + + pleaseInterrupt.countDown(); + try { + q.put(99); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); }}); - t.start(); - Thread.sleep(SHORT_DELAY_MS); + await(pleaseInterrupt); + assertThreadStaysAlive(t); t.interrupt(); - t.join(); + awaitTermination(t); + assertEquals(SIZE, q.size()); + assertEquals(0, q.remainingCapacity()); } /** - * put blocks waiting for take when full + * put blocks interruptibly waiting for take when full */ public void testPutWithTake() throws InterruptedException { - final LinkedBlockingDeque q = new LinkedBlockingDeque(2); - Thread t = new Thread(new CheckedRunnable() { - public void realRun() { - int added = 0; + final int capacity = 2; + final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity); + final CountDownLatch pleaseTake = new CountDownLatch(1); + final CountDownLatch pleaseInterrupt = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + for (int i = 0; i < capacity; i++) + q.put(i); + pleaseTake.countDown(); + q.put(86); + + pleaseInterrupt.countDown(); try { - q.put(new Object()); - ++added; - q.put(new Object()); - ++added; - q.put(new Object()); - ++added; - q.put(new Object()); - ++added; - threadShouldThrow(); - } catch (InterruptedException success) { - threadAssertTrue(added >= 2); - } + q.put(99); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); }}); - t.start(); - Thread.sleep(SHORT_DELAY_MS); - q.take(); + await(pleaseTake); + assertEquals(q.remainingCapacity(), 0); + assertEquals(0, q.take()); + + await(pleaseInterrupt); + assertThreadStaysAlive(t); t.interrupt(); - t.join(); + awaitTermination(t); + assertEquals(q.remainingCapacity(), 0); } /** @@ -641,18 +670,25 @@ public class LinkedBlockingDequeTest ext */ public void testTimedOffer() throws InterruptedException { final LinkedBlockingDeque q = new LinkedBlockingDeque(2); - Thread t = new ThreadShouldThrow(InterruptedException.class) { + final CountDownLatch pleaseInterrupt = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { q.put(new Object()); q.put(new Object()); - threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); - q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); - }}; + long startTime = System.nanoTime(); + assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); + pleaseInterrupt.countDown(); + try { + q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + }}); - t.start(); - Thread.sleep(SMALL_DELAY_MS); + await(pleaseInterrupt); + assertThreadStaysAlive(t); t.interrupt(); - t.join(); + awaitTermination(t); } /** @@ -661,53 +697,50 @@ public class LinkedBlockingDequeTest ext public void testTake() throws InterruptedException { LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.take()).intValue()); + assertEquals(i, q.take()); } } /** - * 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 + * take removes existing elements until empty, then blocks interruptibly */ public void testBlockingTake() throws InterruptedException { - Thread t = new ThreadShouldThrow(InterruptedException.class) { + final LinkedBlockingDeque q = populatedDeque(SIZE); + final CountDownLatch pleaseInterrupt = 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, ((Integer)q.take()).intValue()); + assertEquals(i, q.take()); } - q.take(); - }}; - t.start(); - Thread.sleep(SHORT_DELAY_MS); + Thread.currentThread().interrupt(); + try { + q.take(); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + + pleaseInterrupt.countDown(); + try { + q.take(); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + }}); + + await(pleaseInterrupt); + assertThreadStaysAlive(t); t.interrupt(); - t.join(); + awaitTermination(t); } - /** * poll succeeds unless empty */ public void testPoll() { LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.poll()).intValue()); + assertEquals(i, q.poll()); } assertNull(q.poll()); } @@ -718,7 +751,7 @@ public class LinkedBlockingDequeTest ext public void testTimedPoll0() throws InterruptedException { LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue()); + assertEquals(i, q.poll(0, MILLISECONDS)); } assertNull(q.poll(0, MILLISECONDS)); } @@ -729,9 +762,14 @@ public class LinkedBlockingDequeTest ext public void testTimedPoll() throws InterruptedException { LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); - } - assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); + long startTime = System.nanoTime(); + assertEquals(i, q.poll(LONG_DELAY_MS, MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); + } + long startTime = System.nanoTime(); + assertNull(q.poll(timeoutMillis(), MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); + checkEmpty(q); } /** @@ -739,126 +777,124 @@ 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, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); + 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 { - assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); - assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); - try { - 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 */ - public void testPutFirstNull() throws InterruptedException { + public void testPutFirstNull() throws InterruptedException { try { LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); q.putFirst(null); shouldThrow(); } catch (NullPointerException success) {} - } + } /** * all elements successfully putFirst are contained */ - public void testPutFirst() throws InterruptedException { - LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); - for (int i = 0; i < SIZE; ++i) { - Integer I = new Integer(i); - q.putFirst(I); - assertTrue(q.contains(I)); - } - assertEquals(0, q.remainingCapacity()); + public void testPutFirst() throws InterruptedException { + LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); + for (int i = 0; i < SIZE; ++i) { + Integer I = new Integer(i); + q.putFirst(I); + assertTrue(q.contains(I)); + } + assertEquals(0, q.remainingCapacity()); } /** * putFirst blocks interruptibly if full */ public void testBlockingPutFirst() throws InterruptedException { - Thread t = new Thread(new CheckedRunnable() { - public void realRun() { - int added = 0; + final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); + final CountDownLatch pleaseInterrupt = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + for (int i = 0; i < SIZE; ++i) + q.putFirst(i); + assertEquals(SIZE, q.size()); + assertEquals(0, q.remainingCapacity()); + + Thread.currentThread().interrupt(); try { - LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); - for (int i = 0; i < SIZE; ++i) { - q.putFirst(new Integer(i)); - ++added; - } - q.putFirst(new Integer(SIZE)); - threadShouldThrow(); - } catch (InterruptedException success) { - threadAssertEquals(added, SIZE); - } + q.putFirst(99); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + + pleaseInterrupt.countDown(); + try { + q.putFirst(99); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); }}); - t.start(); - Thread.sleep(SHORT_DELAY_MS); + await(pleaseInterrupt); + assertThreadStaysAlive(t); t.interrupt(); - t.join(); + awaitTermination(t); + assertEquals(SIZE, q.size()); + assertEquals(0, q.remainingCapacity()); } /** - * putFirst blocks waiting for take when full + * putFirst blocks interruptibly waiting for take when full */ public void testPutFirstWithTake() throws InterruptedException { - final LinkedBlockingDeque q = new LinkedBlockingDeque(2); - Thread t = new Thread(new CheckedRunnable() { - public void realRun() { - int added = 0; + final int capacity = 2; + final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity); + final CountDownLatch pleaseTake = new CountDownLatch(1); + final CountDownLatch pleaseInterrupt = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + for (int i = 0; i < capacity; i++) + q.putFirst(i); + pleaseTake.countDown(); + q.putFirst(86); + + pleaseInterrupt.countDown(); try { - q.putFirst(new Object()); - ++added; - q.putFirst(new Object()); - ++added; - q.putFirst(new Object()); - ++added; - q.putFirst(new Object()); - ++added; - threadShouldThrow(); - } catch (InterruptedException success) { - threadAssertTrue(added >= 2); - } + q.putFirst(99); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); }}); - t.start(); - Thread.sleep(SHORT_DELAY_MS); - q.take(); + await(pleaseTake); + assertEquals(q.remainingCapacity(), 0); + assertEquals(capacity - 1, q.take()); + + await(pleaseInterrupt); + assertThreadStaysAlive(t); t.interrupt(); - t.join(); + awaitTermination(t); + assertEquals(q.remainingCapacity(), 0); } /** @@ -866,18 +902,25 @@ public class LinkedBlockingDequeTest ext */ public void testTimedOfferFirst() throws InterruptedException { final LinkedBlockingDeque q = new LinkedBlockingDeque(2); - Thread t = new ThreadShouldThrow(InterruptedException.class) { + final CountDownLatch pleaseInterrupt = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { q.putFirst(new Object()); q.putFirst(new Object()); - threadAssertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, MILLISECONDS)); - q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS); - }}; + long startTime = System.nanoTime(); + assertFalse(q.offerFirst(new Object(), timeoutMillis(), MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); + pleaseInterrupt.countDown(); + try { + q.offerFirst(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + }}); - t.start(); - Thread.sleep(SMALL_DELAY_MS); + await(pleaseInterrupt); + assertThreadStaysAlive(t); t.interrupt(); - t.join(); + awaitTermination(t); } /** @@ -886,53 +929,132 @@ public class LinkedBlockingDequeTest ext public void testTakeFirst() throws InterruptedException { LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.takeFirst()).intValue()); + assertEquals(i, q.takeFirst()); } } /** - * takeFirst blocks interruptibly when empty + * takeFirst() blocks interruptibly when empty */ - public void testTakeFirstFromEmpty() throws InterruptedException { - final LinkedBlockingDeque q = new LinkedBlockingDeque(2); - Thread t = new ThreadShouldThrow(InterruptedException.class) { - public void realRun() throws InterruptedException { - q.takeFirst(); - }}; + public void testTakeFirstFromEmptyBlocksInterruptibly() { + final BlockingDeque q = new LinkedBlockingDeque(); + final CountDownLatch threadStarted = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() { + threadStarted.countDown(); + try { + q.takeFirst(); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + }}); - t.start(); - Thread.sleep(SHORT_DELAY_MS); + await(threadStarted); + assertThreadStaysAlive(t); t.interrupt(); - t.join(); + awaitTermination(t); + } + + /** + * takeFirst() throws InterruptedException immediately if interrupted + * before waiting + */ + public void testTakeFirstFromEmptyAfterInterrupt() { + final BlockingDeque q = new LinkedBlockingDeque(); + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() { + Thread.currentThread().interrupt(); + try { + q.takeFirst(); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + }}); + + awaitTermination(t); + } + + /** + * takeLast() blocks interruptibly when empty + */ + public void testTakeLastFromEmptyBlocksInterruptibly() { + final BlockingDeque q = new LinkedBlockingDeque(); + final CountDownLatch threadStarted = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() { + threadStarted.countDown(); + try { + q.takeLast(); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + }}); + + await(threadStarted); + assertThreadStaysAlive(t); + t.interrupt(); + awaitTermination(t); } /** - * TakeFirst removes existing elements until empty, then blocks interruptibly + * takeLast() throws InterruptedException immediately if interrupted + * before waiting + */ + public void testTakeLastFromEmptyAfterInterrupt() { + final BlockingDeque q = new LinkedBlockingDeque(); + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() { + Thread.currentThread().interrupt(); + try { + q.takeLast(); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + }}); + + awaitTermination(t); + } + + /** + * takeFirst removes existing elements until empty, then blocks interruptibly */ public void testBlockingTakeFirst() throws InterruptedException { - Thread t = new ThreadShouldThrow(InterruptedException.class) { + final LinkedBlockingDeque q = populatedDeque(SIZE); + final CountDownLatch pleaseInterrupt = 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, ((Integer)q.takeFirst()).intValue()); + assertEquals(i, q.takeFirst()); } - q.takeFirst(); - }}; - t.start(); - Thread.sleep(SHORT_DELAY_MS); + Thread.currentThread().interrupt(); + try { + q.takeFirst(); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + + pleaseInterrupt.countDown(); + try { + q.takeFirst(); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + }}); + + await(pleaseInterrupt); + assertThreadStaysAlive(t); t.interrupt(); - t.join(); + awaitTermination(t); } - /** * timed pollFirst with zero timeout succeeds when non-empty, else times out */ public void testTimedPollFirst0() throws InterruptedException { LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.pollFirst(0, MILLISECONDS)).intValue()); + assertEquals(i, q.pollFirst(0, MILLISECONDS)); } assertNull(q.pollFirst(0, MILLISECONDS)); } @@ -943,9 +1065,14 @@ public class LinkedBlockingDequeTest ext public void testTimedPollFirst() throws InterruptedException { LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue()); - } - assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); + long startTime = System.nanoTime(); + assertEquals(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); + } + long startTime = System.nanoTime(); + assertNull(q.pollFirst(timeoutMillis(), MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); + checkEmpty(q); } /** @@ -953,125 +1080,168 @@ public class LinkedBlockingDequeTest ext * returning timeout status */ public void testInterruptedTimedPollFirst() throws InterruptedException { - Thread t = new Thread(new CheckedRunnable() { + final CountDownLatch pleaseInterrupt = 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, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue()); + assertEquals(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS)); } + + Thread.currentThread().interrupt(); + try { + q.pollFirst(SMALL_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + + pleaseInterrupt.countDown(); try { q.pollFirst(SMALL_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); }}); - t.start(); - Thread.sleep(SHORT_DELAY_MS); + await(pleaseInterrupt); + assertThreadStaysAlive(t); t.interrupt(); - t.join(); + awaitTermination(t); } /** - * 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); - Thread t = new Thread(new CheckedRunnable() { + final CheckedBarrier barrier = new CheckedBarrier(2); + Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { - assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); + long startTime = System.nanoTime(); + assertNull(q.pollFirst(timeoutMillis(), MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); + + barrier.await(); + assertSame(zero, q.pollFirst(LONG_DELAY_MS, MILLISECONDS)); + + Thread.currentThread().interrupt(); try { q.pollFirst(LONG_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (InterruptedException success) {} + + barrier.await(); + try { + q.pollFirst(LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); }}); - t.start(); - Thread.sleep(SMALL_DELAY_MS); - assertTrue(q.offerFirst(zero, SHORT_DELAY_MS, MILLISECONDS)); + barrier.await(); + long startTime = System.nanoTime(); + assertTrue(q.offerFirst(zero, LONG_DELAY_MS, MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); + barrier.await(); + assertThreadStaysAlive(t); t.interrupt(); - t.join(); + awaitTermination(t); } /** * putLast(null) throws NPE */ - public void testPutLastNull() throws InterruptedException { + public void testPutLastNull() throws InterruptedException { try { LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); q.putLast(null); shouldThrow(); } catch (NullPointerException success) {} - } + } /** * all elements successfully putLast are contained */ - public void testPutLast() throws InterruptedException { - LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); - for (int i = 0; i < SIZE; ++i) { - Integer I = new Integer(i); - q.putLast(I); - assertTrue(q.contains(I)); - } - assertEquals(0, q.remainingCapacity()); + public void testPutLast() throws InterruptedException { + LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); + for (int i = 0; i < SIZE; ++i) { + Integer I = new Integer(i); + q.putLast(I); + assertTrue(q.contains(I)); + } + assertEquals(0, q.remainingCapacity()); } /** * putLast blocks interruptibly if full */ public void testBlockingPutLast() throws InterruptedException { - Thread t = new Thread(new CheckedRunnable() { - public void realRun() { - int added = 0; + final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); + final CountDownLatch pleaseInterrupt = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + for (int i = 0; i < SIZE; ++i) + q.putLast(i); + assertEquals(SIZE, q.size()); + assertEquals(0, q.remainingCapacity()); + + Thread.currentThread().interrupt(); try { - LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); - for (int i = 0; i < SIZE; ++i) { - q.putLast(new Integer(i)); - ++added; - } - q.putLast(new Integer(SIZE)); - threadShouldThrow(); - } catch (InterruptedException success) { - threadAssertEquals(added, SIZE); - } + q.putLast(99); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + + pleaseInterrupt.countDown(); + try { + q.putLast(99); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); }}); - t.start(); - Thread.sleep(SHORT_DELAY_MS); + await(pleaseInterrupt); + assertThreadStaysAlive(t); t.interrupt(); - t.join(); + awaitTermination(t); + assertEquals(SIZE, q.size()); + assertEquals(0, q.remainingCapacity()); } /** - * putLast blocks waiting for take when full + * putLast blocks interruptibly waiting for take when full */ public void testPutLastWithTake() throws InterruptedException { - final LinkedBlockingDeque q = new LinkedBlockingDeque(2); - Thread t = new Thread(new CheckedRunnable() { - public void realRun() { - int added = 0; + final int capacity = 2; + final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity); + final CountDownLatch pleaseTake = new CountDownLatch(1); + final CountDownLatch pleaseInterrupt = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + for (int i = 0; i < capacity; i++) + q.putLast(i); + pleaseTake.countDown(); + q.putLast(86); + + pleaseInterrupt.countDown(); try { - q.putLast(new Object()); - ++added; - q.putLast(new Object()); - ++added; - q.putLast(new Object()); - ++added; - q.putLast(new Object()); - ++added; - threadShouldThrow(); - } catch (InterruptedException success) { - threadAssertTrue(added >= 2); - } + q.putLast(99); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); }}); - t.start(); - Thread.sleep(SHORT_DELAY_MS); - q.take(); + await(pleaseTake); + assertEquals(q.remainingCapacity(), 0); + assertEquals(0, q.take()); + + await(pleaseInterrupt); + assertThreadStaysAlive(t); t.interrupt(); - t.join(); + awaitTermination(t); + assertEquals(q.remainingCapacity(), 0); } /** @@ -1079,18 +1249,25 @@ public class LinkedBlockingDequeTest ext */ public void testTimedOfferLast() throws InterruptedException { final LinkedBlockingDeque q = new LinkedBlockingDeque(2); - Thread t = new ThreadShouldThrow(InterruptedException.class) { + final CountDownLatch pleaseInterrupt = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { q.putLast(new Object()); q.putLast(new Object()); - threadAssertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, MILLISECONDS)); - q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS); - }}; + long startTime = System.nanoTime(); + assertFalse(q.offerLast(new Object(), timeoutMillis(), MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); + pleaseInterrupt.countDown(); + try { + q.offerLast(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + }}); - t.start(); - Thread.sleep(SMALL_DELAY_MS); + await(pleaseInterrupt); + assertThreadStaysAlive(t); t.interrupt(); - t.join(); + awaitTermination(t); } /** @@ -1099,53 +1276,50 @@ public class LinkedBlockingDequeTest ext public void testTakeLast() throws InterruptedException { LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue()); + assertEquals(SIZE-i-1, q.takeLast()); } } /** - * takeLast blocks interruptibly when empty - */ - public void testTakeLastFromEmpty() throws InterruptedException { - final LinkedBlockingDeque q = new LinkedBlockingDeque(2); - Thread t = new ThreadShouldThrow(InterruptedException.class) { - public void realRun() throws InterruptedException { - q.takeLast(); - }}; - - t.start(); - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); - } - - /** - * TakeLast removes existing elements until empty, then blocks interruptibly + * takeLast removes existing elements until empty, then blocks interruptibly */ public void testBlockingTakeLast() throws InterruptedException { - Thread t = new ThreadShouldThrow(InterruptedException.class) { + final LinkedBlockingDeque q = populatedDeque(SIZE); + final CountDownLatch pleaseInterrupt = 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(SIZE-i-1, ((Integer)q.takeLast()).intValue()); + assertEquals(SIZE-i-1, q.takeLast()); } - q.takeLast(); - }}; - t.start(); - Thread.sleep(SHORT_DELAY_MS); + Thread.currentThread().interrupt(); + try { + q.takeLast(); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + + pleaseInterrupt.countDown(); + try { + q.takeLast(); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + }}); + + await(pleaseInterrupt); + assertThreadStaysAlive(t); t.interrupt(); - t.join(); + awaitTermination(t); } - /** * timed pollLast with zero timeout succeeds when non-empty, else times out */ public void testTimedPollLast0() throws InterruptedException { LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(SIZE-i-1, ((Integer)q.pollLast(0, MILLISECONDS)).intValue()); + assertEquals(SIZE-i-1, q.pollLast(0, MILLISECONDS)); } assertNull(q.pollLast(0, MILLISECONDS)); } @@ -1156,9 +1330,14 @@ public class LinkedBlockingDequeTest ext public void testTimedPollLast() throws InterruptedException { LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue()); - } - assertNull(q.pollLast(SHORT_DELAY_MS, MILLISECONDS)); + long startTime = System.nanoTime(); + assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); + } + long startTime = System.nanoTime(); + assertNull(q.pollLast(timeoutMillis(), MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); + checkEmpty(q); } /** @@ -1166,55 +1345,85 @@ public class LinkedBlockingDequeTest ext * returning timeout status */ public void testInterruptedTimedPollLast() throws InterruptedException { - Thread t = new Thread(new CheckedRunnable() { + final CountDownLatch pleaseInterrupt = 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(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue()); + assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS)); } + + Thread.currentThread().interrupt(); try { - q.pollLast(SMALL_DELAY_MS, MILLISECONDS); + q.pollLast(LONG_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + + pleaseInterrupt.countDown(); + try { + q.pollLast(LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); }}); - t.start(); - Thread.sleep(SHORT_DELAY_MS); + await(pleaseInterrupt); + assertThreadStaysAlive(t); t.interrupt(); - t.join(); + awaitTermination(t); } /** - * 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); - Thread t = new Thread(new CheckedRunnable() { + final CheckedBarrier barrier = new CheckedBarrier(2); + Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { - assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); + long startTime = System.nanoTime(); + assertNull(q.poll(timeoutMillis(), MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); + + barrier.await(); + assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); + + Thread.currentThread().interrupt(); try { q.poll(LONG_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + + barrier.await(); + try { + q.poll(LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); }}); - t.start(); - Thread.sleep(SMALL_DELAY_MS); - assertTrue(q.offerLast(zero, SHORT_DELAY_MS, MILLISECONDS)); + barrier.await(); + long startTime = System.nanoTime(); + assertTrue(q.offerLast(zero, LONG_DELAY_MS, MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); + + barrier.await(); + assertThreadStaysAlive(t); t.interrupt(); - t.join(); + awaitTermination(t); } - /** * element returns next element, or throws NSEE if empty */ public void testElement() { LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.element()).intValue()); + assertEquals(i, q.element()); q.poll(); } try { @@ -1229,11 +1438,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()); } @@ -1316,49 +1531,49 @@ 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 { - LinkedBlockingDeque q = populatedDeque(SIZE); - 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 { - LinkedBlockingDeque q = populatedDeque(SIZE); - Object o[] = q.toArray(new String[10] ); + q.toArray(new String[10]); shouldThrow(); } catch (ArrayStoreException success) {} } - /** * iterator iterates through all elements */ @@ -1373,7 +1588,7 @@ public class LinkedBlockingDequeTest ext /** * iterator.remove removes current element */ - public void testIteratorRemove () { + public void testIteratorRemove() { final LinkedBlockingDeque q = new LinkedBlockingDeque(3); q.add(two); q.add(one); @@ -1384,12 +1599,11 @@ public class LinkedBlockingDequeTest ext it.remove(); it = q.iterator(); - assertEquals(it.next(), one); - assertEquals(it.next(), three); + assertSame(it.next(), one); + assertSame(it.next(), three); assertFalse(it.hasNext()); } - /** * iterator ordering is FIFO */ @@ -1401,8 +1615,7 @@ public class LinkedBlockingDequeTest ext assertEquals(0, q.remainingCapacity()); int k = 0; for (Iterator it = q.iterator(); it.hasNext();) { - int i = ((Integer)(it.next())).intValue(); - assertEquals(++k, i); + assertEquals(++k, it.next()); } assertEquals(3, k); } @@ -1410,7 +1623,7 @@ public class LinkedBlockingDequeTest ext /** * Modifications do not cause iterators to fail */ - public void testWeaklyConsistentIteration () { + public void testWeaklyConsistentIteration() { final LinkedBlockingDeque q = new LinkedBlockingDeque(3); q.add(one); q.add(two); @@ -1422,9 +1635,8 @@ public class LinkedBlockingDequeTest ext assertEquals(0, q.size()); } - /** - * Descending iterator iterates through all elements + * Descending iterator iterates through all elements */ public void testDescendingIterator() { LinkedBlockingDeque q = populatedDeque(SIZE); @@ -1443,7 +1655,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(); @@ -1453,8 +1665,7 @@ public class LinkedBlockingDequeTest ext q.add(new Integer(1)); int k = 0; for (Iterator it = q.descendingIterator(); it.hasNext();) { - int i = ((Integer)(it.next())).intValue(); - assertEquals(++k, i); + assertEquals(++k, it.next()); } assertEquals(3, k); @@ -1467,7 +1678,7 @@ public class LinkedBlockingDequeTest ext /** * descendingIterator.remove removes current element */ - public void testDescendingIteratorRemove () { + public void testDescendingIteratorRemove() { final LinkedBlockingDeque q = new LinkedBlockingDeque(); for (int iters = 0; iters < 100; ++iters) { q.add(new Integer(3)); @@ -1486,7 +1697,6 @@ public class LinkedBlockingDequeTest ext } } - /** * toString contains toStrings of elements */ @@ -1494,11 +1704,10 @@ public class LinkedBlockingDequeTest ext LinkedBlockingDeque q = populatedDeque(SIZE); String s = q.toString(); for (int i = 0; i < SIZE; ++i) { - assertTrue(s.indexOf(String.valueOf(i)) >= 0); + assertTrue(s.contains(String.valueOf(i))); } } - /** * offer transfers elements across Executor tasks */ @@ -1507,38 +1716,42 @@ public class LinkedBlockingDequeTest ext q.add(one); q.add(two); ExecutorService executor = Executors.newFixedThreadPool(2); + final CheckedBarrier threadsStarted = new CheckedBarrier(2); executor.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { - threadAssertFalse(q.offer(three)); - threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); - threadAssertEquals(0, q.remainingCapacity()); + assertFalse(q.offer(three)); + threadsStarted.await(); + assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); + assertEquals(0, q.remainingCapacity()); }}); executor.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { - Thread.sleep(SMALL_DELAY_MS); - threadAssertEquals(one, q.take()); + threadsStarted.await(); + assertSame(one, q.take()); }}); joinPool(executor); } /** - * poll retrieves elements across Executor threads + * timed poll retrieves elements across Executor threads */ public void testPollInExecutor() { final LinkedBlockingDeque q = new LinkedBlockingDeque(2); + final CheckedBarrier threadsStarted = new CheckedBarrier(2); ExecutorService executor = Executors.newFixedThreadPool(2); executor.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { - threadAssertNull(q.poll()); - threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); - threadAssertTrue(q.isEmpty()); + assertNull(q.poll()); + threadsStarted.await(); + assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); + checkEmpty(q); }}); executor.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { - Thread.sleep(SMALL_DELAY_MS); + threadsStarted.await(); q.put(one); }}); @@ -1653,7 +1866,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(); @@ -1662,7 +1875,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)