--- jsr166/src/test/tck/LinkedTransferQueueTest.java 2017/02/18 01:17:06 1.69 +++ jsr166/src/test/tck/LinkedTransferQueueTest.java 2017/08/15 20:30:30 1.80 @@ -15,6 +15,7 @@ import java.util.List; import java.util.NoSuchElementException; import java.util.Queue; import java.util.concurrent.BlockingQueue; +import java.util.concurrent.Callable; import java.util.concurrent.CountDownLatch; import java.util.concurrent.Executors; import java.util.concurrent.ExecutorService; @@ -208,9 +209,7 @@ public class LinkedTransferQueueTest ext 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 { @@ -228,7 +227,7 @@ public class LinkedTransferQueueTest ext }}); await(pleaseInterrupt); - assertThreadStaysAlive(t); + assertThreadBlocks(t, Thread.State.WAITING); t.interrupt(); awaitTermination(t); } @@ -279,22 +278,32 @@ public class LinkedTransferQueueTest ext */ 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); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + + pleaseInterrupt.countDown(); try { q.poll(LONG_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); }}); - aboutToWait.await(); - waitForThreadToEnterWaitState(t); + await(pleaseInterrupt); + assertThreadBlocks(t, Thread.State.TIMED_WAITING); t.interrupt(); awaitTermination(t); checkEmpty(q); @@ -316,6 +325,7 @@ public class LinkedTransferQueueTest ext q.poll(LONG_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); }}); @@ -637,7 +647,7 @@ public class LinkedTransferQueueTest ext } /** - * 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); @@ -739,9 +749,11 @@ public class LinkedTransferQueueTest ext }}); threadStarted.await(); - waitForThreadToEnterWaitState(t); - assertEquals(1, q.getWaitingConsumerCount()); - assertTrue(q.hasWaitingConsumer()); + Callable oneConsumer + = new Callable() { public Boolean call() { + return q.hasWaitingConsumer() + && q.getWaitingConsumerCount() == 1; }}; + waitForThreadToEnterWaitState(t, oneConsumer); assertTrue(q.offer(one)); assertEquals(0, q.getWaitingConsumerCount()); @@ -762,7 +774,7 @@ public class LinkedTransferQueueTest ext } /** - * transfer waits until a poll occurs. The transfered element + * transfer waits until a poll occurs. The transferred element * is returned by the associated poll. */ public void testTransfer2() throws InterruptedException { @@ -777,8 +789,11 @@ public class LinkedTransferQueueTest ext }}); threadStarted.await(); - waitForThreadToEnterWaitState(t); - assertEquals(1, q.size()); + Callable oneElement + = new Callable() { public Boolean call() { + return !q.isEmpty() && q.size() == 1; }}; + waitForThreadToEnterWaitState(t, oneElement); + assertSame(five, q.poll()); checkEmpty(q); awaitTermination(t); @@ -793,7 +808,7 @@ public class LinkedTransferQueueTest ext Thread first = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { q.transfer(four); - assertTrue(!q.contains(four)); + assertFalse(q.contains(four)); assertEquals(1, q.size()); }}); @@ -840,7 +855,7 @@ public class LinkedTransferQueueTest ext } /** - * transfer waits until a take occurs. The transfered element + * transfer waits until a take occurs. The transferred element * is returned by the associated take. */ public void testTransfer5() throws InterruptedException { @@ -957,7 +972,7 @@ public class LinkedTransferQueueTest ext }}); await(pleaseInterrupt); - assertThreadStaysAlive(t); + assertThreadBlocks(t, Thread.State.TIMED_WAITING); t.interrupt(); awaitTermination(t); checkEmpty(q);