diff options
author | Przemyslaw Szczepaniak <pszczepaniak@google.com> | 2016-03-15 09:42:33 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2016-03-15 09:42:33 +0000 |
commit | a8a9d448e97004dcd25c9ccb128e09bcb7690f6a (patch) | |
tree | 214d4e0cdccf19471194d03dd3172933336910f1 /jsr166-tests/src/test/java/jsr166/LinkedTransferQueueTest.java | |
parent | 37f9fec54f50b2e0898ee2b61d058c4de879e38b (diff) | |
parent | ed4f365789d43b1961657195df223a19bf4ef20f (diff) |
Merge "Revert "JSR-166 update""
Diffstat (limited to 'jsr166-tests/src/test/java/jsr166/LinkedTransferQueueTest.java')
-rw-r--r-- | jsr166-tests/src/test/java/jsr166/LinkedTransferQueueTest.java | 191 |
1 files changed, 92 insertions, 99 deletions
diff --git a/jsr166-tests/src/test/java/jsr166/LinkedTransferQueueTest.java b/jsr166-tests/src/test/java/jsr166/LinkedTransferQueueTest.java index c71259234a..8d3f276e01 100644 --- a/jsr166-tests/src/test/java/jsr166/LinkedTransferQueueTest.java +++ b/jsr166-tests/src/test/java/jsr166/LinkedTransferQueueTest.java @@ -25,33 +25,30 @@ import java.util.concurrent.LinkedTransferQueue; 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; } - } +// android-changed: Extend BlockingQueueTest directly. +public class LinkedTransferQueueTest extends BlockingQueueTest { - public static class Generic extends BlockingQueueTest { - protected BlockingQueue emptyCollection() { - return new LinkedTransferQueue(); - } - } - - // android-note: Removed because the CTS runner does a bad job of - // retrying tests that have suite() declarations. + // android-changed: Extend BlockingQueueTest directly. + // + // public static class Generic extends BlockingQueueTest { + // protected BlockingQueue emptyCollection() { + // return new LinkedTransferQueue(); + // } + // } // // public static void main(String[] args) { // main(suite(), args); // } + // // public static Test suite() { // return newTestSuite(LinkedTransferQueueTest.class, - // new Generic().testSuite(), - // CollectionTest.testSuite(new Implementation())); + // new Generic().testSuite()); // } + protected BlockingQueue emptyCollection() { + return new LinkedTransferQueue(); + } + /** * Constructor builds new queue with size being zero and empty * being true @@ -90,7 +87,7 @@ public class LinkedTransferQueueTest extends JSR166TestCase { */ public void testConstructor4() { Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE - 1; ++i) + for (int i = 0; i < SIZE-1; ++i) ints[i] = i; Collection<Integer> elements = Arrays.asList(ints); try { @@ -144,8 +141,8 @@ public class LinkedTransferQueueTest extends JSR166TestCase { * addAll(this) throws IllegalArgumentException */ public void testAddAllSelf() { - LinkedTransferQueue q = populatedQueue(SIZE); try { + LinkedTransferQueue q = populatedQueue(SIZE); q.addAll(q); shouldThrow(); } catch (IllegalArgumentException success) {} @@ -156,11 +153,12 @@ public class LinkedTransferQueueTest extends JSR166TestCase { * NullPointerException after possibly adding some elements */ public void testAddAll3() { - LinkedTransferQueue q = new LinkedTransferQueue(); - Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE - 1; ++i) - ints[i] = i; try { + LinkedTransferQueue q = new LinkedTransferQueue(); + Integer[] ints = new Integer[SIZE]; + for (int i = 0; i < SIZE - 1; ++i) { + ints[i] = i; + } q.addAll(Arrays.asList(ints)); shouldThrow(); } catch (NullPointerException success) {} @@ -267,12 +265,12 @@ public class LinkedTransferQueueTest extends JSR166TestCase { */ public void testTimedPoll() throws InterruptedException { LinkedTransferQueue<Integer> q = populatedQueue(SIZE); - long startTime = System.nanoTime(); - for (int i = 0; i < SIZE; ++i) + 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); - - startTime = System.nanoTime(); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); + } + long startTime = System.nanoTime(); assertNull(q.poll(timeoutMillis(), MILLISECONDS)); assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); checkEmpty(q); @@ -287,21 +285,25 @@ public class LinkedTransferQueueTest extends JSR166TestCase { final CountDownLatch aboutToWait = 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) { + long t0 = System.nanoTime(); assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); + assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); + } + long t0 = System.nanoTime(); aboutToWait.countDown(); try { - q.poll(LONG_DELAY_MS, MILLISECONDS); + q.poll(MEDIUM_DELAY_MS, MILLISECONDS); shouldThrow(); - } catch (InterruptedException success) {} - assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); + } catch (InterruptedException success) { + assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); + } }}); aboutToWait.await(); - waitForThreadToEnterWaitState(t); + waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); t.interrupt(); - awaitTermination(t); + awaitTermination(t, MEDIUM_DELAY_MS); checkEmpty(q); } @@ -313,18 +315,19 @@ public class LinkedTransferQueueTest extends JSR166TestCase { final BlockingQueue<Integer> 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) + for (int i = 0; i < SIZE; ++i) { + long t0 = System.nanoTime(); assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); + assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); + } try { - q.poll(LONG_DELAY_MS, MILLISECONDS); + q.poll(MEDIUM_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (InterruptedException success) {} - assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); }}); - awaitTermination(t); + awaitTermination(t, MEDIUM_DELAY_MS); checkEmpty(q); } @@ -595,24 +598,22 @@ public class LinkedTransferQueueTest extends JSR166TestCase { public void testOfferInExecutor() { final LinkedTransferQueue q = new LinkedTransferQueue(); final CheckedBarrier threadsStarted = new CheckedBarrier(2); - final ExecutorService executor = Executors.newFixedThreadPool(2); - try (PoolCleaner cleaner = cleaner(executor)) { + ExecutorService executor = Executors.newFixedThreadPool(2); - 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); - }}); + 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(); + assertSame(one, q.take()); + checkEmpty(q); + }}); + + joinPool(executor); } /** @@ -621,25 +622,23 @@ public class LinkedTransferQueueTest extends JSR166TestCase { public void testPollInExecutor() { final LinkedTransferQueue q = new LinkedTransferQueue(); final CheckedBarrier threadsStarted = new CheckedBarrier(2); - final ExecutorService executor = Executors.newFixedThreadPool(2); - try (PoolCleaner cleaner = cleaner(executor)) { + ExecutorService executor = Executors.newFixedThreadPool(2); - 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); - }}); + 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 { + threadsStarted.await(); + q.put(one); + }}); + + joinPool(executor); } /** @@ -700,7 +699,7 @@ public class LinkedTransferQueueTest extends JSR166TestCase { assertTrue(l.size() >= SIZE); for (int i = 0; i < SIZE; ++i) assertEquals(i, l.get(i)); - awaitTermination(t); + awaitTermination(t, MEDIUM_DELAY_MS); assertTrue(q.size() + l.size() >= SIZE); } @@ -737,15 +736,14 @@ public class LinkedTransferQueueTest extends JSR166TestCase { 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); + // waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); + waitForThreadToEnterWaitStateNoTimeout(t); assertEquals(1, q.getWaitingConsumerCount()); assertTrue(q.hasWaitingConsumer()); @@ -753,7 +751,7 @@ public class LinkedTransferQueueTest extends JSR166TestCase { assertEquals(0, q.getWaitingConsumerCount()); assertFalse(q.hasWaitingConsumer()); - awaitTermination(t); + awaitTermination(t, MEDIUM_DELAY_MS); } /** @@ -784,11 +782,12 @@ public class LinkedTransferQueueTest extends JSR166TestCase { }}); threadStarted.await(); - waitForThreadToEnterWaitState(t); + // waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); + waitForThreadToEnterWaitStateNoTimeout(t); assertEquals(1, q.size()); assertSame(five, q.poll()); checkEmpty(q); - awaitTermination(t); + awaitTermination(t, MEDIUM_DELAY_MS); } /** @@ -844,7 +843,7 @@ public class LinkedTransferQueueTest extends JSR166TestCase { assertEquals(1, q.size()); assertTrue(q.offer(three)); assertSame(four, q.poll()); - awaitTermination(t); + awaitTermination(t, MEDIUM_DELAY_MS); } /** @@ -867,15 +866,15 @@ public class LinkedTransferQueueTest extends JSR166TestCase { assertEquals(1, q.size()); assertSame(four, q.take()); checkEmpty(q); - awaitTermination(t); + awaitTermination(t, MEDIUM_DELAY_MS); } /** * tryTransfer(null) throws NullPointerException */ public void testTryTransfer1() { - final LinkedTransferQueue q = new LinkedTransferQueue(); try { + final LinkedTransferQueue q = new LinkedTransferQueue(); q.tryTransfer(null); shouldThrow(); } catch (NullPointerException success) {} @@ -909,11 +908,9 @@ public class LinkedTransferQueueTest extends JSR166TestCase { assertTrue(q.tryTransfer(hotPotato)); }}); - long startTime = System.nanoTime(); - assertSame(hotPotato, q.poll(LONG_DELAY_MS, MILLISECONDS)); - assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); + assertSame(hotPotato, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); checkEmpty(q); - awaitTermination(t); + awaitTermination(t, MEDIUM_DELAY_MS); } /** @@ -935,7 +932,7 @@ public class LinkedTransferQueueTest extends JSR166TestCase { assertSame(q.take(), hotPotato); checkEmpty(q); - awaitTermination(t); + awaitTermination(t, MEDIUM_DELAY_MS); } /** @@ -948,7 +945,6 @@ public class LinkedTransferQueueTest extends JSR166TestCase { 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); @@ -962,7 +958,6 @@ public class LinkedTransferQueueTest extends JSR166TestCase { shouldThrow(); } catch (InterruptedException success) {} assertFalse(Thread.interrupted()); - assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); }}); await(pleaseInterrupt); @@ -980,10 +975,10 @@ public class LinkedTransferQueueTest extends JSR166TestCase { Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { - long startTime = System.nanoTime(); + long t0 = System.nanoTime(); assertFalse(q.tryTransfer(new Object(), timeoutMillis(), MILLISECONDS)); - assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); + assertTrue(millisElapsedSince(t0) >= timeoutMillis()); checkEmpty(q); }}); @@ -1001,9 +996,7 @@ public class LinkedTransferQueueTest extends JSR166TestCase { Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { - long startTime = System.nanoTime(); - assertTrue(q.tryTransfer(five, LONG_DELAY_MS, MILLISECONDS)); - assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); + assertTrue(q.tryTransfer(five, MEDIUM_DELAY_MS, MILLISECONDS)); checkEmpty(q); }}); @@ -1013,7 +1006,7 @@ public class LinkedTransferQueueTest extends JSR166TestCase { assertSame(four, q.poll()); assertSame(five, q.poll()); checkEmpty(q); - awaitTermination(t); + awaitTermination(t, MEDIUM_DELAY_MS); } /** @@ -1024,9 +1017,9 @@ public class LinkedTransferQueueTest extends JSR166TestCase { final LinkedTransferQueue q = new LinkedTransferQueue(); assertTrue(q.offer(four)); assertEquals(1, q.size()); - long startTime = System.nanoTime(); + long t0 = System.nanoTime(); assertFalse(q.tryTransfer(five, timeoutMillis(), MILLISECONDS)); - assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); + assertTrue(millisElapsedSince(t0) >= timeoutMillis()); assertEquals(1, q.size()); assertSame(four, q.poll()); assertNull(q.poll()); |