--- jsr166/src/test/tck/SynchronousQueueTest.java 2017/05/14 03:48:35 1.57 +++ jsr166/src/test/tck/SynchronousQueueTest.java 2019/12/16 22:55:54 1.67 @@ -18,7 +18,6 @@ import java.util.concurrent.CountDownLat import java.util.concurrent.Executors; import java.util.concurrent.ExecutorService; import java.util.concurrent.SynchronousQueue; -import java.util.concurrent.ThreadLocalRandom; import junit.framework.Test; @@ -97,7 +96,7 @@ public class SynchronousQueueTest extend } /** - * addAll throws ISE if no active taker + * addAll throws IllegalStateException if no active taker */ public void testAddAll_ISE() { testAddAll_ISE(false); } public void testAddAll_ISE_fair() { testAddAll_ISE(true); } @@ -139,7 +138,7 @@ public class SynchronousQueueTest extend }}); await(pleaseInterrupt); - assertThreadBlocks(t, Thread.State.WAITING); + if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); t.interrupt(); awaitTermination(t); assertEquals(0, q.remainingCapacity()); @@ -159,6 +158,13 @@ public class SynchronousQueueTest extend pleaseTake.countDown(); q.put(one); + Thread.currentThread().interrupt(); + try { + q.put(99); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + pleaseInterrupt.countDown(); try { q.put(99); @@ -173,7 +179,7 @@ public class SynchronousQueueTest extend catch (InterruptedException e) { threadUnexpectedException(e); } await(pleaseInterrupt); - assertThreadBlocks(t, Thread.State.WAITING); + if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); t.interrupt(); awaitTermination(t); assertEquals(0, q.remainingCapacity()); @@ -183,24 +189,33 @@ public class SynchronousQueueTest extend * timed offer times out if elements not taken */ public void testTimedOffer() { - final boolean fair = ThreadLocalRandom.current().nextBoolean(); + final boolean fair = randomBoolean(); final SynchronousQueue q = new SynchronousQueue(fair); final CountDownLatch pleaseInterrupt = new CountDownLatch(1); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { 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); - assertThreadBlocks(t, Thread.State.TIMED_WAITING); + if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); t.interrupt(); awaitTermination(t); } @@ -230,7 +245,7 @@ public class SynchronousQueueTest extend * timed poll with nonzero timeout times out if no active putter */ public void testTimedPoll() { - final boolean fair = ThreadLocalRandom.current().nextBoolean(); + final boolean fair = randomBoolean(); final SynchronousQueue q = new SynchronousQueue(fair); final long startTime = System.nanoTime(); try { assertNull(q.poll(timeoutMillis(), MILLISECONDS)); } @@ -243,7 +258,7 @@ public class SynchronousQueueTest extend * after offer succeeds; on interruption throws */ public void testTimedPollWithOffer() { - final boolean fair = ThreadLocalRandom.current().nextBoolean(); + final boolean fair = randomBoolean(); final SynchronousQueue q = new SynchronousQueue(fair); final CountDownLatch pleaseOffer = new CountDownLatch(1); final CountDownLatch pleaseInterrupt = new CountDownLatch(1); @@ -259,7 +274,7 @@ public class SynchronousQueueTest extend Thread.currentThread().interrupt(); try { - q.poll(LONG_DELAY_MS, MILLISECONDS); + q.poll(randomTimeout(), randomTimeUnit()); shouldThrow(); } catch (InterruptedException success) {} assertFalse(Thread.interrupted()); @@ -281,7 +296,7 @@ public class SynchronousQueueTest extend 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); } @@ -422,7 +437,7 @@ public class SynchronousQueueTest extend public void testToArray_null(boolean fair) { final SynchronousQueue q = new SynchronousQueue(fair); try { - Object[] o = q.toArray(null); + Object[] unused = q.toArray((Object[])null); shouldThrow(); } catch (NullPointerException success) {} } @@ -437,7 +452,7 @@ public class SynchronousQueueTest extend } /** - * iterator remove throws ISE + * iterator remove throws IllegalStateException */ public void testIteratorRemove() { testIteratorRemove(false); } public void testIteratorRemove_fair() { testIteratorRemove(true); } @@ -515,7 +530,7 @@ public class SynchronousQueueTest extend } /** - * a deserialized serialized queue is usable + * a deserialized/reserialized queue is usable */ public void testSerialization() { final SynchronousQueue x = new SynchronousQueue();