--- jsr166/src/test/tck/LinkedBlockingDequeTest.java 2009/11/21 08:37:39 1.9 +++ jsr166/src/test/tck/LinkedBlockingDequeTest.java 2010/11/04 01:04:54 1.31 @@ -11,12 +11,27 @@ 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()); } /** @@ -93,92 +108,93 @@ 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(); shouldThrow(); } catch (NoSuchElementException success) {} + assertNull(q.peekFirst()); } /** - * 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(); @@ -188,26 +204,42 @@ 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(); shouldThrow(); } catch (NoSuchElementException success) {} + assertNull(q.peekFirst()); } /** - * remove removes next 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, q.removeLast()); + } + try { + q.removeLast(); + shouldThrow(); + } catch (NoSuchElementException success) {} + assertNull(q.peekLast()); + } + + /** + * 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(); @@ -252,7 +284,7 @@ public class LinkedBlockingDequeTest ext LinkedBlockingDeque q = populatedDeque(3); q.pollLast(); q.addFirst(four); - assertEquals(four,q.peekFirst()); + assertSame(four, q.peekFirst()); } /** @@ -262,7 +294,7 @@ public class LinkedBlockingDequeTest ext LinkedBlockingDeque q = populatedDeque(3); q.pollLast(); q.addLast(four); - assertEquals(four,q.peekLast()); + assertSame(four, q.peekLast()); } @@ -276,7 +308,7 @@ public class LinkedBlockingDequeTest ext } /** - * Constructor throws IAE if capacity argument nonpositive + * Constructor throws IAE if capacity argument nonpositive */ public void testConstructor2() { try { @@ -323,15 +355,12 @@ public class LinkedBlockingDequeTest ext * Deque contains all elements of collection used to initialize */ public void testConstructor6() { - try { - Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE; ++i) - ints[i] = new Integer(i); - LinkedBlockingDeque q = new LinkedBlockingDeque(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] = new Integer(i); + LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints)); + for (int i = 0; i < SIZE; ++i) + assertEquals(ints[i], q.poll()); } /** @@ -423,17 +452,17 @@ 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(); @@ -499,6 +528,7 @@ public class LinkedBlockingDequeTest ext shouldThrow(); } catch (NullPointerException success) {} } + /** * addAll of a collection with any null elements throws NPE after * possibly adding some elements @@ -513,6 +543,7 @@ public class LinkedBlockingDequeTest ext shouldThrow(); } catch (NullPointerException success) {} } + /** * addAll throws ISE if not enough room */ @@ -526,104 +557,96 @@ public class LinkedBlockingDequeTest ext shouldThrow(); } catch (IllegalStateException success) {} } + /** * Deque 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); - LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); - 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] = new Integer(i); + LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); + 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 */ - public void testPutNull() throws InterruptedException { + public void testPutNull() throws InterruptedException { try { LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); q.put(null); shouldThrow(); } catch (NullPointerException success) {} - } + } /** * all elements successfully put are contained */ - public void testPut() throws InterruptedException { - LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); - for (int i = 0; i < SIZE; ++i) { - Integer I = new Integer(i); - q.put(I); - assertTrue(q.contains(I)); - } - assertEquals(0, q.remainingCapacity()); + public void testPut() throws InterruptedException { + LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); + for (int i = 0; i < SIZE; ++i) { + Integer I = new Integer(i); + q.put(I); + assertTrue(q.contains(I)); + } + assertEquals(0, q.remainingCapacity()); } /** * put blocks interruptibly if full */ public void testBlockingPut() throws InterruptedException { + final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); Thread t = new Thread(new CheckedRunnable() { - public void realRun() { - int added = 0; + public void realRun() throws InterruptedException { + for (int i = 0; i < SIZE; ++i) + q.put(i); + assertEquals(SIZE, q.size()); + assertEquals(0, q.remainingCapacity()); 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) {} }}); t.start(); Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); + assertEquals(SIZE, q.size()); + assertEquals(0, q.remainingCapacity()); } /** * put blocks waiting for take when full */ public void testPutWithTake() throws InterruptedException { - final LinkedBlockingDeque q = new LinkedBlockingDeque(2); - Thread t = new Thread(new Runnable() { - public void run() { - int added = 0; - 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); - } - } - }); + final int capacity = 2; + final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity); + Thread t = new Thread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + for (int i = 0; i < capacity + 1; i++) + q.put(i); + try { + q.put(99); + shouldThrow(); + } catch (InterruptedException success) {} + }}); t.start(); Thread.sleep(SHORT_DELAY_MS); - q.take(); + assertEquals(q.remainingCapacity(), 0); + assertEquals(0, q.take()); + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); + assertEquals(q.remainingCapacity(), 0); } /** @@ -631,13 +654,16 @@ public class LinkedBlockingDequeTest ext */ public void testTimedOffer() throws InterruptedException { final LinkedBlockingDeque q = new LinkedBlockingDeque(2); - Thread t = new ThreadShouldThrow(InterruptedException.class) { + Thread t = new Thread(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); - }}; + assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); + try { + q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + }}); t.start(); Thread.sleep(SMALL_DELAY_MS); @@ -651,38 +677,25 @@ 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 */ public void testBlockingTake() throws InterruptedException { - Thread t = new ThreadShouldThrow(InterruptedException.class) { + final LinkedBlockingDeque q = populatedDeque(SIZE); + Thread t = new Thread(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(); - }}; + try { + q.take(); + shouldThrow(); + } catch (InterruptedException success) {} + }}); t.start(); Thread.sleep(SHORT_DELAY_MS); @@ -697,7 +710,7 @@ public class LinkedBlockingDequeTest ext 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()); } @@ -708,7 +721,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)); } @@ -719,7 +732,7 @@ 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()); + assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); } assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); } @@ -729,14 +742,17 @@ public class LinkedBlockingDequeTest ext * returning timeout status */ public void testInterruptedTimedPoll() throws InterruptedException { - Thread t = new ThreadShouldThrow(InterruptedException.class) { + Thread t = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); + assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); } - q.poll(SMALL_DELAY_MS, MILLISECONDS); - }}; + try { + q.poll(SMALL_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + }}); t.start(); Thread.sleep(SHORT_DELAY_MS); @@ -745,27 +761,6 @@ public class LinkedBlockingDequeTest ext } /** - * 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 ThreadShouldThrow(InterruptedException.class) { - public void realRun() throws InterruptedException { - threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); - q.poll(LONG_DELAY_MS, MILLISECONDS); - q.poll(LONG_DELAY_MS, MILLISECONDS); - }}; - - t.start(); - Thread.sleep(SMALL_DELAY_MS); - assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); - t.interrupt(); - t.join(); - } - - - /** * putFirst(null) throws NPE */ public void testPutFirstNull() throws InterruptedException { @@ -793,57 +788,51 @@ public class LinkedBlockingDequeTest ext * putFirst blocks interruptibly if full */ public void testBlockingPutFirst() throws InterruptedException { - Thread t = new Thread(new Runnable() { - public void run() { - int added = 0; - 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); - } - }}); + final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); + Thread t = new Thread(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()); + try { + q.putFirst(99); + shouldThrow(); + } catch (InterruptedException success) {} + }}); t.start(); Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); + assertEquals(SIZE, q.size()); + assertEquals(0, q.remainingCapacity()); } /** * putFirst blocks waiting for take when full */ public void testPutFirstWithTake() throws InterruptedException { - final LinkedBlockingDeque q = new LinkedBlockingDeque(2); - Thread t = new Thread(new Runnable() { - public void run() { - int added = 0; - 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); - } - } - }); + final int capacity = 2; + final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity); + Thread t = new Thread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + for (int i = 0; i < capacity + 1; i++) + q.putFirst(i); + try { + q.putFirst(99); + shouldThrow(); + } catch (InterruptedException success) {} + }}); t.start(); Thread.sleep(SHORT_DELAY_MS); - q.take(); + assertEquals(q.remainingCapacity(), 0); + assertEquals(capacity - 1, q.take()); + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); + assertEquals(q.remainingCapacity(), 0); } /** @@ -851,13 +840,16 @@ public class LinkedBlockingDequeTest ext */ public void testTimedOfferFirst() throws InterruptedException { final LinkedBlockingDeque q = new LinkedBlockingDeque(2); - Thread t = new ThreadShouldThrow(InterruptedException.class) { + Thread t = new Thread(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); - }}; + assertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, MILLISECONDS)); + try { + q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + }}); t.start(); Thread.sleep(SMALL_DELAY_MS); @@ -871,7 +863,7 @@ 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()); } } @@ -895,14 +887,16 @@ public class LinkedBlockingDequeTest ext * 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); + Thread t = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { - LinkedBlockingDeque q = populatedDeque(SIZE); - for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.takeFirst()).intValue()); - } - q.takeFirst(); - }}; + for (int i = 0; i < SIZE; ++i) + assertEquals(i, q.takeFirst()); + try { + q.takeFirst(); + shouldThrow(); + } catch (InterruptedException success) {} + }}); t.start(); Thread.sleep(SHORT_DELAY_MS); @@ -917,7 +911,7 @@ public class LinkedBlockingDequeTest ext 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)); } @@ -928,7 +922,7 @@ 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()); + assertEquals(i, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); } assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); } @@ -938,14 +932,17 @@ public class LinkedBlockingDequeTest ext * returning timeout status */ public void testInterruptedTimedPollFirst() throws InterruptedException { - Thread t = new ThreadShouldThrow(InterruptedException.class) { + Thread t = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - threadAssertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue()); + assertEquals(i, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); } - q.pollFirst(SMALL_DELAY_MS, MILLISECONDS); - }}; + try { + q.pollFirst(SMALL_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + }}); t.start(); Thread.sleep(SHORT_DELAY_MS); @@ -954,17 +951,20 @@ 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); - Thread t = new ThreadShouldThrow(InterruptedException.class) { + Thread t = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { - threadAssertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); - q.pollFirst(LONG_DELAY_MS, MILLISECONDS); - q.pollFirst(LONG_DELAY_MS, MILLISECONDS); - }}; + assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); + assertSame(zero, q.pollFirst(LONG_DELAY_MS, MILLISECONDS)); + try { + q.pollFirst(LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + }}); t.start(); Thread.sleep(SMALL_DELAY_MS); @@ -1001,56 +1001,51 @@ public class LinkedBlockingDequeTest ext * putLast blocks interruptibly if full */ public void testBlockingPutLast() throws InterruptedException { - Thread t = new Thread(new Runnable() { - public void run() { - int added = 0; - 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); - } - }}); + final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); + Thread t = new Thread(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()); + try { + q.putLast(99); + shouldThrow(); + } catch (InterruptedException success) {} + }}); + t.start(); Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); + assertEquals(SIZE, q.size()); + assertEquals(0, q.remainingCapacity()); } /** * putLast blocks waiting for take when full */ public void testPutLastWithTake() throws InterruptedException { - final LinkedBlockingDeque q = new LinkedBlockingDeque(2); - Thread t = new Thread(new Runnable() { - public void run() { - int added = 0; - 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); - } - } - }); + final int capacity = 2; + final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity); + Thread t = new Thread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + for (int i = 0; i < capacity + 1; i++) + q.putLast(i); + try { + q.putLast(99); + shouldThrow(); + } catch (InterruptedException success) {} + }}); t.start(); Thread.sleep(SHORT_DELAY_MS); - q.take(); + assertEquals(q.remainingCapacity(), 0); + assertEquals(0, q.take()); + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); + assertEquals(q.remainingCapacity(), 0); } /** @@ -1058,13 +1053,16 @@ public class LinkedBlockingDequeTest ext */ public void testTimedOfferLast() throws InterruptedException { final LinkedBlockingDeque q = new LinkedBlockingDeque(2); - Thread t = new ThreadShouldThrow(InterruptedException.class) { + Thread t = new Thread(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); - }}; + assertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, MILLISECONDS)); + try { + q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + }}); t.start(); Thread.sleep(SMALL_DELAY_MS); @@ -1078,7 +1076,7 @@ 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()); } } @@ -1102,14 +1100,16 @@ public class LinkedBlockingDequeTest ext * 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); + Thread t = new Thread(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()); - } - q.takeLast(); - }}; + for (int i = 0; i < SIZE; ++i) + assertEquals(SIZE - 1 - i, q.takeLast()); + try { + q.takeLast(); + shouldThrow(); + } catch (InterruptedException success) {} + }}); t.start(); Thread.sleep(SHORT_DELAY_MS); @@ -1117,14 +1117,13 @@ public class LinkedBlockingDequeTest ext t.join(); } - /** * 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)); } @@ -1135,7 +1134,7 @@ 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()); + assertEquals(SIZE-i-1, q.pollLast(SHORT_DELAY_MS, MILLISECONDS)); } assertNull(q.pollLast(SHORT_DELAY_MS, MILLISECONDS)); } @@ -1145,14 +1144,17 @@ public class LinkedBlockingDequeTest ext * returning timeout status */ public void testInterruptedTimedPollLast() throws InterruptedException { - Thread t = new ThreadShouldThrow(InterruptedException.class) { + Thread t = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - threadAssertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue()); + assertEquals(SIZE-i-1, q.pollLast(SHORT_DELAY_MS, MILLISECONDS)); } - q.pollLast(SMALL_DELAY_MS, MILLISECONDS); - }}; + try { + q.pollLast(SMALL_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + }}); t.start(); Thread.sleep(SHORT_DELAY_MS); @@ -1161,8 +1163,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); @@ -1190,7 +1192,7 @@ public class LinkedBlockingDequeTest ext 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 { @@ -1292,44 +1294,44 @@ 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 { + public void testToArray2() { LinkedBlockingDeque q = populatedDeque(SIZE); Integer[] ints = new Integer[SIZE]; - ints = (Integer[])q.toArray(ints); + assertSame(ints, q.toArray(ints)); 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) {} } @@ -1349,7 +1351,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); @@ -1360,8 +1362,8 @@ 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()); } @@ -1377,8 +1379,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); } @@ -1386,7 +1387,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); @@ -1400,7 +1401,7 @@ public class LinkedBlockingDequeTest ext /** - * Descending iterator iterates through all elements + * Descending iterator iterates through all elements */ public void testDescendingIterator() { LinkedBlockingDeque q = populatedDeque(SIZE); @@ -1419,7 +1420,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(); @@ -1429,8 +1430,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); @@ -1443,7 +1443,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)); @@ -1485,15 +1485,15 @@ public class LinkedBlockingDequeTest ext ExecutorService executor = Executors.newFixedThreadPool(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)); + assertTrue(q.offer(three, MEDIUM_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()); + assertSame(one, q.take()); }}); joinPool(executor); @@ -1507,9 +1507,9 @@ public class LinkedBlockingDequeTest ext 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()); + assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); + assertTrue(q.isEmpty()); }}); executor.execute(new CheckedRunnable() { @@ -1559,8 +1559,7 @@ public class LinkedBlockingDequeTest ext try { q.drainTo(q); shouldThrow(); - } catch (IllegalArgumentException success) { - } + } catch (IllegalArgumentException success) {} } /** @@ -1630,7 +1629,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(); @@ -1639,7 +1638,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)