--- jsr166/src/test/tck/LinkedBlockingDequeTest.java 2009/11/16 04:57:10 1.5 +++ jsr166/src/test/tck/LinkedBlockingDequeTest.java 2010/10/06 07:49:22 1.24 @@ -7,15 +7,31 @@ import junit.framework.*; import java.util.*; import java.util.concurrent.*; +import static java.util.concurrent.TimeUnit.MILLISECONDS; 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()); } /** @@ -25,11 +41,11 @@ public class LinkedBlockingDequeTest ext 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))); + for (int i = 0; i < n; i++) + assertTrue(q.offer(new Integer(i))); assertFalse(q.isEmpty()); assertEquals(0, q.remainingCapacity()); - assertEquals(n, q.size()); + assertEquals(n, q.size()); return q; } @@ -66,12 +82,11 @@ public class LinkedBlockingDequeTest ext * offer(null) throws NPE */ public void testOfferFirstNull() { - try { + try { LinkedBlockingDeque q = new LinkedBlockingDeque(); q.offerFirst(null); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** @@ -98,9 +113,9 @@ public class LinkedBlockingDequeTest ext 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()); + assertNull(q.pollFirst()); } /** @@ -109,9 +124,9 @@ public class LinkedBlockingDequeTest ext 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()); + assertNull(q.pollLast()); } /** @@ -120,12 +135,12 @@ public class LinkedBlockingDequeTest ext 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()); + assertNull(q.peekFirst()); } /** @@ -134,12 +149,12 @@ public class LinkedBlockingDequeTest ext 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()); + assertNull(q.peek()); } /** @@ -148,60 +163,74 @@ public class LinkedBlockingDequeTest ext 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()); + 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) {} + } 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(); shouldThrow(); - } - catch (NoSuchElementException success) {} - assertNull(q.peekLast()); + } catch (NoSuchElementException success) {} + assertNull(q.peekLast()); } /** - * 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){ - } + } catch (NoSuchElementException success) {} + assertNull(q.peekFirst()); + } + + /** + * 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()); } /** @@ -210,13 +239,12 @@ public class LinkedBlockingDequeTest ext 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(); shouldThrow(); - } catch (NoSuchElementException success){ - } + } catch (NoSuchElementException success) {} } /** @@ -255,8 +283,8 @@ public class LinkedBlockingDequeTest ext public void testAddFirst() { LinkedBlockingDeque q = populatedDeque(3); q.pollLast(); - q.addFirst(four); - assertEquals(four,q.peekFirst()); + q.addFirst(four); + assertSame(four, q.peekFirst()); } /** @@ -265,8 +293,8 @@ public class LinkedBlockingDequeTest ext public void testAddLast() { LinkedBlockingDeque q = populatedDeque(3); q.pollLast(); - q.addLast(four); - assertEquals(four,q.peekLast()); + q.addLast(four); + assertSame(four, q.peekLast()); } @@ -280,14 +308,13 @@ public class LinkedBlockingDequeTest ext } /** - * Constructor throws IAE if capacity argument nonpositive + * Constructor throws IAE if capacity argument nonpositive */ public void testConstructor2() { try { LinkedBlockingDeque q = new LinkedBlockingDeque(0); shouldThrow(); - } - catch (IllegalArgumentException success) {} + } catch (IllegalArgumentException success) {} } /** @@ -297,8 +324,7 @@ public class LinkedBlockingDequeTest ext try { LinkedBlockingDeque q = new LinkedBlockingDeque(null); shouldThrow(); - } - catch (NullPointerException success) {} + } catch (NullPointerException success) {} } /** @@ -309,8 +335,7 @@ public class LinkedBlockingDequeTest ext Integer[] ints = new Integer[SIZE]; LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints)); shouldThrow(); - } - catch (NullPointerException success) {} + } catch (NullPointerException success) {} } /** @@ -323,23 +348,19 @@ public class LinkedBlockingDequeTest ext ints[i] = new Integer(i); LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints)); shouldThrow(); - } - catch (NullPointerException success) {} + } catch (NullPointerException success) {} } /** * 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()); } /** @@ -378,40 +399,40 @@ public class LinkedBlockingDequeTest ext * offer(null) throws NPE */ public void testOfferNull() { - try { + try { LinkedBlockingDeque q = new LinkedBlockingDeque(1); q.offer(null); shouldThrow(); - } catch (NullPointerException success) { } + } catch (NullPointerException success) {} } /** * add(null) throws NPE */ public void testAddNull() { - try { + try { LinkedBlockingDeque q = new LinkedBlockingDeque(1); q.add(null); shouldThrow(); - } catch (NullPointerException success) { } + } catch (NullPointerException success) {} } /** * push(null) throws NPE */ public void testPushNull() { - try { + try { LinkedBlockingDeque q = new LinkedBlockingDeque(1); q.push(null); shouldThrow(); - } catch (NullPointerException success) { } + } catch (NullPointerException success) {} } /** * push succeeds if not full; throws ISE if full */ public void testPush() { - try { + try { LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); for (int i = 0; i < SIZE; ++i) { Integer I = new Integer(i); @@ -420,8 +441,8 @@ public class LinkedBlockingDequeTest ext } assertEquals(0, q.remainingCapacity()); q.push(new Integer(SIZE)); - } catch (IllegalStateException success){ - } + shouldThrow(); + } catch (IllegalStateException success) {} } /** @@ -430,8 +451,8 @@ public class LinkedBlockingDequeTest ext public void testPushWithPeek() { LinkedBlockingDeque q = populatedDeque(3); q.pollLast(); - q.push(four); - assertEquals(four,q.peekFirst()); + q.push(four); + assertSame(four, q.peekFirst()); } @@ -441,13 +462,12 @@ public class LinkedBlockingDequeTest ext 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(); shouldThrow(); - } catch (NoSuchElementException success){ - } + } catch (NoSuchElementException success) {} } @@ -464,15 +484,15 @@ public class LinkedBlockingDequeTest ext * add succeeds if not full; throws ISE if full */ public void testAdd() { - try { + try { LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); for (int i = 0; i < SIZE; ++i) { assertTrue(q.add(new Integer(i))); } assertEquals(0, q.remainingCapacity()); q.add(new Integer(SIZE)); - } catch (IllegalStateException success){ - } + shouldThrow(); + } catch (IllegalStateException success) {} } /** @@ -483,8 +503,7 @@ public class LinkedBlockingDequeTest ext LinkedBlockingDeque q = new LinkedBlockingDeque(1); q.addAll(null); shouldThrow(); - } - catch (NullPointerException success) {} + } catch (NullPointerException success) {} } /** @@ -495,8 +514,7 @@ public class LinkedBlockingDequeTest ext LinkedBlockingDeque q = populatedDeque(SIZE); q.addAll(q); shouldThrow(); - } - catch (IllegalArgumentException success) {} + } catch (IllegalArgumentException success) {} } /** @@ -508,9 +526,9 @@ public class LinkedBlockingDequeTest ext Integer[] ints = new Integer[SIZE]; q.addAll(Arrays.asList(ints)); shouldThrow(); - } - catch (NullPointerException success) {} + } catch (NullPointerException success) {} } + /** * addAll of a collection with any null elements throws NPE after * possibly adding some elements @@ -523,9 +541,9 @@ public class LinkedBlockingDequeTest ext ints[i] = new Integer(i); q.addAll(Arrays.asList(ints)); shouldThrow(); - } - catch (NullPointerException success) {} + } catch (NullPointerException success) {} } + /** * addAll throws ISE if not enough room */ @@ -537,215 +555,168 @@ public class LinkedBlockingDequeTest ext ints[i] = new Integer(i); q.addAll(Arrays.asList(ints)); shouldThrow(); - } - catch (IllegalStateException success) {} + } 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() { - try { + public void testPutNull() throws InterruptedException { + try { LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); q.put(null); shouldThrow(); - } - catch (NullPointerException success){ - } - catch (InterruptedException ie) { - unexpectedException(); - } - } + } catch (NullPointerException success) {} + } /** * all elements successfully put are contained */ - public void testPut() { - try { - 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()); - } - catch (InterruptedException ie) { - unexpectedException(); + 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() { - 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.put(new Integer(i)); - ++added; - } - q.put(new Integer(SIZE)); - threadShouldThrow(); - } catch (InterruptedException ie){ - threadAssertEquals(added, SIZE); - } - }}); - t.start(); - try { - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); - } - catch (InterruptedException ie) { - unexpectedException(); - } + public void testBlockingPut() throws InterruptedException { + 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.put(i); + assertEquals(SIZE, q.size()); + assertEquals(0, q.remainingCapacity()); + try { + 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() { - 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 e){ - threadAssertTrue(added >= 2); - } - } - }); - try { - t.start(); - Thread.sleep(SHORT_DELAY_MS); - q.take(); - t.interrupt(); - t.join(); - } catch (Exception e){ - unexpectedException(); - } + public void testPutWithTake() throws InterruptedException { + 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); + assertEquals(q.remainingCapacity(), 0); + assertEquals(0, q.take()); + Thread.sleep(SHORT_DELAY_MS); + t.interrupt(); + t.join(); + assertEquals(q.remainingCapacity(), 0); } /** * timed offer times out if full and elements not taken */ - public void testTimedOffer() { + public void testTimedOffer() throws InterruptedException { final LinkedBlockingDeque q = new LinkedBlockingDeque(2); - Thread t = new Thread(new Runnable() { - public void run() { - try { - q.put(new Object()); - q.put(new Object()); - threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); - q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); - threadShouldThrow(); - } catch (InterruptedException success){} - } - }); + Thread t = new Thread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + q.put(new Object()); + q.put(new Object()); + assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); + try { + q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + }}); - try { - t.start(); - Thread.sleep(SMALL_DELAY_MS); - t.interrupt(); - t.join(); - } catch (Exception e){ - unexpectedException(); - } + t.start(); + Thread.sleep(SMALL_DELAY_MS); + t.interrupt(); + t.join(); } /** * take retrieves elements in FIFO order */ - public void testTake() { - try { - LinkedBlockingDeque q = populatedDeque(SIZE); - for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.take()).intValue()); - } - } catch (InterruptedException e){ - unexpectedException(); - } + public void testTake() throws InterruptedException { + LinkedBlockingDeque q = populatedDeque(SIZE); + for (int i = 0; i < SIZE; ++i) { + assertEquals(i, q.take()); + } } /** * take blocks interruptibly when empty */ - public void testTakeFromEmpty() { + public void testTakeFromEmpty() throws InterruptedException { final LinkedBlockingDeque q = new LinkedBlockingDeque(2); - Thread t = new Thread(new Runnable() { - public void run() { - try { - q.take(); - threadShouldThrow(); - } catch (InterruptedException success){ } - } - }); - try { - t.start(); - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); - } catch (Exception e){ - unexpectedException(); - } + Thread t = 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() { - Thread t = new Thread(new Runnable() { - public void run() { - try { - LinkedBlockingDeque q = populatedDeque(SIZE); - for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.take()).intValue()); - } - q.take(); - threadShouldThrow(); - } catch (InterruptedException success){ - } - }}); - t.start(); - try { - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); - } - catch (InterruptedException ie) { - unexpectedException(); - } + public void testBlockingTake() throws InterruptedException { + final LinkedBlockingDeque q = populatedDeque(SIZE); + Thread t = new Thread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + for (int i = 0; i < SIZE; ++i) { + assertEquals(i, q.take()); + } + try { + q.take(); + shouldThrow(); + } catch (InterruptedException success) {} + }}); + + t.start(); + Thread.sleep(SHORT_DELAY_MS); + t.interrupt(); + t.join(); } @@ -755,642 +726,479 @@ 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()); + assertNull(q.poll()); } /** * timed poll with zero timeout succeeds when non-empty, else times out */ - public void testTimedPoll0() { - try { - LinkedBlockingDeque q = populatedDeque(SIZE); - for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue()); - } - assertNull(q.poll(0, TimeUnit.MILLISECONDS)); - } catch (InterruptedException e){ - unexpectedException(); - } + public void testTimedPoll0() throws InterruptedException { + LinkedBlockingDeque q = populatedDeque(SIZE); + for (int i = 0; i < SIZE; ++i) { + assertEquals(i, q.poll(0, MILLISECONDS)); + } + assertNull(q.poll(0, MILLISECONDS)); } /** * timed poll with nonzero timeout succeeds when non-empty, else times out */ - public void testTimedPoll() { - try { - LinkedBlockingDeque q = populatedDeque(SIZE); - for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); - } - assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); - } catch (InterruptedException e){ - unexpectedException(); - } + public void testTimedPoll() throws InterruptedException { + LinkedBlockingDeque q = populatedDeque(SIZE); + for (int i = 0; i < SIZE; ++i) { + assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); + } + assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); } /** * Interrupted timed poll throws InterruptedException instead of * returning timeout status */ - public void testInterruptedTimedPoll() { - Thread t = new Thread(new Runnable() { - public void run() { - try { - LinkedBlockingDeque q = populatedDeque(SIZE); - for (int i = 0; i < SIZE; ++i) { - threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); - } - threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); - } catch (InterruptedException success){ - } - }}); - t.start(); - try { - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); - } - catch (InterruptedException ie) { - unexpectedException(); - } - } - - /** - * timed poll before a delayed offer fails; after offer succeeds; - * on interruption throws - */ - public void testTimedPollWithOffer() { - final LinkedBlockingDeque q = new LinkedBlockingDeque(2); - Thread t = new Thread(new Runnable() { - public void run() { - try { - threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); - q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); - q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); - threadShouldThrow(); - } catch (InterruptedException success) { } + public void testInterruptedTimedPoll() throws InterruptedException { + Thread t = new Thread(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)); } - }); - try { - t.start(); - Thread.sleep(SMALL_DELAY_MS); - assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); - t.interrupt(); - t.join(); - } catch (Exception e){ - unexpectedException(); - } - } + try { + q.poll(SMALL_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + }}); + t.start(); + Thread.sleep(SHORT_DELAY_MS); + t.interrupt(); + t.join(); + } /** * putFirst(null) throws NPE */ - public void testPutFirstNull() { - try { + public void testPutFirstNull() throws InterruptedException { + try { LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); q.putFirst(null); shouldThrow(); - } - catch (NullPointerException success){ - } - catch (InterruptedException ie) { - unexpectedException(); - } + } catch (NullPointerException success) {} } /** * all elements successfully putFirst are contained */ - public void testPutFirst() { - try { - 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)); } - catch (InterruptedException ie) { - unexpectedException(); - } + assertEquals(0, q.remainingCapacity()); } /** * putFirst blocks interruptibly if full */ - public void testBlockingPutFirst() { - 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 ie){ - threadAssertEquals(added, SIZE); - } - }}); - t.start(); - try { - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); - } - catch (InterruptedException ie) { - unexpectedException(); - } + public void testBlockingPutFirst() throws InterruptedException { + 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() { - 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 e){ - threadAssertTrue(added >= 2); - } - } - }); - try { - t.start(); - Thread.sleep(SHORT_DELAY_MS); - q.take(); - t.interrupt(); - t.join(); - } catch (Exception e){ - unexpectedException(); - } + public void testPutFirstWithTake() throws InterruptedException { + 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); + assertEquals(q.remainingCapacity(), 0); + assertEquals(capacity - 1, q.take()); + Thread.sleep(SHORT_DELAY_MS); + t.interrupt(); + t.join(); + assertEquals(q.remainingCapacity(), 0); } /** * timed offerFirst times out if full and elements not taken */ - public void testTimedOfferFirst() { + public void testTimedOfferFirst() throws InterruptedException { final LinkedBlockingDeque q = new LinkedBlockingDeque(2); - Thread t = new Thread(new Runnable() { - public void run() { - try { - q.putFirst(new Object()); - q.putFirst(new Object()); - threadAssertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); - q.offerFirst(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); - threadShouldThrow(); - } catch (InterruptedException success){} - } - }); + Thread t = new Thread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + q.putFirst(new Object()); + q.putFirst(new Object()); + assertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, MILLISECONDS)); + try { + q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + }}); - try { - t.start(); - Thread.sleep(SMALL_DELAY_MS); - t.interrupt(); - t.join(); - } catch (Exception e){ - unexpectedException(); - } + t.start(); + Thread.sleep(SMALL_DELAY_MS); + t.interrupt(); + t.join(); } /** * take retrieves elements in FIFO order */ - public void testTakeFirst() { - try { - LinkedBlockingDeque q = populatedDeque(SIZE); - for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.takeFirst()).intValue()); - } - } catch (InterruptedException e){ - unexpectedException(); - } + public void testTakeFirst() throws InterruptedException { + LinkedBlockingDeque q = populatedDeque(SIZE); + for (int i = 0; i < SIZE; ++i) { + assertEquals(i, q.takeFirst()); + } } /** * takeFirst blocks interruptibly when empty */ - public void testTakeFirstFromEmpty() { + public void testTakeFirstFromEmpty() throws InterruptedException { final LinkedBlockingDeque q = new LinkedBlockingDeque(2); - Thread t = new Thread(new Runnable() { - public void run() { - try { - q.takeFirst(); - threadShouldThrow(); - } catch (InterruptedException success){ } - } - }); - try { - t.start(); - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); - } catch (Exception e){ - unexpectedException(); - } + Thread t = new ThreadShouldThrow(InterruptedException.class) { + public void realRun() throws InterruptedException { + q.takeFirst(); + }}; + + t.start(); + Thread.sleep(SHORT_DELAY_MS); + t.interrupt(); + t.join(); } /** * TakeFirst removes existing elements until empty, then blocks interruptibly */ - public void testBlockingTakeFirst() { - Thread t = new Thread(new Runnable() { - public void run() { - try { - LinkedBlockingDeque q = populatedDeque(SIZE); - for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.takeFirst()).intValue()); - } - q.takeFirst(); - threadShouldThrow(); - } catch (InterruptedException success){ - } - }}); - t.start(); - try { - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); - } - catch (InterruptedException ie) { - unexpectedException(); - } + public void testBlockingTakeFirst() throws InterruptedException { + final LinkedBlockingDeque q = populatedDeque(SIZE); + Thread t = new Thread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + 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); + t.interrupt(); + t.join(); } /** * timed pollFirst with zero timeout succeeds when non-empty, else times out */ - public void testTimedPollFirst0() { - try { - LinkedBlockingDeque q = populatedDeque(SIZE); - for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.pollFirst(0, TimeUnit.MILLISECONDS)).intValue()); - } - assertNull(q.pollFirst(0, TimeUnit.MILLISECONDS)); - } catch (InterruptedException e){ - unexpectedException(); - } + public void testTimedPollFirst0() throws InterruptedException { + LinkedBlockingDeque q = populatedDeque(SIZE); + for (int i = 0; i < SIZE; ++i) { + assertEquals(i, q.pollFirst(0, MILLISECONDS)); + } + assertNull(q.pollFirst(0, MILLISECONDS)); } /** * timed pollFirst with nonzero timeout succeeds when non-empty, else times out */ - public void testTimedPollFirst() { - try { - LinkedBlockingDeque q = populatedDeque(SIZE); - for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); - } - assertNull(q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); - } catch (InterruptedException e){ - unexpectedException(); - } + public void testTimedPollFirst() throws InterruptedException { + LinkedBlockingDeque q = populatedDeque(SIZE); + for (int i = 0; i < SIZE; ++i) { + assertEquals(i, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); + } + assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); } /** * Interrupted timed pollFirst throws InterruptedException instead of * returning timeout status */ - public void testInterruptedTimedPollFirst() { - Thread t = new Thread(new Runnable() { - public void run() { - try { - LinkedBlockingDeque q = populatedDeque(SIZE); - for (int i = 0; i < SIZE; ++i) { - threadAssertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); - } - threadAssertNull(q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); - } catch (InterruptedException success){ - } - }}); - t.start(); - try { - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); - } - catch (InterruptedException ie) { - unexpectedException(); - } + public void testInterruptedTimedPollFirst() throws InterruptedException { + Thread t = new Thread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + LinkedBlockingDeque q = populatedDeque(SIZE); + for (int i = 0; i < SIZE; ++i) { + assertEquals(i, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); + } + try { + q.pollFirst(SMALL_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + }}); + + t.start(); + Thread.sleep(SHORT_DELAY_MS); + t.interrupt(); + t.join(); } /** * timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds; * on interruption throws */ - public void testTimedPollFirstWithOfferFirst() { + public void testTimedPollFirstWithOfferFirst() throws InterruptedException { final LinkedBlockingDeque q = new LinkedBlockingDeque(2); - Thread t = new Thread(new Runnable() { - public void run() { - try { - threadAssertNull(q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); - q.pollFirst(LONG_DELAY_MS, TimeUnit.MILLISECONDS); - q.pollFirst(LONG_DELAY_MS, TimeUnit.MILLISECONDS); - threadShouldThrow(); - } catch (InterruptedException success) { } - } - }); - try { - t.start(); - Thread.sleep(SMALL_DELAY_MS); - assertTrue(q.offerFirst(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); - t.interrupt(); - t.join(); - } catch (Exception e){ - unexpectedException(); - } + Thread t = new Thread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + 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); + assertTrue(q.offerFirst(zero, SHORT_DELAY_MS, MILLISECONDS)); + t.interrupt(); + t.join(); } /** * putLast(null) throws NPE */ - public void testPutLastNull() { - try { + public void testPutLastNull() throws InterruptedException { + try { LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); q.putLast(null); shouldThrow(); - } - catch (NullPointerException success){ - } - catch (InterruptedException ie) { - unexpectedException(); - } + } catch (NullPointerException success) {} } /** * all elements successfully putLast are contained */ - public void testPutLast() { - try { - 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)); } - catch (InterruptedException ie) { - unexpectedException(); - } + assertEquals(0, q.remainingCapacity()); } /** * putLast blocks interruptibly if full */ - public void testBlockingPutLast() { - 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 ie){ - threadAssertEquals(added, SIZE); - } - }}); - t.start(); - try { - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); - } - catch (InterruptedException ie) { - unexpectedException(); - } + public void testBlockingPutLast() throws InterruptedException { + 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() { - 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 e){ - threadAssertTrue(added >= 2); - } - } - }); - try { - t.start(); - Thread.sleep(SHORT_DELAY_MS); - q.take(); - t.interrupt(); - t.join(); - } catch (Exception e){ - unexpectedException(); - } + public void testPutLastWithTake() throws InterruptedException { + 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); + assertEquals(q.remainingCapacity(), 0); + assertEquals(0, q.take()); + Thread.sleep(SHORT_DELAY_MS); + t.interrupt(); + t.join(); + assertEquals(q.remainingCapacity(), 0); } /** * timed offerLast times out if full and elements not taken */ - public void testTimedOfferLast() { + public void testTimedOfferLast() throws InterruptedException { final LinkedBlockingDeque q = new LinkedBlockingDeque(2); - Thread t = new Thread(new Runnable() { - public void run() { - try { - q.putLast(new Object()); - q.putLast(new Object()); - threadAssertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); - q.offerLast(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); - threadShouldThrow(); - } catch (InterruptedException success){} - } - }); + Thread t = new Thread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + q.putLast(new Object()); + q.putLast(new Object()); + assertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, MILLISECONDS)); + try { + q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + }}); - try { - t.start(); - Thread.sleep(SMALL_DELAY_MS); - t.interrupt(); - t.join(); - } catch (Exception e){ - unexpectedException(); - } + t.start(); + Thread.sleep(SMALL_DELAY_MS); + t.interrupt(); + t.join(); } /** * takeLast retrieves elements in FIFO order */ - public void testTakeLast() { - try { - LinkedBlockingDeque q = populatedDeque(SIZE); - for (int i = 0; i < SIZE; ++i) { - assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue()); - } - } catch (InterruptedException e){ - unexpectedException(); - } + public void testTakeLast() throws InterruptedException { + LinkedBlockingDeque q = populatedDeque(SIZE); + for (int i = 0; i < SIZE; ++i) { + assertEquals(SIZE-i-1, q.takeLast()); + } } /** * takeLast blocks interruptibly when empty */ - public void testTakeLastFromEmpty() { + public void testTakeLastFromEmpty() throws InterruptedException { final LinkedBlockingDeque q = new LinkedBlockingDeque(2); - Thread t = new Thread(new Runnable() { - public void run() { - try { - q.takeLast(); - threadShouldThrow(); - } catch (InterruptedException success){ } - } - }); - try { - t.start(); - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); - } catch (Exception e){ - unexpectedException(); - } + 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 */ - public void testBlockingTakeLast() { - Thread t = new Thread(new Runnable() { - public void run() { - try { - LinkedBlockingDeque q = populatedDeque(SIZE); - for (int i = 0; i < SIZE; ++i) { - assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue()); - } - q.takeLast(); - threadShouldThrow(); - } catch (InterruptedException success){ - } - }}); - t.start(); - try { - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); - } - catch (InterruptedException ie) { - unexpectedException(); - } - } + public void testBlockingTakeLast() throws InterruptedException { + final LinkedBlockingDeque q = populatedDeque(SIZE); + Thread t = new Thread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + 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); + t.interrupt(); + t.join(); + } /** * timed pollLast with zero timeout succeeds when non-empty, else times out */ - public void testTimedPollLast0() { - try { - LinkedBlockingDeque q = populatedDeque(SIZE); - for (int i = 0; i < SIZE; ++i) { - assertEquals(SIZE-i-1, ((Integer)q.pollLast(0, TimeUnit.MILLISECONDS)).intValue()); - } - assertNull(q.pollLast(0, TimeUnit.MILLISECONDS)); - } catch (InterruptedException e){ - unexpectedException(); - } + public void testTimedPollLast0() throws InterruptedException { + LinkedBlockingDeque q = populatedDeque(SIZE); + for (int i = 0; i < SIZE; ++i) { + assertEquals(SIZE-i-1, q.pollLast(0, MILLISECONDS)); + } + assertNull(q.pollLast(0, MILLISECONDS)); } /** * timed pollLast with nonzero timeout succeeds when non-empty, else times out */ - public void testTimedPollLast() { - try { - LinkedBlockingDeque q = populatedDeque(SIZE); - for (int i = 0; i < SIZE; ++i) { - assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); - } - assertNull(q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); - } catch (InterruptedException e){ - unexpectedException(); - } + public void testTimedPollLast() throws InterruptedException { + LinkedBlockingDeque q = populatedDeque(SIZE); + for (int i = 0; i < SIZE; ++i) { + assertEquals(SIZE-i-1, q.pollLast(SHORT_DELAY_MS, MILLISECONDS)); + } + assertNull(q.pollLast(SHORT_DELAY_MS, MILLISECONDS)); } /** * Interrupted timed pollLast throws InterruptedException instead of * returning timeout status */ - public void testInterruptedTimedPollLast() { - Thread t = new Thread(new Runnable() { - public void run() { - try { - LinkedBlockingDeque q = populatedDeque(SIZE); - for (int i = 0; i < SIZE; ++i) { - threadAssertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); - } - threadAssertNull(q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); - } catch (InterruptedException success){ - } - }}); + public void testInterruptedTimedPollLast() throws InterruptedException { + 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, q.pollLast(SHORT_DELAY_MS, MILLISECONDS)); + } + try { + q.pollLast(SMALL_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + }}); + t.start(); - try { - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); - } - catch (InterruptedException ie) { - unexpectedException(); - } + Thread.sleep(SHORT_DELAY_MS); + t.interrupt(); + t.join(); } /** - * 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() { + public void testTimedPollWithOfferLast() throws InterruptedException { final LinkedBlockingDeque q = new LinkedBlockingDeque(2); - Thread t = new Thread(new Runnable() { - public void run() { - try { - threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); - q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); - q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); - threadShouldThrow(); - } catch (InterruptedException success) { } - } - }); - try { - t.start(); - Thread.sleep(SMALL_DELAY_MS); - assertTrue(q.offerLast(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); - t.interrupt(); - t.join(); - } catch (Exception e){ - unexpectedException(); - } + 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); + shouldThrow(); + } catch (InterruptedException success) {} + }}); + + t.start(); + Thread.sleep(SMALL_DELAY_MS); + assertTrue(q.offerLast(zero, SHORT_DELAY_MS, MILLISECONDS)); + t.interrupt(); + t.join(); } @@ -1400,14 +1208,13 @@ 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 { q.element(); shouldThrow(); - } - catch (NoSuchElementException success) {} + } catch (NoSuchElementException success) {} } /** @@ -1505,74 +1312,62 @@ public class LinkedBlockingDequeTest ext /** * toArray contains all elements */ - public void testToArray() { + public void testToArray() throws InterruptedException{ LinkedBlockingDeque q = populatedDeque(SIZE); - Object[] o = q.toArray(); - try { - for (int i = 0; i < o.length; i++) - assertEquals(o[i], q.take()); - } catch (InterruptedException e){ - unexpectedException(); - } + Object[] o = q.toArray(); + for (int i = 0; i < o.length; i++) + assertEquals(o[i], q.take()); } /** * toArray(a) contains all elements */ - public void testToArray2() { + public void testToArray2() throws InterruptedException { LinkedBlockingDeque q = populatedDeque(SIZE); - Integer[] ints = new Integer[SIZE]; - ints = (Integer[])q.toArray(ints); - try { - for (int i = 0; i < ints.length; i++) - assertEquals(ints[i], q.take()); - } catch (InterruptedException e){ - unexpectedException(); - } + Integer[] ints = new Integer[SIZE]; + ints = (Integer[])q.toArray(ints); + for (int i = 0; i < ints.length; i++) + assertEquals(ints[i], q.take()); } /** * toArray(null) throws NPE */ public void testToArray_BadArg() { - try { - LinkedBlockingDeque q = populatedDeque(SIZE); - Object o[] = q.toArray(null); - shouldThrow(); - } catch (NullPointerException success){} + LinkedBlockingDeque q = populatedDeque(SIZE); + try { + Object o[] = q.toArray(null); + shouldThrow(); + } catch (NullPointerException success) {} } /** * toArray with incompatible array type throws CCE */ public void testToArray1_BadArg() { - try { - LinkedBlockingDeque q = populatedDeque(SIZE); - Object o[] = q.toArray(new String[10] ); - shouldThrow(); - } catch (ArrayStoreException success){} + LinkedBlockingDeque q = populatedDeque(SIZE); + try { + Object o[] = q.toArray(new String[10]); + shouldThrow(); + } catch (ArrayStoreException success) {} } /** * iterator iterates through all elements */ - public void testIterator() { + public void testIterator() throws InterruptedException { LinkedBlockingDeque q = populatedDeque(SIZE); - Iterator it = q.iterator(); - try { - while (it.hasNext()){ - assertEquals(it.next(), q.take()); - } - } catch (InterruptedException e){ - unexpectedException(); - } + Iterator it = q.iterator(); + while (it.hasNext()) { + assertEquals(it.next(), q.take()); + } } /** * iterator.remove removes current element */ - public void testIteratorRemove () { + public void testIteratorRemove() { final LinkedBlockingDeque q = new LinkedBlockingDeque(3); q.add(two); q.add(one); @@ -1583,8 +1378,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()); } @@ -1600,8 +1395,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); } @@ -1609,19 +1403,14 @@ 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); q.add(three); - try { - for (Iterator it = q.iterator(); it.hasNext();) { - q.remove(); - it.next(); - } - } - catch (ConcurrentModificationException e) { - unexpectedException(); + for (Iterator it = q.iterator(); it.hasNext();) { + q.remove(); + it.next(); } assertEquals(0, q.size()); } @@ -1633,7 +1422,7 @@ public class LinkedBlockingDequeTest ext public void testDescendingIterator() { LinkedBlockingDeque q = populatedDeque(SIZE); int i = 0; - Iterator it = q.descendingIterator(); + Iterator it = q.descendingIterator(); while (it.hasNext()) { assertTrue(q.contains(it.next())); ++i; @@ -1642,8 +1431,8 @@ public class LinkedBlockingDequeTest ext assertFalse(it.hasNext()); try { it.next(); - } catch (NoSuchElementException success) { - } + shouldThrow(); + } catch (NoSuchElementException success) {} } /** @@ -1657,8 +1446,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); @@ -1671,7 +1459,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)); @@ -1711,30 +1499,18 @@ public class LinkedBlockingDequeTest ext q.add(one); q.add(two); ExecutorService executor = Executors.newFixedThreadPool(2); - executor.execute(new Runnable() { - public void run() { - threadAssertFalse(q.offer(three)); - try { - threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); - threadAssertEquals(0, q.remainingCapacity()); - } - catch (InterruptedException e) { - threadUnexpectedException(); - } - } - }); - - executor.execute(new Runnable() { - public void run() { - try { - Thread.sleep(SMALL_DELAY_MS); - threadAssertEquals(one, q.take()); - } - catch (InterruptedException e) { - threadUnexpectedException(); - } - } - }); + executor.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { + 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); + assertSame(one, q.take()); + }}); joinPool(executor); } @@ -1745,30 +1521,18 @@ public class LinkedBlockingDequeTest ext public void testPollInExecutor() { final LinkedBlockingDeque q = new LinkedBlockingDeque(2); ExecutorService executor = Executors.newFixedThreadPool(2); - executor.execute(new Runnable() { - public void run() { - threadAssertNull(q.poll()); - try { - threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); - threadAssertTrue(q.isEmpty()); - } - catch (InterruptedException e) { - threadUnexpectedException(); - } - } - }); - - executor.execute(new Runnable() { - public void run() { - try { - Thread.sleep(SMALL_DELAY_MS); - q.put(one); - } - catch (InterruptedException e) { - threadUnexpectedException(); - } - } - }); + executor.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { + assertNull(q.poll()); + assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); + assertTrue(q.isEmpty()); + }}); + + executor.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { + Thread.sleep(SMALL_DELAY_MS); + q.put(one); + }}); joinPool(executor); } @@ -1776,24 +1540,20 @@ public class LinkedBlockingDequeTest ext /** * A deserialized serialized deque has same elements in same order */ - public void testSerialization() { + public void testSerialization() throws Exception { LinkedBlockingDeque q = populatedDeque(SIZE); - try { - ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); - ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); - out.writeObject(q); - out.close(); - - ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); - ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); - LinkedBlockingDeque r = (LinkedBlockingDeque)in.readObject(); - assertEquals(q.size(), r.size()); - while (!q.isEmpty()) - assertEquals(q.remove(), r.remove()); - } catch (Exception e){ - unexpectedException(); - } + ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); + ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); + out.writeObject(q); + out.close(); + + ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); + ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); + LinkedBlockingDeque r = (LinkedBlockingDeque)in.readObject(); + assertEquals(q.size(), r.size()); + while (!q.isEmpty()) + assertEquals(q.remove(), r.remove()); } /** @@ -1804,8 +1564,7 @@ public class LinkedBlockingDequeTest ext try { q.drainTo(null); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** @@ -1816,8 +1575,7 @@ public class LinkedBlockingDequeTest ext try { q.drainTo(q); shouldThrow(); - } catch (IllegalArgumentException success) { - } + } catch (IllegalArgumentException success) {} } /** @@ -1847,29 +1605,21 @@ public class LinkedBlockingDequeTest ext /** * drainTo empties full deque, unblocking a waiting put. */ - public void testDrainToWithActivePut() { + public void testDrainToWithActivePut() throws InterruptedException { final LinkedBlockingDeque q = populatedDeque(SIZE); - Thread t = new Thread(new Runnable() { - public void run() { - try { - q.put(new Integer(SIZE+1)); - } catch (InterruptedException ie){ - threadUnexpectedException(); - } - } - }); - try { - t.start(); - ArrayList l = new ArrayList(); - q.drainTo(l); - assertTrue(l.size() >= SIZE); - for (int i = 0; i < SIZE; ++i) - assertEquals(l.get(i), new Integer(i)); - t.join(); - assertTrue(q.size() + l.size() >= SIZE); - } catch (Exception e){ - unexpectedException(); - } + Thread t = new Thread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + q.put(new Integer(SIZE+1)); + }}); + + t.start(); + ArrayList l = new ArrayList(); + q.drainTo(l); + assertTrue(l.size() >= SIZE); + for (int i = 0; i < SIZE; ++i) + assertEquals(l.get(i), new Integer(i)); + t.join(); + assertTrue(q.size() + l.size() >= SIZE); } /** @@ -1880,8 +1630,7 @@ public class LinkedBlockingDequeTest ext try { q.drainTo(null, 0); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} } /** @@ -1892,8 +1641,7 @@ public class LinkedBlockingDequeTest ext try { q.drainTo(q, 0); shouldThrow(); - } catch (IllegalArgumentException success) { - } + } catch (IllegalArgumentException success) {} } /**