--- jsr166/src/test/tck/LinkedTransferQueueTest.java 2015/06/14 20:58:14 1.62 +++ jsr166/src/test/tck/LinkedTransferQueueTest.java 2017/02/18 16:37:49 1.70 @@ -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; @@ -24,14 +25,6 @@ import junit.framework.Test; @SuppressWarnings({"unchecked", "rawtypes"}) public class LinkedTransferQueueTest extends JSR166TestCase { - static class Implementation implements CollectionImplementation { - public Class klazz() { return LinkedTransferQueue.class; } - public Collection emptyCollection() { return new LinkedTransferQueue(); } - public Object makeElement(int i) { return i; } - public boolean isConcurrent() { return true; } - public boolean permitsNulls() { return false; } - } - public static class Generic extends BlockingQueueTest { protected BlockingQueue emptyCollection() { return new LinkedTransferQueue(); @@ -43,6 +36,13 @@ public class LinkedTransferQueueTest ext } public static Test suite() { + class Implementation implements CollectionImplementation { + public Class klazz() { return LinkedTransferQueue.class; } + public Collection emptyCollection() { return new LinkedTransferQueue(); } + public Object makeElement(int i) { return i; } + public boolean isConcurrent() { return true; } + public boolean permitsNulls() { return false; } + } return newTestSuite(LinkedTransferQueueTest.class, new Generic().testSuite(), CollectionTest.testSuite(new Implementation())); @@ -183,7 +183,7 @@ public class LinkedTransferQueueTest ext * all elements successfully put are contained */ public void testPut() { - LinkedTransferQueue q = new LinkedTransferQueue(); + LinkedTransferQueue q = new LinkedTransferQueue<>(); for (int i = 0; i < SIZE; ++i) { assertEquals(i, q.size()); q.put(i); @@ -263,12 +263,12 @@ public class LinkedTransferQueueTest ext */ public void testTimedPoll() throws InterruptedException { LinkedTransferQueue q = populatedQueue(SIZE); - for (int i = 0; i < SIZE; ++i) { - long startTime = System.nanoTime(); - assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); - assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); - } long startTime = System.nanoTime(); + for (int i = 0; i < SIZE; ++i) + assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); + + startTime = System.nanoTime(); assertNull(q.poll(timeoutMillis(), MILLISECONDS)); assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); checkEmpty(q); @@ -283,25 +283,21 @@ public class LinkedTransferQueueTest ext final CountDownLatch aboutToWait = new CountDownLatch(1); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { - for (int i = 0; i < SIZE; ++i) { - long t0 = System.nanoTime(); + long startTime = System.nanoTime(); + for (int i = 0; i < SIZE; ++i) assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); - assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); - } - long t0 = System.nanoTime(); aboutToWait.countDown(); try { - q.poll(MEDIUM_DELAY_MS, MILLISECONDS); + q.poll(LONG_DELAY_MS, MILLISECONDS); shouldThrow(); - } catch (InterruptedException success) { - assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); - } + } catch (InterruptedException success) {} + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); }}); aboutToWait.await(); - waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); + waitForThreadToEnterWaitState(t); t.interrupt(); - awaitTermination(t, MEDIUM_DELAY_MS); + awaitTermination(t); checkEmpty(q); } @@ -313,19 +309,18 @@ public class LinkedTransferQueueTest ext final BlockingQueue q = populatedQueue(SIZE); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { + long startTime = System.nanoTime(); Thread.currentThread().interrupt(); - for (int i = 0; i < SIZE; ++i) { - long t0 = System.nanoTime(); + for (int i = 0; i < SIZE; ++i) assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); - assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); - } try { - q.poll(MEDIUM_DELAY_MS, MILLISECONDS); + q.poll(LONG_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (InterruptedException success) {} + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); }}); - awaitTermination(t, MEDIUM_DELAY_MS); + awaitTermination(t); checkEmpty(q); } @@ -421,7 +416,7 @@ public class LinkedTransferQueueTest ext */ public void testContainsAll() { LinkedTransferQueue q = populatedQueue(SIZE); - LinkedTransferQueue p = new LinkedTransferQueue(); + LinkedTransferQueue p = new LinkedTransferQueue<>(); for (int i = 0; i < SIZE; ++i) { assertTrue(q.containsAll(p)); assertFalse(p.containsAll(q)); @@ -550,8 +545,7 @@ public class LinkedTransferQueueTest ext * iterator ordering is FIFO */ public void testIteratorOrdering() { - final LinkedTransferQueue q - = new LinkedTransferQueue(); + final LinkedTransferQueue q = new LinkedTransferQueue<>(); assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); q.add(one); q.add(two); @@ -596,22 +590,24 @@ public class LinkedTransferQueueTest ext public void testOfferInExecutor() { final LinkedTransferQueue q = new LinkedTransferQueue(); final CheckedBarrier threadsStarted = new CheckedBarrier(2); - ExecutorService executor = Executors.newFixedThreadPool(2); + final ExecutorService executor = Executors.newFixedThreadPool(2); + try (PoolCleaner cleaner = cleaner(executor)) { - executor.execute(new CheckedRunnable() { - public void realRun() throws InterruptedException { - threadsStarted.await(); - assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS)); - }}); - - executor.execute(new CheckedRunnable() { - public void realRun() throws InterruptedException { - threadsStarted.await(); - assertSame(one, q.take()); - checkEmpty(q); - }}); + executor.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { + threadsStarted.await(); + long startTime = System.nanoTime(); + assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); + }}); - joinPool(executor); + executor.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { + threadsStarted.await(); + assertSame(one, q.take()); + checkEmpty(q); + }}); + } } /** @@ -620,23 +616,25 @@ public class LinkedTransferQueueTest ext public void testPollInExecutor() { final LinkedTransferQueue q = new LinkedTransferQueue(); final CheckedBarrier threadsStarted = new CheckedBarrier(2); - ExecutorService executor = Executors.newFixedThreadPool(2); + 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)); - checkEmpty(q); - }}); - - executor.execute(new CheckedRunnable() { - public void realRun() throws InterruptedException { - threadsStarted.await(); - q.put(one); - }}); + executor.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { + assertNull(q.poll()); + threadsStarted.await(); + long startTime = System.nanoTime(); + assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); + checkEmpty(q); + }}); - joinPool(executor); + executor.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { + threadsStarted.await(); + q.put(one); + }}); + } } /** @@ -697,7 +695,7 @@ public class LinkedTransferQueueTest ext assertTrue(l.size() >= SIZE); for (int i = 0; i < SIZE; ++i) assertEquals(i, l.get(i)); - awaitTermination(t, MEDIUM_DELAY_MS); + awaitTermination(t); assertTrue(q.size() + l.size() >= SIZE); } @@ -734,21 +732,25 @@ public class LinkedTransferQueueTest ext Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { threadStarted.countDown(); + long startTime = System.nanoTime(); assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); assertEquals(0, q.getWaitingConsumerCount()); assertFalse(q.hasWaitingConsumer()); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); }}); threadStarted.await(); - waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); - 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()); assertFalse(q.hasWaitingConsumer()); - awaitTermination(t, MEDIUM_DELAY_MS); + awaitTermination(t); } /** @@ -764,11 +766,10 @@ public class LinkedTransferQueueTest ext /** * transfer waits until a poll occurs. The transfered element - * is returned by this associated poll. + * is returned by the associated poll. */ public void testTransfer2() throws InterruptedException { - final LinkedTransferQueue q - = new LinkedTransferQueue(); + final LinkedTransferQueue q = new LinkedTransferQueue<>(); final CountDownLatch threadStarted = new CountDownLatch(1); Thread t = newStartedThread(new CheckedRunnable() { @@ -779,19 +780,21 @@ public class LinkedTransferQueueTest ext }}); threadStarted.await(); - waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); - 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, MEDIUM_DELAY_MS); + awaitTermination(t); } /** * transfer waits until a poll occurs, and then transfers in fifo order */ public void testTransfer3() throws InterruptedException { - final LinkedTransferQueue q - = new LinkedTransferQueue(); + final LinkedTransferQueue q = new LinkedTransferQueue<>(); Thread first = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { @@ -839,16 +842,15 @@ public class LinkedTransferQueueTest ext assertEquals(1, q.size()); assertTrue(q.offer(three)); assertSame(four, q.poll()); - awaitTermination(t, MEDIUM_DELAY_MS); + awaitTermination(t); } /** * transfer waits until a take occurs. The transfered element - * is returned by this associated take. + * is returned by the associated take. */ public void testTransfer5() throws InterruptedException { - final LinkedTransferQueue q - = new LinkedTransferQueue(); + final LinkedTransferQueue q = new LinkedTransferQueue<>(); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { @@ -862,7 +864,7 @@ public class LinkedTransferQueueTest ext assertEquals(1, q.size()); assertSame(four, q.take()); checkEmpty(q); - awaitTermination(t, MEDIUM_DELAY_MS); + awaitTermination(t); } /** @@ -904,9 +906,11 @@ public class LinkedTransferQueueTest ext assertTrue(q.tryTransfer(hotPotato)); }}); - assertSame(hotPotato, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); + long startTime = System.nanoTime(); + assertSame(hotPotato, q.poll(LONG_DELAY_MS, MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); checkEmpty(q); - awaitTermination(t, MEDIUM_DELAY_MS); + awaitTermination(t); } /** @@ -928,7 +932,7 @@ public class LinkedTransferQueueTest ext assertSame(q.take(), hotPotato); checkEmpty(q); - awaitTermination(t, MEDIUM_DELAY_MS); + awaitTermination(t); } /** @@ -941,6 +945,7 @@ public class LinkedTransferQueueTest ext Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { + long startTime = System.nanoTime(); Thread.currentThread().interrupt(); try { q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); @@ -954,6 +959,7 @@ public class LinkedTransferQueueTest ext shouldThrow(); } catch (InterruptedException success) {} assertFalse(Thread.interrupted()); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); }}); await(pleaseInterrupt); @@ -971,10 +977,10 @@ public class LinkedTransferQueueTest ext Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { - long t0 = System.nanoTime(); + long startTime = System.nanoTime(); assertFalse(q.tryTransfer(new Object(), timeoutMillis(), MILLISECONDS)); - assertTrue(millisElapsedSince(t0) >= timeoutMillis()); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); checkEmpty(q); }}); @@ -992,7 +998,9 @@ public class LinkedTransferQueueTest ext Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { - assertTrue(q.tryTransfer(five, MEDIUM_DELAY_MS, MILLISECONDS)); + long startTime = System.nanoTime(); + assertTrue(q.tryTransfer(five, LONG_DELAY_MS, MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); checkEmpty(q); }}); @@ -1002,7 +1010,7 @@ public class LinkedTransferQueueTest ext assertSame(four, q.poll()); assertSame(five, q.poll()); checkEmpty(q); - awaitTermination(t, MEDIUM_DELAY_MS); + awaitTermination(t); } /** @@ -1013,9 +1021,9 @@ public class LinkedTransferQueueTest ext final LinkedTransferQueue q = new LinkedTransferQueue(); assertTrue(q.offer(four)); assertEquals(1, q.size()); - long t0 = System.nanoTime(); + long startTime = System.nanoTime(); assertFalse(q.tryTransfer(five, timeoutMillis(), MILLISECONDS)); - assertTrue(millisElapsedSince(t0) >= timeoutMillis()); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); assertEquals(1, q.size()); assertSame(four, q.poll()); assertNull(q.poll()); @@ -1023,7 +1031,7 @@ public class LinkedTransferQueueTest ext } private LinkedTransferQueue populatedQueue(int n) { - LinkedTransferQueue q = new LinkedTransferQueue(); + LinkedTransferQueue q = new LinkedTransferQueue<>(); checkEmpty(q); for (int i = 0; i < n; i++) { assertEquals(i, q.size());