--- jsr166/src/test/tck/ArrayBlockingQueueTest.java 2016/11/06 03:11:15 1.77 +++ jsr166/src/test/tck/ArrayBlockingQueueTest.java 2019/12/16 21:16:08 1.97 @@ -34,7 +34,7 @@ public class ArrayBlockingQueueTest exte class Implementation implements CollectionImplementation { public Class klazz() { return ArrayBlockingQueue.class; } public Collection emptyCollection() { - boolean fair = ThreadLocalRandom.current().nextBoolean(); + boolean fair = randomBoolean(); return populatedQueue(0, SIZE, 2 * SIZE, fair); } public Object makeElement(int i) { return i; } @@ -103,7 +103,7 @@ public class ArrayBlockingQueueTest exte } /** - * Constructor throws IAE if capacity argument nonpositive + * Constructor throws IllegalArgumentException if capacity argument nonpositive */ public void testConstructor_nonPositiveCapacity() { for (int i : new int[] { 0, -1, Integer.MIN_VALUE }) { @@ -156,7 +156,7 @@ public class ArrayBlockingQueueTest exte } /** - * Initializing from too large collection throws IAE + * Initializing from too large collection throws IllegalArgumentException */ public void testConstructor_collectionTooLarge() { // just barely fits - succeeds @@ -227,22 +227,20 @@ public class ArrayBlockingQueueTest exte } /** - * add succeeds if not full; throws ISE if full + * add succeeds if not full; throws IllegalStateException if full */ public void testAdd() { ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); - for (int i = 0; i < SIZE; ++i) { - assertTrue(q.add(new Integer(i))); - } + for (int i = 0; i < SIZE; i++) assertTrue(q.add((Integer) i)); assertEquals(0, q.remainingCapacity()); try { - q.add(new Integer(SIZE)); + q.add((Integer) SIZE); shouldThrow(); } catch (IllegalStateException success) {} } /** - * addAll(this) throws IAE + * addAll(this) throws IllegalArgumentException */ public void testAddAllSelf() { ArrayBlockingQueue q = populatedQueue(SIZE); @@ -268,15 +266,19 @@ public class ArrayBlockingQueueTest exte } /** - * addAll throws ISE if not enough room + * addAll throws IllegalStateException if not enough room */ - public void testAddAll4() { - ArrayBlockingQueue q = new ArrayBlockingQueue(1); - Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE; ++i) - ints[i] = new Integer(i); + public void testAddAll_insufficientSpace() { + int size = ThreadLocalRandom.current().nextInt(1, SIZE); + ArrayBlockingQueue q = populatedQueue(0, size, size, false); + // Just fits: + q.addAll(populatedQueue(size, size, 2 * size, false)); + assertEquals(0, q.remainingCapacity()); + assertEquals(size, q.size()); + assertEquals(0, q.peek()); try { - q.addAll(Arrays.asList(ints)); + q = populatedQueue(0, size, size, false); + q.addAll(Collections.nCopies(size + 1, 42)); shouldThrow(); } catch (IllegalStateException success) {} } @@ -338,7 +340,7 @@ public class ArrayBlockingQueueTest exte }}); await(pleaseInterrupt); - assertThreadStaysAlive(t); + if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); t.interrupt(); awaitTermination(t); assertEquals(SIZE, q.size()); @@ -360,6 +362,13 @@ public class ArrayBlockingQueueTest exte pleaseTake.countDown(); q.put(86); + Thread.currentThread().interrupt(); + try { + q.put(99); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + pleaseInterrupt.countDown(); try { q.put(99); @@ -373,7 +382,7 @@ public class ArrayBlockingQueueTest exte assertEquals(0, q.take()); await(pleaseInterrupt); - assertThreadStaysAlive(t); + if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); t.interrupt(); awaitTermination(t); assertEquals(0, q.remainingCapacity()); @@ -382,7 +391,7 @@ public class ArrayBlockingQueueTest exte /** * timed offer times out if full and elements not taken */ - public void testTimedOffer() throws InterruptedException { + public void testTimedOffer() { final ArrayBlockingQueue q = new ArrayBlockingQueue(2); final CountDownLatch pleaseInterrupt = new CountDownLatch(1); Thread t = newStartedThread(new CheckedRunnable() { @@ -392,15 +401,24 @@ public class ArrayBlockingQueueTest exte long startTime = System.nanoTime(); assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS)); assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); + + Thread.currentThread().interrupt(); + try { + q.offer(new Object(), randomTimeout(), randomTimeUnit()); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + pleaseInterrupt.countDown(); try { - q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); + q.offer(new Object(), LONGER_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); }}); await(pleaseInterrupt); - assertThreadStaysAlive(t); + if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); t.interrupt(); awaitTermination(t); } @@ -423,9 +441,7 @@ public class ArrayBlockingQueueTest exte 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 { @@ -443,7 +459,7 @@ public class ArrayBlockingQueueTest exte }}); await(pleaseInterrupt); - assertThreadStaysAlive(t); + if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); t.interrupt(); awaitTermination(t); } @@ -493,24 +509,29 @@ public class ArrayBlockingQueueTest exte */ 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(LONG_DELAY_MS, MILLISECONDS); + q.poll(randomTimeout(), randomTimeUnit()); shouldThrow(); - } catch (InterruptedException success) { - assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); - } + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + + pleaseInterrupt.countDown(); + try { + q.poll(LONGER_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); }}); - await(aboutToWait); - waitForThreadToEnterWaitState(t); + await(pleaseInterrupt); + if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); t.interrupt(); awaitTermination(t); checkEmpty(q); @@ -563,8 +584,10 @@ public class ArrayBlockingQueueTest exte * contains(x) reports true when elements added but not yet removed */ public void testContains() { - ArrayBlockingQueue q = populatedQueue(SIZE); - for (int i = 0; i < SIZE; ++i) { + int size = ThreadLocalRandom.current().nextInt(1, SIZE); + ArrayBlockingQueue q = populatedQueue(size, size, 2 * size, false); + assertFalse(q.contains(null)); + for (int i = 0; i < size; ++i) { assertTrue(q.contains(new Integer(i))); assertEquals(i, q.poll()); assertFalse(q.contains(new Integer(i))); @@ -575,11 +598,13 @@ public class ArrayBlockingQueueTest exte * clear removes all elements */ public void testClear() { - ArrayBlockingQueue q = populatedQueue(SIZE); + int size = ThreadLocalRandom.current().nextInt(1, 5); + ArrayBlockingQueue q = populatedQueue(size, size, 2 * size, false); + int capacity = size + q.remainingCapacity(); q.clear(); assertTrue(q.isEmpty()); assertEquals(0, q.size()); - assertEquals(SIZE, q.remainingCapacity()); + assertEquals(capacity, q.remainingCapacity()); q.add(one); assertFalse(q.isEmpty()); assertTrue(q.contains(one)); @@ -663,7 +688,7 @@ public class ArrayBlockingQueueTest exte Integer x = (Integer) it.next(); assertEquals(s + i, (int) x); for (Object[] a : as) - assertSame(a1[i], x); + assertSame(a[i], x); } } @@ -848,7 +873,7 @@ public class ArrayBlockingQueueTest exte } /** - * A deserialized serialized queue has same elements in same order + * A deserialized/reserialized queue has same elements in same order */ public void testSerialization() throws Exception { Queue x = populatedQueue(SIZE); @@ -933,8 +958,8 @@ public class ArrayBlockingQueueTest exte */ public void testNeverContainsNull() { Collection[] qs = { - new ArrayBlockingQueue(10), - populatedQueue(2), + populatedQueue(0, 1, 10, false), + populatedQueue(2, 2, 10, true), }; for (Collection q : qs) {