--- jsr166/src/test/tck/SynchronousQueueTest.java 2017/05/14 00:56:43 1.56 +++ jsr166/src/test/tck/SynchronousQueueTest.java 2019/09/05 20:54:24 1.66 @@ -96,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); } @@ -138,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()); @@ -158,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); @@ -172,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()); @@ -181,26 +188,34 @@ public class SynchronousQueueTest extend /** * timed offer times out if elements not taken */ - public void testTimedOffer() { testTimedOffer(false); } - public void testTimedOffer_fair() { testTimedOffer(true); } - public void testTimedOffer(boolean fair) { + public void testTimedOffer() { + 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); } @@ -229,11 +244,10 @@ public class SynchronousQueueTest extend /** * timed poll with nonzero timeout times out if no active putter */ - public void testTimedPoll() { testTimedPoll(false); } - public void testTimedPoll_fair() { testTimedPoll(true); } - public void testTimedPoll(boolean fair) { + public void testTimedPoll() { + final boolean fair = randomBoolean(); final SynchronousQueue q = new SynchronousQueue(fair); - long startTime = System.nanoTime(); + final long startTime = System.nanoTime(); try { assertNull(q.poll(timeoutMillis(), MILLISECONDS)); } catch (InterruptedException e) { threadUnexpectedException(e); } assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); @@ -243,9 +257,8 @@ public class SynchronousQueueTest extend * timed poll before a delayed offer times out, returning null; * after offer succeeds; on interruption throws */ - public void testTimedPollWithOffer() { testTimedPollWithOffer(false); } - public void testTimedPollWithOffer_fair() { testTimedPollWithOffer(true); } - public void testTimedPollWithOffer(boolean fair) { + public void testTimedPollWithOffer() { + final boolean fair = randomBoolean(); final SynchronousQueue q = new SynchronousQueue(fair); final CountDownLatch pleaseOffer = new CountDownLatch(1); final CountDownLatch pleaseInterrupt = new CountDownLatch(1); @@ -261,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()); @@ -283,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); } @@ -424,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[] o = q.toArray((Object[])null); shouldThrow(); } catch (NullPointerException success) {} } @@ -439,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); } @@ -517,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();