--- jsr166/src/test/tck/ArrayBlockingQueueTest.java 2017/05/14 04:02:06 1.89 +++ jsr166/src/test/tck/ArrayBlockingQueueTest.java 2019/08/13 00:54:51 1.94 @@ -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,7 +227,7 @@ 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); @@ -240,7 +240,7 @@ public class ArrayBlockingQueueTest exte } /** - * addAll(this) throws IAE + * addAll(this) throws IllegalArgumentException */ public void testAddAllSelf() { ArrayBlockingQueue q = populatedQueue(SIZE); @@ -266,7 +266,7 @@ public class ArrayBlockingQueueTest exte } /** - * addAll throws ISE if not enough room + * addAll throws IllegalStateException if not enough room */ public void testAddAll_insufficientSpace() { int size = ThreadLocalRandom.current().nextInt(1, SIZE); @@ -340,7 +340,7 @@ public class ArrayBlockingQueueTest exte }}); await(pleaseInterrupt); - assertThreadBlocks(t, Thread.State.WAITING); + if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); t.interrupt(); awaitTermination(t); assertEquals(SIZE, q.size()); @@ -382,7 +382,7 @@ public class ArrayBlockingQueueTest exte assertEquals(0, q.take()); await(pleaseInterrupt); - assertThreadBlocks(t, Thread.State.WAITING); + if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); t.interrupt(); awaitTermination(t); assertEquals(0, q.remainingCapacity()); @@ -391,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() { @@ -401,16 +401,26 @@ 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(), LONG_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (InterruptedException success) {} assertFalse(Thread.interrupted()); + + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); }}); await(pleaseInterrupt); - assertThreadBlocks(t, Thread.State.TIMED_WAITING); + if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); t.interrupt(); awaitTermination(t); } @@ -451,7 +461,7 @@ public class ArrayBlockingQueueTest exte }}); await(pleaseInterrupt); - assertThreadBlocks(t, Thread.State.WAITING); + if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); t.interrupt(); awaitTermination(t); } @@ -510,7 +520,7 @@ public class ArrayBlockingQueueTest exte Thread.currentThread().interrupt(); try { - q.poll(LONG_DELAY_MS, MILLISECONDS); + q.poll(randomTimeout(), randomTimeUnit()); shouldThrow(); } catch (InterruptedException success) {} assertFalse(Thread.interrupted()); @@ -526,7 +536,7 @@ public class ArrayBlockingQueueTest exte }}); await(pleaseInterrupt); - assertThreadBlocks(t, Thread.State.TIMED_WAITING); + if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); t.interrupt(); awaitTermination(t); checkEmpty(q); @@ -868,7 +878,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);