--- jsr166/src/test/tck/PriorityBlockingQueueTest.java 2014/12/31 20:17:39 1.57 +++ jsr166/src/test/tck/PriorityBlockingQueueTest.java 2019/09/05 21:11:13 1.84 @@ -20,6 +20,7 @@ import java.util.concurrent.CountDownLat import java.util.concurrent.Executors; import java.util.concurrent.ExecutorService; import java.util.concurrent.PriorityBlockingQueue; +import java.util.concurrent.ThreadLocalRandom; import junit.framework.Test; @@ -33,24 +34,48 @@ public class PriorityBlockingQueueTest e public static class InitialCapacity extends BlockingQueueTest { protected BlockingQueue emptyCollection() { - return new PriorityBlockingQueue(SIZE); + ThreadLocalRandom rnd = ThreadLocalRandom.current(); + int initialCapacity = rnd.nextInt(1, SIZE); + return new PriorityBlockingQueue(initialCapacity); } } public static void main(String[] args) { - junit.textui.TestRunner.run(suite()); + main(suite(), args); } public static Test suite() { - return newTestSuite(PriorityBlockingQueueTest.class, - new Generic().testSuite(), - new InitialCapacity().testSuite()); + class Implementation implements CollectionImplementation { + public Class klazz() { return PriorityBlockingQueue.class; } + public Collection emptyCollection() { + return new PriorityBlockingQueue(); + } + public Object makeElement(int i) { return i; } + public boolean isConcurrent() { return true; } + public boolean permitsNulls() { return false; } + } + class ComparatorImplementation implements CollectionImplementation { + public Class klazz() { return PriorityBlockingQueue.class; } + public Collection emptyCollection() { + ThreadLocalRandom rnd = ThreadLocalRandom.current(); + int initialCapacity = rnd.nextInt(1, 10); + return new PriorityBlockingQueue( + initialCapacity, new MyReverseComparator()); + } + public Object makeElement(int i) { return i; } + public boolean isConcurrent() { return true; } + public boolean permitsNulls() { return false; } + } + return newTestSuite( + PriorityBlockingQueueTest.class, + new Generic().testSuite(), + new InitialCapacity().testSuite(), + CollectionTest.testSuite(new Implementation()), + CollectionTest.testSuite(new ComparatorImplementation())); } - private static final int NOCAP = Integer.MAX_VALUE; - /** Sample Comparator */ - static class MyReverseComparator implements Comparator { + static class MyReverseComparator implements Comparator, java.io.Serializable { public int compare(Object x, Object y) { return ((Comparable)y).compareTo(x); } @@ -58,19 +83,20 @@ public class PriorityBlockingQueueTest e /** * Returns a new queue of given size containing consecutive - * Integers 0 ... n. + * Integers 0 ... n - 1. */ - private PriorityBlockingQueue populatedQueue(int n) { + private static PriorityBlockingQueue populatedQueue(int n) { PriorityBlockingQueue q = new PriorityBlockingQueue(n); assertTrue(q.isEmpty()); - for (int i = n-1; i >= 0; i -= 2) + for (int i = n - 1; i >= 0; i -= 2) assertTrue(q.offer(new Integer(i))); for (int i = (n & 1); i < n; i += 2) assertTrue(q.offer(new Integer(i))); assertFalse(q.isEmpty()); - assertEquals(NOCAP, q.remainingCapacity()); + assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); assertEquals(n, q.size()); + assertEquals((Integer) 0, q.peek()); return q; } @@ -78,11 +104,12 @@ public class PriorityBlockingQueueTest e * A new queue has unbounded capacity */ public void testConstructor1() { - assertEquals(NOCAP, new PriorityBlockingQueue(SIZE).remainingCapacity()); + assertEquals(Integer.MAX_VALUE, + new PriorityBlockingQueue(SIZE).remainingCapacity()); } /** - * Constructor throws IAE if capacity argument nonpositive + * Constructor throws IllegalArgumentException if capacity argument nonpositive */ public void testConstructor2() { try { @@ -117,7 +144,7 @@ public class PriorityBlockingQueueTest e */ public void testConstructor5() { Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE-1; ++i) + for (int i = 0; i < SIZE - 1; ++i) ints[i] = i; Collection elements = Arrays.asList(ints); try { @@ -149,7 +176,7 @@ public class PriorityBlockingQueueTest e for (int i = 0; i < SIZE; ++i) ints[i] = new Integer(i); q.addAll(Arrays.asList(ints)); - for (int i = SIZE-1; i >= 0; --i) + for (int i = SIZE - 1; i >= 0; --i) assertEquals(ints[i], q.poll()); } @@ -159,7 +186,7 @@ public class PriorityBlockingQueueTest e public void testEmpty() { PriorityBlockingQueue q = new PriorityBlockingQueue(2); assertTrue(q.isEmpty()); - assertEquals(NOCAP, q.remainingCapacity()); + assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); q.add(one); assertFalse(q.isEmpty()); q.add(two); @@ -169,20 +196,19 @@ public class PriorityBlockingQueueTest e } /** - * remainingCapacity does not change when elements added or removed, - * but size does + * remainingCapacity() always returns Integer.MAX_VALUE */ public void testRemainingCapacity() { - PriorityBlockingQueue q = populatedQueue(SIZE); + BlockingQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(NOCAP, q.remainingCapacity()); - assertEquals(SIZE-i, q.size()); - q.remove(); + assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); + assertEquals(SIZE - i, q.size()); + assertEquals(i, q.remove()); } for (int i = 0; i < SIZE; ++i) { - assertEquals(NOCAP, q.remainingCapacity()); + assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); assertEquals(i, q.size()); - q.add(new Integer(i)); + assertTrue(q.add(i)); } } @@ -199,13 +225,15 @@ public class PriorityBlockingQueueTest e * Offer of non-Comparable throws CCE */ public void testOfferNonComparable() { + PriorityBlockingQueue q = new PriorityBlockingQueue(1); try { - PriorityBlockingQueue q = new PriorityBlockingQueue(1); - q.offer(new Object()); - q.offer(new Object()); q.offer(new Object()); shouldThrow(); - } catch (ClassCastException success) {} + } catch (ClassCastException success) { + assertTrue(q.isEmpty()); + assertEquals(0, q.size()); + assertNull(q.poll()); + } } /** @@ -220,11 +248,11 @@ public class PriorityBlockingQueueTest e } /** - * addAll(this) throws IAE + * addAll(this) throws IllegalArgumentException */ public void testAddAllSelf() { + PriorityBlockingQueue q = populatedQueue(SIZE); try { - PriorityBlockingQueue q = populatedQueue(SIZE); q.addAll(q); shouldThrow(); } catch (IllegalArgumentException success) {} @@ -235,11 +263,11 @@ public class PriorityBlockingQueueTest e * possibly adding some elements */ public void testAddAll3() { + PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); + Integer[] ints = new Integer[SIZE]; + for (int i = 0; i < SIZE - 1; ++i) + ints[i] = new Integer(i); try { - PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); - Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE-1; ++i) - ints[i] = new Integer(i); q.addAll(Arrays.asList(ints)); shouldThrow(); } catch (NullPointerException success) {} @@ -251,7 +279,7 @@ public class PriorityBlockingQueueTest e public void testAddAll5() { Integer[] empty = new Integer[0]; Integer[] ints = new Integer[SIZE]; - for (int i = SIZE-1; i >= 0; --i) + for (int i = SIZE - 1; i >= 0; --i) ints[i] = new Integer(i); PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); assertFalse(q.addAll(Arrays.asList(empty))); @@ -291,9 +319,9 @@ public class PriorityBlockingQueueTest e } /** - * timed offer does not time out + * Queue is unbounded, so timed offer never times out */ - public void testTimedOffer() throws InterruptedException { + public void testTimedOffer() { final PriorityBlockingQueue q = new PriorityBlockingQueue(2); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() { @@ -324,9 +352,7 @@ public class PriorityBlockingQueueTest e final CountDownLatch pleaseInterrupt = new CountDownLatch(1); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { - for (int i = 0; i < SIZE; ++i) { - assertEquals(i, q.take()); - } + for (int i = 0; i < SIZE; i++) assertEquals(i, q.take()); Thread.currentThread().interrupt(); try { @@ -344,7 +370,7 @@ public class PriorityBlockingQueueTest e }}); await(pleaseInterrupt); - assertThreadStaysAlive(t); + if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); t.interrupt(); awaitTermination(t); } @@ -393,28 +419,31 @@ public class PriorityBlockingQueueTest e */ public void testInterruptedTimedPoll() throws InterruptedException { final BlockingQueue q = populatedQueue(SIZE); - final CountDownLatch aboutToWait = new CountDownLatch(1); + final CountDownLatch pleaseInterrupt = new CountDownLatch(1); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { - for (int i = 0; i < SIZE; ++i) { - long t0 = System.nanoTime(); + for (int i = 0; i < SIZE; i++) assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); - assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); - } - long t0 = System.nanoTime(); - aboutToWait.countDown(); + + Thread.currentThread().interrupt(); + try { + q.poll(randomTimeout(), randomTimeUnit()); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + + pleaseInterrupt.countDown(); try { - q.poll(LONG_DELAY_MS, MILLISECONDS); + q.poll(LONGER_DELAY_MS, MILLISECONDS); shouldThrow(); - } catch (InterruptedException success) { - assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); - } + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); }}); - aboutToWait.await(); - waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); + await(pleaseInterrupt); + if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); t.interrupt(); - awaitTermination(t, MEDIUM_DELAY_MS); + awaitTermination(t); } /** @@ -515,7 +544,7 @@ public class PriorityBlockingQueueTest e assertTrue(changed); assertTrue(q.containsAll(p)); - assertEquals(SIZE-i, q.size()); + assertEquals(SIZE - i, q.size()); p.remove(); } } @@ -528,7 +557,7 @@ public class PriorityBlockingQueueTest e PriorityBlockingQueue q = populatedQueue(SIZE); PriorityBlockingQueue p = populatedQueue(i); assertTrue(q.removeAll(p)); - assertEquals(SIZE-i, q.size()); + assertEquals(SIZE - i, q.size()); for (int j = 0; j < i; ++j) { Integer x = (Integer)(p.remove()); assertFalse(q.contains(x)); @@ -541,10 +570,12 @@ public class PriorityBlockingQueueTest e */ public void testToArray() throws InterruptedException { PriorityBlockingQueue q = populatedQueue(SIZE); - Object[] o = q.toArray(); - Arrays.sort(o); - for (int i = 0; i < o.length; i++) - assertSame(o[i], q.take()); + Object[] a = q.toArray(); + assertSame(Object[].class, a.getClass()); + Arrays.sort(a); + for (Object o : a) + assertSame(o, q.take()); + assertTrue(q.isEmpty()); } /** @@ -556,8 +587,9 @@ public class PriorityBlockingQueueTest e Integer[] array = q.toArray(ints); assertSame(ints, array); Arrays.sort(ints); - for (int i = 0; i < ints.length; i++) - assertSame(ints[i], q.take()); + for (Integer o : ints) + assertSame(o, q.take()); + assertTrue(q.isEmpty()); } /** @@ -576,13 +608,19 @@ public class PriorityBlockingQueueTest e */ public void testIterator() { PriorityBlockingQueue q = populatedQueue(SIZE); - int i = 0; Iterator it = q.iterator(); - while (it.hasNext()) { + int i; + for (i = 0; it.hasNext(); i++) assertTrue(q.contains(it.next())); - ++i; - } assertEquals(i, SIZE); + assertIteratorExhausted(it); + } + + /** + * iterator of empty collection has no elements + */ + public void testEmptyIterator() { + assertIteratorExhausted(new PriorityBlockingQueue().iterator()); } /** @@ -621,26 +659,26 @@ public class PriorityBlockingQueueTest e public void testPollInExecutor() { final PriorityBlockingQueue q = new PriorityBlockingQueue(2); final CheckedBarrier threadsStarted = new CheckedBarrier(2); - ExecutorService executor = Executors.newFixedThreadPool(2); - 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 CheckedRunnable() { - public void realRun() throws InterruptedException { - threadsStarted.await(); - q.put(one); - }}); - - joinPool(executor); + 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 CheckedRunnable() { + public void realRun() throws InterruptedException { + threadsStarted.await(); + q.put(one); + }}); + } } /** - * A deserialized serialized queue has same elements + * A deserialized/reserialized queue has same elements */ public void testSerialization() throws Exception { Queue x = populatedQueue(SIZE); @@ -686,7 +724,7 @@ public class PriorityBlockingQueueTest e final PriorityBlockingQueue q = populatedQueue(SIZE); Thread t = new Thread(new CheckedRunnable() { public void realRun() { - q.put(new Integer(SIZE+1)); + q.put(new Integer(SIZE + 1)); }}); t.start(); @@ -703,7 +741,7 @@ public class PriorityBlockingQueueTest e * drainTo(c, n) empties first min(n, size) elements of queue into c */ public void testDrainToN() { - PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE*2); + PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE * 2); for (int i = 0; i < SIZE + 2; ++i) { for (int j = 0; j < SIZE; j++) assertTrue(q.offer(new Integer(j))); @@ -711,7 +749,7 @@ public class PriorityBlockingQueueTest e q.drainTo(l, i); int k = (i < SIZE) ? i : SIZE; assertEquals(k, l.size()); - assertEquals(SIZE-k, q.size()); + assertEquals(SIZE - k, q.size()); for (int j = 0; j < k; ++j) assertEquals(l.get(j), new Integer(j)); do {} while (q.poll() != null);