--- jsr166/src/test/tck/PriorityBlockingQueueTest.java 2015/10/06 00:03:55 1.66 +++ 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,7 +34,9 @@ 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); } } @@ -42,13 +45,37 @@ public class PriorityBlockingQueueTest e } 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())); } /** 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); } @@ -56,9 +83,9 @@ 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()); @@ -69,6 +96,7 @@ public class PriorityBlockingQueueTest e assertFalse(q.isEmpty()); assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); assertEquals(n, q.size()); + assertEquals((Integer) 0, q.peek()); return q; } @@ -81,7 +109,7 @@ public class PriorityBlockingQueueTest e } /** - * Constructor throws IAE if capacity argument nonpositive + * Constructor throws IllegalArgumentException if capacity argument nonpositive */ public void testConstructor2() { try { @@ -200,9 +228,12 @@ public class PriorityBlockingQueueTest e PriorityBlockingQueue q = new PriorityBlockingQueue(1); try { 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()); + } } /** @@ -217,7 +248,7 @@ public class PriorityBlockingQueueTest e } /** - * addAll(this) throws IAE + * addAll(this) throws IllegalArgumentException */ public void testAddAllSelf() { PriorityBlockingQueue q = populatedQueue(SIZE); @@ -288,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() { @@ -321,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 { @@ -341,7 +370,7 @@ public class PriorityBlockingQueueTest e }}); await(pleaseInterrupt); - assertThreadStaysAlive(t); + if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); t.interrupt(); awaitTermination(t); } @@ -390,24 +419,29 @@ 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 { - long startTime = System.nanoTime(); - for (int i = 0; i < SIZE; ++i) { + for (int i = 0; i < SIZE; i++) assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); - } - 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(startTime) < LONG_DELAY_MS); - } + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); }}); - aboutToWait.await(); - waitForThreadToEnterWaitState(t, LONG_DELAY_MS); + await(pleaseInterrupt); + if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); t.interrupt(); awaitTermination(t); } @@ -536,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()); } /** @@ -551,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()); } /** @@ -641,7 +678,7 @@ public class PriorityBlockingQueueTest e } /** - * A deserialized serialized queue has same elements + * A deserialized/reserialized queue has same elements */ public void testSerialization() throws Exception { Queue x = populatedQueue(SIZE);