--- jsr166/src/test/tck/LinkedBlockingDequeTest.java 2009/11/02 20:28:31 1.4 +++ jsr166/src/test/tck/LinkedBlockingDequeTest.java 2021/01/27 01:57:24 1.89 @@ -1,35 +1,73 @@ /* * 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.*; -import java.util.*; -import java.util.concurrent.*; -import java.io.*; +import static java.util.concurrent.TimeUnit.MILLISECONDS; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Deque; +import java.util.Iterator; +import java.util.NoSuchElementException; +import java.util.Queue; +import java.util.concurrent.BlockingDeque; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Executors; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.LinkedBlockingDeque; + +import junit.framework.Test; 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(SIZE); + } + } + public static void main(String[] args) { - junit.textui.TestRunner.run (suite()); + main(suite(), args); } public static Test suite() { - return new TestSuite(LinkedBlockingDequeTest.class); + class Implementation implements CollectionImplementation { + public Class klazz() { return LinkedBlockingDeque.class; } + public Collection emptyCollection() { return new LinkedBlockingDeque(); } + public Object makeElement(int i) { return JSR166TestCase.itemFor(i); } + public boolean isConcurrent() { return true; } + public boolean permitsNulls() { return false; } + } + return newTestSuite(LinkedBlockingDequeTest.class, + new Unbounded().testSuite(), + new Bounded().testSuite(), + CollectionTest.testSuite(new Implementation())); } /** - * Create a deque of given size containing consecutive - * Integers 0 ... n. + * Returns a new deque of given size containing consecutive + * Items 0 ... n - 1. */ - private LinkedBlockingDeque populatedDeque(int n) { - LinkedBlockingDeque q = new LinkedBlockingDeque(n); + private static 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++) + mustOffer(q, i); assertFalse(q.isEmpty()); - assertEquals(0, q.remainingCapacity()); - assertEquals(n, q.size()); + mustEqual(0, q.remainingCapacity()); + mustEqual(n, q.size()); + mustEqual(0, q.peekFirst()); + mustEqual((n - 1), q.peekLast()); return q; } @@ -37,11 +75,11 @@ public class LinkedBlockingDequeTest ext * isEmpty is true before add, false after */ public void testEmpty() { - LinkedBlockingDeque q = new LinkedBlockingDeque(); + LinkedBlockingDeque q = new LinkedBlockingDeque<>(); assertTrue(q.isEmpty()); - q.add(new Integer(1)); + q.add(one); assertFalse(q.isEmpty()); - q.add(new Integer(2)); + q.add(two); q.removeFirst(); q.removeFirst(); assertTrue(q.isEmpty()); @@ -51,185 +89,208 @@ public class LinkedBlockingDequeTest ext * size changes when elements added and removed */ public void testSize() { - LinkedBlockingDeque q = populatedDeque(SIZE); + LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(SIZE-i, q.size()); + mustEqual(SIZE - i, q.size()); q.removeFirst(); } for (int i = 0; i < SIZE; ++i) { - assertEquals(i, q.size()); - q.add(new Integer(i)); + mustEqual(i, q.size()); + mustAdd(q, one); } } /** - * offer(null) throws NPE + * offerFirst(null) throws NullPointerException */ public void testOfferFirstNull() { - try { - LinkedBlockingDeque q = new LinkedBlockingDeque(); + LinkedBlockingDeque q = new LinkedBlockingDeque<>(); + try { q.offerFirst(null); shouldThrow(); - } catch (NullPointerException success) { - } + } catch (NullPointerException success) {} + } + + /** + * offerLast(null) throws NullPointerException + */ + public void testOfferLastNull() { + LinkedBlockingDeque q = new LinkedBlockingDeque<>(); + try { + q.offerLast(null); + shouldThrow(); + } catch (NullPointerException success) {} } /** * OfferFirst succeeds */ public void testOfferFirst() { - LinkedBlockingDeque q = new LinkedBlockingDeque(); - assertTrue(q.offerFirst(new Integer(0))); - assertTrue(q.offerFirst(new Integer(1))); + LinkedBlockingDeque q = new LinkedBlockingDeque<>(); + assertTrue(q.offerFirst(zero)); + assertTrue(q.offerFirst(two)); } /** * OfferLast succeeds */ public void testOfferLast() { - LinkedBlockingDeque q = new LinkedBlockingDeque(); - assertTrue(q.offerLast(new Integer(0))); - assertTrue(q.offerLast(new Integer(1))); + LinkedBlockingDeque q = new LinkedBlockingDeque<>(); + assertTrue(q.offerLast(zero)); + assertTrue(q.offerLast(one)); } /** - * pollFirst succeeds unless empty + * pollFirst succeeds unless empty */ public void testPollFirst() { - LinkedBlockingDeque q = populatedDeque(SIZE); + LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.pollFirst()).intValue()); + mustEqual(i, q.pollFirst()); } - assertNull(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()); + LinkedBlockingDeque q = populatedDeque(SIZE); + for (int i = SIZE - 1; i >= 0; --i) { + mustEqual(i, q.pollLast()); } - assertNull(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); + LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.peekFirst()).intValue()); - q.pollFirst(); + mustEqual(i, q.peekFirst()); + mustEqual(i, q.pollFirst()); assertTrue(q.peekFirst() == null || - i != ((Integer)q.peekFirst()).intValue()); + !q.peekFirst().equals(i)); } - assertNull(q.peekFirst()); + 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); + LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.peek()).intValue()); - q.pollFirst(); + mustEqual(i, q.peek()); + mustEqual(i, q.pollFirst()); assertTrue(q.peek() == null || - i != ((Integer)q.peek()).intValue()); + !q.peek().equals(i)); } - assertNull(q.peek()); + 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(); + LinkedBlockingDeque q = populatedDeque(SIZE); + for (int i = SIZE - 1; i >= 0; --i) { + mustEqual(i, q.peekLast()); + mustEqual(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); + LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.getFirst()).intValue()); - q.pollFirst(); + mustEqual(i, q.getFirst()); + mustEqual(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(); + LinkedBlockingDeque q = populatedDeque(SIZE); + for (int i = SIZE - 1; i >= 0; --i) { + mustEqual(i, q.getLast()); + mustEqual(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); + LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.removeFirst()).intValue()); + mustEqual(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) { + mustEqual(i, q.removeLast()); + } + try { + q.removeLast(); + shouldThrow(); + } catch (NoSuchElementException success) {} + assertNull(q.peekLast()); } /** - * 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); + LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.remove()).intValue()); + mustEqual(i, q.remove()); } try { q.remove(); shouldThrow(); - } catch (NoSuchElementException success){ - } + } catch (NoSuchElementException success) {} } /** * removeFirstOccurrence(x) removes x and returns true if present */ public void testRemoveFirstOccurrence() { - LinkedBlockingDeque q = populatedDeque(SIZE); - for (int i = 1; i < SIZE; i+=2) { - assertTrue(q.removeFirstOccurrence(new Integer(i))); - } - for (int i = 0; i < SIZE; i+=2) { - assertTrue(q.removeFirstOccurrence(new Integer(i))); - assertFalse(q.removeFirstOccurrence(new Integer(i+1))); + LinkedBlockingDeque q = populatedDeque(SIZE); + for (int i = 1; i < SIZE; i += 2) { + assertTrue(q.removeFirstOccurrence(itemFor(i))); + } + for (int i = 0; i < SIZE; i += 2) { + assertTrue(q.removeFirstOccurrence(itemFor(i))); + assertFalse(q.removeFirstOccurrence(itemFor(i + 1))); } assertTrue(q.isEmpty()); } @@ -238,13 +299,13 @@ public class LinkedBlockingDequeTest ext * removeLastOccurrence(x) removes x and returns true if present */ public void testRemoveLastOccurrence() { - LinkedBlockingDeque q = populatedDeque(SIZE); - for (int i = 1; i < SIZE; i+=2) { - assertTrue(q.removeLastOccurrence(new Integer(i))); - } - for (int i = 0; i < SIZE; i+=2) { - assertTrue(q.removeLastOccurrence(new Integer(i))); - assertFalse(q.removeLastOccurrence(new Integer(i+1))); + LinkedBlockingDeque q = populatedDeque(SIZE); + for (int i = 1; i < SIZE; i += 2) { + assertTrue(q.removeLastOccurrence(itemFor(i))); + } + for (int i = 0; i < SIZE; i += 2) { + assertTrue(q.removeLastOccurrence(itemFor(i))); + assertFalse(q.removeLastOccurrence(itemFor(i + 1))); } assertTrue(q.isEmpty()); } @@ -253,107 +314,98 @@ public class LinkedBlockingDequeTest ext * peekFirst returns element inserted with addFirst */ public void testAddFirst() { - LinkedBlockingDeque q = populatedDeque(3); + LinkedBlockingDeque q = populatedDeque(3); q.pollLast(); - q.addFirst(four); - assertEquals(four,q.peekFirst()); + q.addFirst(four); + assertSame(four, q.peekFirst()); } /** * peekLast returns element inserted with addLast */ public void testAddLast() { - LinkedBlockingDeque q = populatedDeque(3); + LinkedBlockingDeque q = populatedDeque(3); q.pollLast(); - q.addLast(four); - assertEquals(four,q.peekLast()); + q.addLast(four); + assertSame(four, q.peekLast()); } - /** * A new deque has the indicated capacity, or Integer.MAX_VALUE if * none given */ public void testConstructor1() { - assertEquals(SIZE, new LinkedBlockingDeque(SIZE).remainingCapacity()); - assertEquals(Integer.MAX_VALUE, new LinkedBlockingDeque().remainingCapacity()); + mustEqual(SIZE, new LinkedBlockingDeque(SIZE).remainingCapacity()); + mustEqual(Integer.MAX_VALUE, new LinkedBlockingDeque().remainingCapacity()); } /** - * Constructor throws IAE if capacity argument nonpositive + * Constructor throws IllegalArgumentException if capacity argument nonpositive */ public void testConstructor2() { try { - LinkedBlockingDeque q = new LinkedBlockingDeque(0); + new LinkedBlockingDeque(0); shouldThrow(); - } - catch (IllegalArgumentException success) {} + } catch (IllegalArgumentException success) {} } /** - * Initializing from null Collection throws NPE + * Initializing from null Collection throws NullPointerException */ public void testConstructor3() { try { - LinkedBlockingDeque q = new LinkedBlockingDeque(null); + new LinkedBlockingDeque(null); shouldThrow(); - } - catch (NullPointerException success) {} + } catch (NullPointerException success) {} } /** - * Initializing from Collection of null elements throws NPE + * Initializing from Collection of null elements throws NullPointerException */ public void testConstructor4() { + Collection elements = Arrays.asList(new Item[SIZE]); try { - Integer[] ints = new Integer[SIZE]; - LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints)); + new LinkedBlockingDeque(elements); shouldThrow(); - } - catch (NullPointerException success) {} + } catch (NullPointerException success) {} } /** - * Initializing from Collection with some null elements throws NPE + * Initializing from Collection with some null elements throws + * NullPointerException */ public void testConstructor5() { + Item[] items = new Item[2]; + items[0] = zero; + Collection elements = Arrays.asList(items); try { - Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE-1; ++i) - ints[i] = new Integer(i); - LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints)); + new LinkedBlockingDeque(elements); 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 {} + Item[] items = defaultItems; + LinkedBlockingDeque q = new LinkedBlockingDeque<>(Arrays.asList(items)); + for (int i = 0; i < SIZE; ++i) + mustEqual(items[i], q.poll()); } /** * Deque transitions from empty to full when elements added */ public void testEmptyFull() { - LinkedBlockingDeque q = new LinkedBlockingDeque(2); + LinkedBlockingDeque q = new LinkedBlockingDeque<>(2); assertTrue(q.isEmpty()); - assertEquals("should have room for 2", 2, q.remainingCapacity()); + mustEqual(2, q.remainingCapacity()); q.add(one); assertFalse(q.isEmpty()); q.add(two); assertFalse(q.isEmpty()); - assertEquals(0, q.remainingCapacity()); + mustEqual(0, q.remainingCapacity()); assertFalse(q.offer(three)); } @@ -361,1079 +413,1048 @@ public class LinkedBlockingDequeTest ext * remainingCapacity decreases on add, increases on remove */ public void testRemainingCapacity() { - LinkedBlockingDeque q = populatedDeque(SIZE); + BlockingQueue q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, q.remainingCapacity()); - assertEquals(SIZE-i, q.size()); - q.remove(); + mustEqual(i, q.remainingCapacity()); + mustEqual(SIZE, q.size() + q.remainingCapacity()); + mustEqual(i, q.remove()); } for (int i = 0; i < SIZE; ++i) { - assertEquals(SIZE-i, q.remainingCapacity()); - assertEquals(i, q.size()); - q.add(new Integer(i)); + mustEqual(SIZE - i, q.remainingCapacity()); + mustEqual(SIZE, q.size() + q.remainingCapacity()); + mustAdd(q, i); } } /** - * offer(null) throws NPE - */ - public void testOfferNull() { - try { - LinkedBlockingDeque q = new LinkedBlockingDeque(1); - q.offer(null); - shouldThrow(); - } catch (NullPointerException success) { } - } - - /** - * add(null) throws NPE - */ - public void testAddNull() { - try { - LinkedBlockingDeque q = new LinkedBlockingDeque(1); - q.add(null); - shouldThrow(); - } catch (NullPointerException success) { } - } - - /** * push(null) throws NPE */ public void testPushNull() { - try { - LinkedBlockingDeque q = new LinkedBlockingDeque(1); + LinkedBlockingDeque q = new LinkedBlockingDeque<>(1); + try { q.push(null); shouldThrow(); - } catch (NullPointerException success) { } + } catch (NullPointerException success) {} } /** - * push succeeds if not full; throws ISE if full + * push succeeds if not full; throws IllegalStateException if full */ public void testPush() { - try { - LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); - for (int i = 0; i < SIZE; ++i) { - Integer I = new Integer(i); - q.push(I); - assertEquals(I, q.peek()); - } - assertEquals(0, q.remainingCapacity()); - q.push(new Integer(SIZE)); - } catch (IllegalStateException success){ - } + LinkedBlockingDeque q = new LinkedBlockingDeque<>(SIZE); + for (int i = 0; i < SIZE; ++i) { + Item x = itemFor(i); + q.push(x); + mustEqual(x, q.peek()); + } + mustEqual(0, q.remainingCapacity()); + try { + q.push(itemFor(SIZE)); + shouldThrow(); + } catch (IllegalStateException success) {} } /** * peekFirst returns element inserted with push */ public void testPushWithPeek() { - LinkedBlockingDeque q = populatedDeque(3); + LinkedBlockingDeque q = populatedDeque(3); q.pollLast(); - q.push(four); - assertEquals(four,q.peekFirst()); + q.push(four); + 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); + LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.pop()).intValue()); + mustEqual(i, q.pop()); } try { q.pop(); shouldThrow(); - } catch (NoSuchElementException success){ - } + } catch (NoSuchElementException success) {} } - /** * Offer succeeds if not full; fails if full */ public void testOffer() { - LinkedBlockingDeque q = new LinkedBlockingDeque(1); + LinkedBlockingDeque q = new LinkedBlockingDeque<>(1); assertTrue(q.offer(zero)); assertFalse(q.offer(one)); } /** - * add succeeds if not full; throws ISE if full + * add succeeds if not full; throws IllegalStateException if full */ public void testAdd() { - 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){ - } - } - - /** - * addAll(null) throws NPE - */ - public void testAddAll1() { + LinkedBlockingDeque q = new LinkedBlockingDeque<>(SIZE); + for (int i = 0; i < SIZE; ++i) + mustAdd(q, i); + mustEqual(0, q.remainingCapacity()); try { - LinkedBlockingDeque q = new LinkedBlockingDeque(1); - q.addAll(null); + q.add(itemFor(SIZE)); shouldThrow(); - } - catch (NullPointerException success) {} + } catch (IllegalStateException success) {} } /** - * addAll(this) throws IAE + * addAll(this) throws IllegalArgumentException */ public void testAddAllSelf() { + LinkedBlockingDeque q = populatedDeque(SIZE); try { - LinkedBlockingDeque q = populatedDeque(SIZE); q.addAll(q); shouldThrow(); - } - catch (IllegalArgumentException success) {} + } catch (IllegalArgumentException success) {} } /** - * addAll of a collection with null elements throws NPE - */ - public void testAddAll2() { - try { - LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); - Integer[] ints = new Integer[SIZE]; - q.addAll(Arrays.asList(ints)); - shouldThrow(); - } - catch (NullPointerException success) {} - } - /** * addAll of a collection with any null elements throws NPE after * possibly adding some elements */ public void testAddAll3() { + LinkedBlockingDeque q = new LinkedBlockingDeque<>(SIZE); + Item[] items = new Item[2]; items[0] = zero; + Collection elements = Arrays.asList(items); try { - LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); - Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE-1; ++i) - ints[i] = new Integer(i); - q.addAll(Arrays.asList(ints)); + q.addAll(elements); shouldThrow(); - } - catch (NullPointerException success) {} + } catch (NullPointerException success) {} } + /** - * addAll throws ISE if not enough room + * addAll throws IllegalStateException if not enough room */ public void testAddAll4() { + LinkedBlockingDeque q = new LinkedBlockingDeque<>(SIZE - 1); + Item[] items = defaultItems; + Collection elements = Arrays.asList(items); try { - LinkedBlockingDeque q = new LinkedBlockingDeque(1); - Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE; ++i) - ints[i] = new Integer(i); - q.addAll(Arrays.asList(ints)); + q.addAll(elements); 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 {} + Item[] empty = new Item[0]; + Item[] items = defaultItems; + LinkedBlockingDeque q = new LinkedBlockingDeque<>(SIZE); + assertFalse(q.addAll(Arrays.asList(empty))); + assertTrue(q.addAll(Arrays.asList(items))); + for (int i = 0; i < SIZE; ++i) + mustEqual(items[i], q.poll()); } - - /** - * put(null) throws NPE - */ - public void testPutNull() { - try { - LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); - q.put(null); - shouldThrow(); - } - catch (NullPointerException success){ - } - catch (InterruptedException ie) { - unexpectedException(); - } - } - /** * 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) { + Item x = itemFor(i); + q.put(x); + mustContain(q, x); } + mustEqual(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); + 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(itemFor(i)); + mustEqual(SIZE, q.size()); + mustEqual(0, q.remainingCapacity()); + + Thread.currentThread().interrupt(); + try { + q.put(ninetynine); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + + pleaseInterrupt.countDown(); + try { + q.put(ninetynine); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + }}); + + await(pleaseInterrupt); + if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); + t.interrupt(); + awaitTermination(t); + mustEqual(SIZE, q.size()); + mustEqual(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(); - } + * put blocks interruptibly waiting for take when full + */ + public void testPutWithTake() throws InterruptedException { + 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(itemFor(i)); + pleaseTake.countDown(); + q.put(eightysix); + + Thread.currentThread().interrupt(); + try { + q.put(ninetynine); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + + pleaseInterrupt.countDown(); + try { + q.put(ninetynine); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + }}); + + await(pleaseTake); + mustEqual(0, q.remainingCapacity()); + mustEqual(0, q.take()); + + await(pleaseInterrupt); + if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); + t.interrupt(); + awaitTermination(t); + mustEqual(0, q.remainingCapacity()); } /** * timed offer times out if full and elements not taken */ public void testTimedOffer() { - 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){} - } - }); - - try { - t.start(); - Thread.sleep(SMALL_DELAY_MS); - t.interrupt(); - t.join(); - } catch (Exception e){ - unexpectedException(); - } + final LinkedBlockingDeque q = new LinkedBlockingDeque<>(2); + final CountDownLatch pleaseInterrupt = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + q.put(zero); + q.put(one); + long startTime = System.nanoTime(); + + assertFalse(q.offer(two, timeoutMillis(), MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); + + Thread.currentThread().interrupt(); + try { + q.offer(three, randomTimeout(), randomTimeUnit()); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + + pleaseInterrupt.countDown(); + try { + q.offer(four, LONGER_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + }}); + + await(pleaseInterrupt); + if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); + t.interrupt(); + awaitTermination(t); } /** * 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(); - } - } - - /** - * take blocks interruptibly when empty - */ - public void testTakeFromEmpty() { - 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(); + public void testTake() throws InterruptedException { + LinkedBlockingDeque q = populatedDeque(SIZE); + for (int i = 0; i < SIZE; ++i) { + mustEqual(i, q.take()); } } /** - * 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(); - } - } + * take removes existing elements until empty, then blocks interruptibly + */ + public void testBlockingTake() throws InterruptedException { + final LinkedBlockingDeque q = populatedDeque(SIZE); + final CountDownLatch pleaseInterrupt = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + for (int i = 0; i < SIZE; i++) mustEqual(i, q.take()); + + 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); + if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); + t.interrupt(); + awaitTermination(t); + } /** * poll succeeds unless empty */ public void testPoll() { - LinkedBlockingDeque q = populatedDeque(SIZE); + LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.poll()).intValue()); + mustEqual(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) { + mustEqual(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) { + long startTime = System.nanoTime(); + mustEqual(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); } /** * 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(); - } - } + public void testInterruptedTimedPoll() throws InterruptedException { + final BlockingQueue q = populatedDeque(SIZE); + final CountDownLatch pleaseInterrupt = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + for (int i = 0; i < SIZE; i++) + mustEqual(i, q.poll(LONG_DELAY_MS, MILLISECONDS)); - /** - * 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) { } - } - }); - 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(); - } - } + Thread.currentThread().interrupt(); + try { + q.poll(randomTimeout(), randomTimeUnit()); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + pleaseInterrupt.countDown(); + try { + q.poll(LONGER_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + }}); + + await(pleaseInterrupt); + if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); + t.interrupt(); + awaitTermination(t); + checkEmpty(q); + } /** * putFirst(null) throws NPE */ - public void testPutFirstNull() { - try { - LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); + public void testPutFirstNull() throws InterruptedException { + LinkedBlockingDeque q = new LinkedBlockingDeque<>(SIZE); + try { 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()); - } - catch (InterruptedException ie) { - unexpectedException(); + public void testPutFirst() throws InterruptedException { + LinkedBlockingDeque q = new LinkedBlockingDeque<>(SIZE); + for (int i = 0; i < SIZE; ++i) { + Item x = itemFor(i); + q.putFirst(x); + mustContain(q, x); } + mustEqual(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); + 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(itemFor(i)); + mustEqual(SIZE, q.size()); + mustEqual(0, q.remainingCapacity()); + + Thread.currentThread().interrupt(); + try { + q.putFirst(ninetynine); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + + pleaseInterrupt.countDown(); + try { + q.putFirst(ninetynine); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + }}); + + await(pleaseInterrupt); + if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); + t.interrupt(); + awaitTermination(t); + mustEqual(SIZE, q.size()); + mustEqual(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(); - } + * putFirst blocks interruptibly waiting for take when full + */ + public void testPutFirstWithTake() throws InterruptedException { + 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(itemFor(i)); + pleaseTake.countDown(); + q.putFirst(eightysix); + + pleaseInterrupt.countDown(); + try { + q.putFirst(ninetynine); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + }}); + + await(pleaseTake); + mustEqual(0, q.remainingCapacity()); + mustEqual(capacity - 1, q.take()); + + await(pleaseInterrupt); + if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); + t.interrupt(); + awaitTermination(t); + mustEqual(0, q.remainingCapacity()); } /** * timed offerFirst times out if full and elements not taken */ public void testTimedOfferFirst() { - 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){} - } - }); - - try { - t.start(); - Thread.sleep(SMALL_DELAY_MS); - t.interrupt(); - t.join(); - } catch (Exception e){ - unexpectedException(); - } + final LinkedBlockingDeque q = new LinkedBlockingDeque<>(2); + final CountDownLatch pleaseInterrupt = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + q.putFirst(zero); + q.putFirst(one); + long startTime = System.nanoTime(); + + assertFalse(q.offerFirst(two, timeoutMillis(), MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); + + Thread.currentThread().interrupt(); + try { + q.offerFirst(three, randomTimeout(), randomTimeUnit()); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + + pleaseInterrupt.countDown(); + try { + q.offerFirst(four, LONGER_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + }}); + + await(pleaseInterrupt); + if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); + t.interrupt(); + awaitTermination(t); } /** * 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) { + mustEqual(i, q.takeFirst()); + } } /** - * takeFirst blocks interruptibly when empty - */ - public void testTakeFirstFromEmpty() { - 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(); - } + * takeFirst() blocks interruptibly when empty + */ + 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()); + }}); + + await(threadStarted); + if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); + t.interrupt(); + awaitTermination(t); } /** - * 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(); - } + * 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); + if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); + t.interrupt(); + awaitTermination(t); + } + + /** + * 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 { + final LinkedBlockingDeque q = populatedDeque(SIZE); + final CountDownLatch pleaseInterrupt = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + for (int i = 0; i < SIZE; i++) mustEqual(i, q.takeFirst()); + + 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); + if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); + t.interrupt(); + awaitTermination(t); + } /** * 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) { + mustEqual(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) { + long startTime = System.nanoTime(); + mustEqual(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); } /** * 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 { + final LinkedBlockingDeque q = populatedDeque(SIZE); + final CountDownLatch pleaseInterrupt = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + for (int i = 0; i < SIZE; i++) + mustEqual(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS)); + + Thread.currentThread().interrupt(); + try { + q.pollFirst(randomTimeout(), randomTimeUnit()); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + + pleaseInterrupt.countDown(); + try { + q.pollFirst(LONGER_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + }}); + + await(pleaseInterrupt); + if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); + t.interrupt(); + awaitTermination(t); } /** - * timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds; - * on interruption throws - */ - public void testTimedPollFirstWithOfferFirst() { - 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(); - } + * timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds; + * on interruption throws + */ + public void testTimedPollFirstWithOfferFirst() throws InterruptedException { + final LinkedBlockingDeque q = new LinkedBlockingDeque<>(2); + final CheckedBarrier barrier = new CheckedBarrier(2); + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + 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(randomTimeout(), randomTimeUnit()); + shouldThrow(); + } catch (InterruptedException success) {} + + barrier.await(); + try { + q.pollFirst(LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); + }}); + + barrier.await(); + long startTime = System.nanoTime(); + assertTrue(q.offerFirst(zero, LONG_DELAY_MS, MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); + barrier.await(); + if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); + t.interrupt(); + awaitTermination(t); } /** * putLast(null) throws NPE */ - public void testPutLastNull() { - try { - LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); + public void testPutLastNull() throws InterruptedException { + LinkedBlockingDeque q = new LinkedBlockingDeque<>(SIZE); + try { 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()); - } - catch (InterruptedException ie) { - unexpectedException(); + public void testPutLast() throws InterruptedException { + LinkedBlockingDeque q = new LinkedBlockingDeque<>(SIZE); + for (int i = 0; i < SIZE; ++i) { + Item x = itemFor(i); + q.putLast(x); + mustContain(q, x); } + mustEqual(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); + 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(itemFor(i)); + mustEqual(SIZE, q.size()); + mustEqual(0, q.remainingCapacity()); + + Thread.currentThread().interrupt(); + try { + q.putLast(ninetynine); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + + pleaseInterrupt.countDown(); + try { + q.putLast(ninetynine); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + }}); + + await(pleaseInterrupt); + if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); + t.interrupt(); + awaitTermination(t); + mustEqual(SIZE, q.size()); + mustEqual(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(); - } + * putLast blocks interruptibly waiting for take when full + */ + public void testPutLastWithTake() throws InterruptedException { + 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(itemFor(i)); + pleaseTake.countDown(); + q.putLast(eightysix); + + Thread.currentThread().interrupt(); + try { + q.putLast(ninetynine); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + + pleaseInterrupt.countDown(); + try { + q.putLast(ninetynine); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + }}); + + await(pleaseTake); + mustEqual(0, q.remainingCapacity()); + mustEqual(0, q.take()); + + await(pleaseInterrupt); + if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); + t.interrupt(); + awaitTermination(t); + mustEqual(0, q.remainingCapacity()); } /** * timed offerLast times out if full and elements not taken */ public void testTimedOfferLast() { - 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){} - } - }); - - try { - t.start(); - Thread.sleep(SMALL_DELAY_MS); - t.interrupt(); - t.join(); - } catch (Exception e){ - unexpectedException(); - } + final LinkedBlockingDeque q = new LinkedBlockingDeque<>(2); + final CountDownLatch pleaseInterrupt = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + q.putLast(zero); + q.putLast(one); + long startTime = System.nanoTime(); + + assertFalse(q.offerLast(two, timeoutMillis(), MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); + + Thread.currentThread().interrupt(); + try { + q.offerLast(three, randomTimeout(), randomTimeUnit()); + shouldThrow(); + } catch (InterruptedException success) {} + + pleaseInterrupt.countDown(); + try { + q.offerLast(four, LONGER_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + }}); + + await(pleaseInterrupt); + if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); + t.interrupt(); + awaitTermination(t); } /** * 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(); - } - } - - /** - * takeLast blocks interruptibly when empty - */ - public void testTakeLastFromEmpty() { - 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(); + public void testTakeLast() throws InterruptedException { + LinkedBlockingDeque q = populatedDeque(SIZE); + for (int i = 0; i < SIZE; ++i) { + mustEqual(SIZE - i - 1, q.takeLast()); } } /** - * 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(); - } - } + * takeLast removes existing elements until empty, then blocks interruptibly + */ + public void testBlockingTakeLast() throws InterruptedException { + final LinkedBlockingDeque q = populatedDeque(SIZE); + final CountDownLatch pleaseInterrupt = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + for (int i = 0; i < SIZE; i++) + mustEqual(SIZE - i - 1, q.takeLast()); + + 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); + if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); + t.interrupt(); + awaitTermination(t); + } /** * 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) { + mustEqual(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) { + long startTime = System.nanoTime(); + mustEqual(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); } /** * 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){ - } - }}); - t.start(); - try { - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); - } - catch (InterruptedException ie) { - unexpectedException(); - } + public void testInterruptedTimedPollLast() throws InterruptedException { + final LinkedBlockingDeque q = populatedDeque(SIZE); + final CountDownLatch pleaseInterrupt = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + for (int i = 0; i < SIZE; i++) + mustEqual(SIZE - i - 1, + q.pollLast(LONG_DELAY_MS, MILLISECONDS)); + + Thread.currentThread().interrupt(); + try { + q.pollLast(randomTimeout(), randomTimeUnit()); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + + pleaseInterrupt.countDown(); + try { + q.pollLast(LONGER_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + }}); + + await(pleaseInterrupt); + if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); + t.interrupt(); + awaitTermination(t); + checkEmpty(q); } /** - * timed poll before a delayed offerLast fails; after offerLast succeeds; - * on interruption throws - */ - public void testTimedPollWithOfferLast() { - 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(); - } - } + * timed poll before a delayed offerLast fails; after offerLast succeeds; + * on interruption throws + */ + public void testTimedPollWithOfferLast() throws InterruptedException { + final LinkedBlockingDeque q = new LinkedBlockingDeque<>(2); + final CheckedBarrier barrier = new CheckedBarrier(2); + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + 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(randomTimeout(), randomTimeUnit()); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + barrier.await(); + try { + q.poll(LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); + }}); + + barrier.await(); + long startTime = System.nanoTime(); + assertTrue(q.offerLast(zero, LONG_DELAY_MS, MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); + + barrier.await(); + if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); + t.interrupt(); + awaitTermination(t); + } /** * element returns next element, or throws NSEE if empty */ public void testElement() { - LinkedBlockingDeque q = populatedDeque(SIZE); + LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.element()).intValue()); + mustEqual(i, q.element()); q.poll(); } try { q.element(); shouldThrow(); - } - catch (NoSuchElementException success) {} - } - - /** - * remove(x) removes x and returns true if present - */ - public void testRemoveElement() { - LinkedBlockingDeque q = populatedDeque(SIZE); - for (int i = 1; i < SIZE; i+=2) { - assertTrue(q.remove(new Integer(i))); - } - for (int i = 0; i < SIZE; i+=2) { - assertTrue(q.remove(new Integer(i))); - assertFalse(q.remove(new Integer(i+1))); - } - assertTrue(q.isEmpty()); + } catch (NoSuchElementException success) {} } /** * contains(x) reports true when elements added but not yet removed */ public void testContains() { - LinkedBlockingDeque q = populatedDeque(SIZE); + LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - assertTrue(q.contains(new Integer(i))); + mustContain(q, i); q.poll(); - assertFalse(q.contains(new Integer(i))); + mustNotContain(q, i); } } @@ -1441,14 +1462,14 @@ public class LinkedBlockingDequeTest ext * clear removes all elements */ public void testClear() { - LinkedBlockingDeque q = populatedDeque(SIZE); + LinkedBlockingDeque q = populatedDeque(SIZE); q.clear(); assertTrue(q.isEmpty()); - assertEquals(0, q.size()); - assertEquals(SIZE, q.remainingCapacity()); + mustEqual(0, q.size()); + mustEqual(SIZE, q.remainingCapacity()); q.add(one); assertFalse(q.isEmpty()); - assertTrue(q.contains(one)); + mustContain(q, one); q.clear(); assertTrue(q.isEmpty()); } @@ -1457,12 +1478,12 @@ public class LinkedBlockingDequeTest ext * containsAll(c) is true when c contains a subset of elements */ public void testContainsAll() { - LinkedBlockingDeque q = populatedDeque(SIZE); - LinkedBlockingDeque p = new LinkedBlockingDeque(SIZE); + LinkedBlockingDeque q = populatedDeque(SIZE); + LinkedBlockingDeque p = new LinkedBlockingDeque<>(SIZE); for (int i = 0; i < SIZE; ++i) { assertTrue(q.containsAll(p)); assertFalse(p.containsAll(q)); - p.add(new Integer(i)); + mustAdd(p, i); } assertTrue(p.containsAll(q)); } @@ -1471,8 +1492,8 @@ public class LinkedBlockingDequeTest ext * retainAll(c) retains only those elements of c and reports true if changed */ public void testRetainAll() { - LinkedBlockingDeque q = populatedDeque(SIZE); - LinkedBlockingDeque p = populatedDeque(SIZE); + LinkedBlockingDeque q = populatedDeque(SIZE); + LinkedBlockingDeque p = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { boolean changed = q.retainAll(p); if (i == 0) @@ -1481,7 +1502,7 @@ public class LinkedBlockingDequeTest ext assertTrue(changed); assertTrue(q.containsAll(p)); - assertEquals(SIZE-i, q.size()); + mustEqual(SIZE - i, q.size()); p.remove(); } } @@ -1491,177 +1512,165 @@ public class LinkedBlockingDequeTest ext */ public void testRemoveAll() { for (int i = 1; i < SIZE; ++i) { - LinkedBlockingDeque q = populatedDeque(SIZE); - LinkedBlockingDeque p = populatedDeque(i); + LinkedBlockingDeque q = populatedDeque(SIZE); + LinkedBlockingDeque p = populatedDeque(i); assertTrue(q.removeAll(p)); - assertEquals(SIZE-i, q.size()); + mustEqual(SIZE - i, q.size()); for (int j = 0; j < i; ++j) { - Integer I = (Integer)(p.remove()); - assertFalse(q.contains(I)); + mustNotContain(q, p.remove()); } } } /** - * toArray contains all elements + * toArray contains all elements in FIFO order */ - public void testToArray() { - 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(); - } + public void testToArray() throws InterruptedException { + LinkedBlockingDeque q = populatedDeque(SIZE); + Object[] a = q.toArray(); + assertSame(Object[].class, a.getClass()); + for (Object o : a) + assertSame(o, q.poll()); + assertTrue(q.isEmpty()); } /** - * toArray(a) contains all elements + * toArray(a) contains all elements in FIFO order */ public void testToArray2() { - 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(); - } + LinkedBlockingDeque q = populatedDeque(SIZE); + Item[] items = new Item[SIZE]; + Item[] array = q.toArray(items); + assertSame(items, array); + for (Item o : items) + assertSame(o, q.remove()); + assertTrue(q.isEmpty()); } /** - * toArray(null) throws NPE + * toArray(incompatible array type) throws ArrayStoreException */ - public void testToArray_BadArg() { - try { - LinkedBlockingDeque q = populatedDeque(SIZE); - Object o[] = q.toArray(null); - shouldThrow(); - } catch(NullPointerException success){} + public void testToArray1_BadArg() { + LinkedBlockingDeque q = populatedDeque(SIZE); + try { + q.toArray(new String[10]); + shouldThrow(); + } catch (ArrayStoreException success) {} } /** - * toArray with incompatible array type throws CCE + * iterator iterates through all elements */ - public void testToArray1_BadArg() { - try { - LinkedBlockingDeque q = populatedDeque(SIZE); - Object o[] = q.toArray(new String[10] ); - shouldThrow(); - } catch(ArrayStoreException success){} - } + public void testIterator() throws InterruptedException { + LinkedBlockingDeque q = populatedDeque(SIZE); + Iterator it = q.iterator(); + int i; + for (i = 0; it.hasNext(); i++) + mustContain(q, it.next()); + mustEqual(i, SIZE); + assertIteratorExhausted(it); + it = q.iterator(); + for (i = 0; it.hasNext(); i++) + mustEqual(it.next(), q.take()); + mustEqual(i, SIZE); + assertIteratorExhausted(it); + } /** - * iterator iterates through all elements + * iterator of empty collection has no elements */ - public void testIterator() { - LinkedBlockingDeque q = populatedDeque(SIZE); - Iterator it = q.iterator(); - try { - while(it.hasNext()){ - assertEquals(it.next(), q.take()); - } - } catch (InterruptedException e){ - unexpectedException(); - } + public void testEmptyIterator() { + Deque c = new LinkedBlockingDeque<>(); + assertIteratorExhausted(c.iterator()); + assertIteratorExhausted(c.descendingIterator()); } /** * iterator.remove removes current element */ - public void testIteratorRemove () { - final LinkedBlockingDeque q = new LinkedBlockingDeque(3); + public void testIteratorRemove() { + final LinkedBlockingDeque q = new LinkedBlockingDeque<>(3); q.add(two); q.add(one); q.add(three); - Iterator it = q.iterator(); + Iterator it = q.iterator(); it.next(); 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 */ public void testIteratorOrdering() { - final LinkedBlockingDeque q = new LinkedBlockingDeque(3); + final LinkedBlockingDeque q = new LinkedBlockingDeque<>(3); q.add(one); q.add(two); q.add(three); - assertEquals(0, q.remainingCapacity()); + mustEqual(0, q.remainingCapacity()); int k = 0; - for (Iterator it = q.iterator(); it.hasNext();) { - int i = ((Integer)(it.next())).intValue(); - assertEquals(++k, i); + for (Iterator it = q.iterator(); it.hasNext();) { + mustEqual(++k, it.next()); } - assertEquals(3, k); + mustEqual(3, k); } /** * Modifications do not cause iterators to fail */ - public void testWeaklyConsistentIteration () { - final LinkedBlockingDeque q = new LinkedBlockingDeque(3); + 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()); + mustEqual(0, q.size()); } - /** - * Descending iterator iterates through all elements + * Descending iterator iterates through all elements */ public void testDescendingIterator() { - LinkedBlockingDeque q = populatedDeque(SIZE); + LinkedBlockingDeque q = populatedDeque(SIZE); int i = 0; - Iterator it = q.descendingIterator(); - while(it.hasNext()) { - assertTrue(q.contains(it.next())); + Iterator it = q.descendingIterator(); + while (it.hasNext()) { + mustContain(q, it.next()); ++i; } - assertEquals(i, SIZE); + mustEqual(i, SIZE); assertFalse(it.hasNext()); try { it.next(); - } catch(NoSuchElementException success) { - } + shouldThrow(); + } catch (NoSuchElementException success) {} } /** - * Descending iterator ordering is reverse FIFO + * Descending iterator ordering is reverse FIFO */ public void testDescendingIteratorOrdering() { - final LinkedBlockingDeque q = new LinkedBlockingDeque(); + final LinkedBlockingDeque q = new LinkedBlockingDeque<>(); for (int iters = 0; iters < 100; ++iters) { - q.add(new Integer(3)); - q.add(new Integer(2)); - q.add(new Integer(1)); + mustAdd(q, three); + mustAdd(q, two); + mustAdd(q, one); + int k = 0; - for (Iterator it = q.descendingIterator(); it.hasNext();) { - int i = ((Integer)(it.next())).intValue(); - assertEquals(++k, i); + for (Iterator it = q.descendingIterator(); it.hasNext();) { + mustEqual(++k, it.next()); } - assertEquals(3, k); + mustEqual(3, k); q.remove(); q.remove(); q.remove(); @@ -1671,247 +1680,181 @@ public class LinkedBlockingDequeTest ext /** * descendingIterator.remove removes current element */ - public void testDescendingIteratorRemove () { - final LinkedBlockingDeque q = new LinkedBlockingDeque(); + public void testDescendingIteratorRemove() { + final LinkedBlockingDeque q = new LinkedBlockingDeque<>(); for (int iters = 0; iters < 100; ++iters) { - q.add(new Integer(3)); - q.add(new Integer(2)); - q.add(new Integer(1)); - Iterator it = q.descendingIterator(); - assertEquals(it.next(), new Integer(1)); + mustAdd(q, three); + mustAdd(q, two); + mustAdd(q, one); + Iterator it = q.descendingIterator(); + mustEqual(it.next(), one); it.remove(); - assertEquals(it.next(), new Integer(2)); + mustEqual(it.next(), two); it = q.descendingIterator(); - assertEquals(it.next(), new Integer(2)); - assertEquals(it.next(), new Integer(3)); + mustEqual(it.next(), two); + mustEqual(it.next(), three); it.remove(); assertFalse(it.hasNext()); q.remove(); } } - /** * toString contains toStrings of elements */ public void testToString() { - LinkedBlockingDeque q = populatedDeque(SIZE); + 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 */ public void testOfferInExecutor() { - final LinkedBlockingDeque q = new LinkedBlockingDeque(2); + final LinkedBlockingDeque q = new LinkedBlockingDeque<>(2); 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(); - } - } - }); + final CheckedBarrier threadsStarted = new CheckedBarrier(2); + final ExecutorService executor = Executors.newFixedThreadPool(2); + try (PoolCleaner cleaner = cleaner(executor)) { + executor.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { + assertFalse(q.offer(three)); + threadsStarted.await(); + assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); + mustEqual(0, q.remainingCapacity()); + }}); - joinPool(executor); + executor.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { + threadsStarted.await(); + assertSame(one, q.take()); + }}); + } } /** - * poll retrieves elements across Executor threads + * timed poll retrieves elements across Executor threads */ 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(); - } - } - }); + final LinkedBlockingDeque q = new LinkedBlockingDeque<>(2); + final CheckedBarrier threadsStarted = new CheckedBarrier(2); + final ExecutorService executor = Executors.newFixedThreadPool(2); + try (PoolCleaner cleaner = cleaner(executor)) { + executor.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { + assertNull(q.poll()); + threadsStarted.await(); + assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); + checkEmpty(q); + }}); - executor.execute(new Runnable() { - public void run() { - try { - Thread.sleep(SMALL_DELAY_MS); + executor.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { + threadsStarted.await(); q.put(one); - } - catch (InterruptedException e) { - threadUnexpectedException(); - } - } - }); - - joinPool(executor); - } - - /** - * A deserialized serialized deque has same elements in same order - */ - public void testSerialization() { - 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(); - } - } - - /** - * drainTo(null) throws NPE - */ - public void testDrainToNull() { - LinkedBlockingDeque q = populatedDeque(SIZE); - try { - q.drainTo(null); - shouldThrow(); - } catch(NullPointerException success) { + }}); } } /** - * drainTo(this) throws IAE + * A deserialized/reserialized deque has same elements in same order */ - public void testDrainToSelf() { - LinkedBlockingDeque q = populatedDeque(SIZE); - try { - q.drainTo(q); - shouldThrow(); - } catch(IllegalArgumentException success) { + public void testSerialization() throws Exception { + Queue x = populatedDeque(SIZE); + Queue y = serialClone(x); + + assertNotSame(y, x); + mustEqual(x.size(), y.size()); + mustEqual(x.toString(), y.toString()); + assertTrue(Arrays.equals(x.toArray(), y.toArray())); + while (!x.isEmpty()) { + assertFalse(y.isEmpty()); + mustEqual(x.remove(), y.remove()); } + assertTrue(y.isEmpty()); } /** * drainTo(c) empties deque into another collection c */ public void testDrainTo() { - LinkedBlockingDeque q = populatedDeque(SIZE); - ArrayList l = new ArrayList(); + LinkedBlockingDeque q = populatedDeque(SIZE); + ArrayList l = new ArrayList<>(); q.drainTo(l); - assertEquals(q.size(), 0); - assertEquals(l.size(), SIZE); + mustEqual(0, q.size()); + mustEqual(SIZE, l.size()); for (int i = 0; i < SIZE; ++i) - assertEquals(l.get(i), new Integer(i)); + mustEqual(l.get(i), i); q.add(zero); q.add(one); assertFalse(q.isEmpty()); - assertTrue(q.contains(zero)); - assertTrue(q.contains(one)); + mustContain(q, zero); + mustContain(q, one); l.clear(); q.drainTo(l); - assertEquals(q.size(), 0); - assertEquals(l.size(), 2); + mustEqual(0, q.size()); + mustEqual(2, l.size()); for (int i = 0; i < 2; ++i) - assertEquals(l.get(i), new Integer(i)); + mustEqual(l.get(i), i); } /** * drainTo empties full deque, unblocking a waiting put. */ - public void testDrainToWithActivePut() { - 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(); - } - } + public void testDrainToWithActivePut() throws InterruptedException { + final LinkedBlockingDeque q = populatedDeque(SIZE); + Thread t = new Thread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + q.put(new Item(SIZE + 1)); + }}); - /** - * drainTo(null, n) throws NPE - */ - public void testDrainToNullN() { - LinkedBlockingDeque q = populatedDeque(SIZE); - try { - q.drainTo(null, 0); - shouldThrow(); - } catch(NullPointerException success) { - } + t.start(); + ArrayList l = new ArrayList<>(); + q.drainTo(l); + assertTrue(l.size() >= SIZE); + for (int i = 0; i < SIZE; ++i) + mustEqual(l.get(i), i); + t.join(); + assertTrue(q.size() + l.size() >= SIZE); } /** - * drainTo(this, n) throws IAE + * drainTo(c, n) empties first min(n, size) elements of queue into c */ - public void testDrainToSelfN() { - LinkedBlockingDeque q = populatedDeque(SIZE); - try { - q.drainTo(q, 0); - shouldThrow(); - } catch(IllegalArgumentException success) { + public void testDrainToN() { + LinkedBlockingDeque q = new LinkedBlockingDeque<>(); + for (int i = 0; i < SIZE + 2; ++i) { + for (int j = 0; j < SIZE; j++) + mustOffer(q, j); + ArrayList l = new ArrayList<>(); + q.drainTo(l, i); + int k = (i < SIZE) ? i : SIZE; + mustEqual(k, l.size()); + mustEqual(SIZE - k, q.size()); + for (int j = 0; j < k; ++j) + mustEqual(l.get(j), j); + do {} while (q.poll() != null); } } /** - * drainTo(c, n) empties first max {n, size} elements of deque into c + * remove(null), contains(null) always return false */ - public void testDrainToN() { - LinkedBlockingDeque q = new LinkedBlockingDeque(); - for (int i = 0; i < SIZE + 2; ++i) { - for(int j = 0; j < SIZE; j++) - assertTrue(q.offer(new Integer(j))); - ArrayList l = new ArrayList(); - q.drainTo(l, i); - int k = (i < SIZE)? i : SIZE; - assertEquals(l.size(), k); - assertEquals(q.size(), SIZE-k); - for (int j = 0; j < k; ++j) - assertEquals(l.get(j), new Integer(j)); - while (q.poll() != null) ; + public void testNeverContainsNull() { + Deque[] qs = { + new LinkedBlockingDeque<>(), + populatedDeque(2), + }; + + for (Deque q : qs) { + assertFalse(q.contains(null)); + assertFalse(q.remove(null)); + assertFalse(q.removeFirstOccurrence(null)); + assertFalse(q.removeLastOccurrence(null)); } }