--- jsr166/src/test/tck/SynchronousQueueTest.java 2014/12/31 20:29:13 1.45 +++ jsr166/src/test/tck/SynchronousQueueTest.java 2017/08/04 03:30:21 1.61 @@ -18,6 +18,7 @@ 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; @@ -36,7 +37,7 @@ public class SynchronousQueueTest extend } public static void main(String[] args) { - junit.textui.TestRunner.run(suite()); + main(suite(), args); } public static Test suite() { @@ -96,7 +97,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 +139,7 @@ public class SynchronousQueueTest extend }}); await(pleaseInterrupt); - assertThreadStaysAlive(t); + assertThreadBlocks(t, Thread.State.WAITING); t.interrupt(); awaitTermination(t); assertEquals(0, q.remainingCapacity()); @@ -158,6 +159,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 +180,7 @@ public class SynchronousQueueTest extend catch (InterruptedException e) { threadUnexpectedException(e); } await(pleaseInterrupt); - assertThreadStaysAlive(t); + assertThreadBlocks(t, Thread.State.WAITING); t.interrupt(); awaitTermination(t); assertEquals(0, q.remainingCapacity()); @@ -181,9 +189,8 @@ 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 = ThreadLocalRandom.current().nextBoolean(); final SynchronousQueue q = new SynchronousQueue(fair); final CountDownLatch pleaseInterrupt = new CountDownLatch(1); Thread t = newStartedThread(new CheckedRunnable() { @@ -191,15 +198,24 @@ public class SynchronousQueueTest extend long startTime = System.nanoTime(); assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS)); assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); + + Thread.currentThread().interrupt(); + try { + q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + pleaseInterrupt.countDown(); try { q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); }}); await(pleaseInterrupt); - assertThreadStaysAlive(t); + assertThreadBlocks(t, Thread.State.TIMED_WAITING); t.interrupt(); awaitTermination(t); } @@ -228,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 = ThreadLocalRandom.current().nextBoolean(); 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()); @@ -242,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 = ThreadLocalRandom.current().nextBoolean(); final SynchronousQueue q = new SynchronousQueue(fair); final CountDownLatch pleaseOffer = new CountDownLatch(1); final CountDownLatch pleaseInterrupt = new CountDownLatch(1); @@ -257,7 +271,6 @@ public class SynchronousQueueTest extend pleaseOffer.countDown(); startTime = System.nanoTime(); assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); - assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS); Thread.currentThread().interrupt(); try { @@ -272,16 +285,18 @@ public class SynchronousQueueTest extend shouldThrow(); } catch (InterruptedException success) {} assertFalse(Thread.interrupted()); + + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); }}); await(pleaseOffer); long startTime = System.nanoTime(); try { assertTrue(q.offer(zero, LONG_DELAY_MS, MILLISECONDS)); } catch (InterruptedException e) { threadUnexpectedException(e); } - assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); await(pleaseInterrupt); - assertThreadStaysAlive(t); + assertThreadBlocks(t, Thread.State.TIMED_WAITING); t.interrupt(); awaitTermination(t); } @@ -400,8 +415,7 @@ public class SynchronousQueueTest extend public void testToArray2() { testToArray2(false); } public void testToArray2_fair() { testToArray2(true); } public void testToArray2(boolean fair) { - final SynchronousQueue q - = new SynchronousQueue(fair); + final SynchronousQueue q = new SynchronousQueue<>(fair); Integer[] a; a = new Integer[0]; @@ -434,17 +448,11 @@ public class SynchronousQueueTest extend public void testIterator() { testIterator(false); } public void testIterator_fair() { testIterator(true); } public void testIterator(boolean fair) { - final SynchronousQueue q = new SynchronousQueue(fair); - Iterator it = q.iterator(); - assertFalse(it.hasNext()); - try { - Object x = it.next(); - shouldThrow(); - } catch (NoSuchElementException success) {} + assertIteratorExhausted(new SynchronousQueue(fair).iterator()); } /** - * iterator remove throws ISE + * iterator remove throws IllegalStateException */ public void testIteratorRemove() { testIteratorRemove(false); } public void testIteratorRemove_fair() { testIteratorRemove(true); } @@ -475,24 +483,24 @@ public class SynchronousQueueTest extend public void testOfferInExecutor_fair() { testOfferInExecutor(true); } public void testOfferInExecutor(boolean fair) { final SynchronousQueue q = new SynchronousQueue(fair); - ExecutorService executor = Executors.newFixedThreadPool(2); final CheckedBarrier threadsStarted = new CheckedBarrier(2); + final ExecutorService executor = Executors.newFixedThreadPool(2); + try (PoolCleaner cleaner = cleaner(executor)) { - executor.execute(new CheckedRunnable() { - public void realRun() throws InterruptedException { - assertFalse(q.offer(one)); - threadsStarted.await(); - assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS)); - assertEquals(0, q.remainingCapacity()); - }}); - - executor.execute(new CheckedRunnable() { - public void realRun() throws InterruptedException { - threadsStarted.await(); - assertSame(one, q.take()); - }}); - - joinPool(executor); + executor.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { + assertFalse(q.offer(one)); + threadsStarted.await(); + assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS)); + assertEquals(0, q.remainingCapacity()); + }}); + + executor.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { + threadsStarted.await(); + assertSame(one, q.take()); + }}); + } } /** @@ -503,26 +511,26 @@ public class SynchronousQueueTest extend public void testPollInExecutor(boolean fair) { final SynchronousQueue q = new SynchronousQueue(fair); final CheckedBarrier threadsStarted = new CheckedBarrier(2); - ExecutorService executor = Executors.newFixedThreadPool(2); - executor.execute(new CheckedRunnable() { - public void realRun() throws InterruptedException { - assertNull(q.poll()); - threadsStarted.await(); - assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); - assertTrue(q.isEmpty()); - }}); - - executor.execute(new CheckedRunnable() { - public void realRun() throws InterruptedException { - threadsStarted.await(); - q.put(one); - }}); - - joinPool(executor); + final ExecutorService executor = Executors.newFixedThreadPool(2); + try (PoolCleaner cleaner = cleaner(executor)) { + executor.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { + assertNull(q.poll()); + threadsStarted.await(); + assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); + assertTrue(q.isEmpty()); + }}); + + executor.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { + threadsStarted.await(); + q.put(one); + }}); + } } /** - * a deserialized serialized queue is usable + * a deserialized/reserialized queue is usable */ public void testSerialization() { final SynchronousQueue x = new SynchronousQueue(); @@ -575,7 +583,7 @@ public class SynchronousQueueTest extend fail("timed out"); Thread.yield(); } - assertTrue(l.size() == 1); + assertEquals(1, l.size()); assertSame(one, l.get(0)); awaitTermination(t); } @@ -596,10 +604,12 @@ public class SynchronousQueueTest extend }}); ArrayList l = new ArrayList(); - delay(SHORT_DELAY_MS); - q.drainTo(l, 1); + int drained; + while ((drained = q.drainTo(l, 1)) == 0) Thread.yield(); + assertEquals(1, drained); assertEquals(1, l.size()); - q.drainTo(l, 1); + while ((drained = q.drainTo(l, 1)) == 0) Thread.yield(); + assertEquals(1, drained); assertEquals(2, l.size()); assertTrue(l.contains(one)); assertTrue(l.contains(two));