diff options
Diffstat (limited to 'jsr166-tests/src/test/java/jsr166/LinkedBlockingDequeTest.java')
-rw-r--r-- | jsr166-tests/src/test/java/jsr166/LinkedBlockingDequeTest.java | 172 |
1 files changed, 83 insertions, 89 deletions
diff --git a/jsr166-tests/src/test/java/jsr166/LinkedBlockingDequeTest.java b/jsr166-tests/src/test/java/jsr166/LinkedBlockingDequeTest.java index 789373d3b2..62802bb7fe 100644 --- a/jsr166-tests/src/test/java/jsr166/LinkedBlockingDequeTest.java +++ b/jsr166-tests/src/test/java/jsr166/LinkedBlockingDequeTest.java @@ -26,24 +26,25 @@ import junit.framework.Test; public class LinkedBlockingDequeTest extends JSR166TestCase { - public static class Unbounded extends BlockingQueueTest { - protected BlockingQueue emptyCollection() { - return new LinkedBlockingDeque(); - } - } - - public static class Bounded extends BlockingQueueTest { - protected BlockingQueue emptyCollection() { - return new LinkedBlockingDeque(SIZE); - } - } - - // android-note: Removed because the CTS runner does a bad job of - // retrying tests that have suite() declarations. + // android-note: These tests have been moved into their own separate + // classes to work around CTS issues. + // + // public static class Unbounded extends BlockingQueueTest { + // protected BlockingQueue emptyCollection() { + // return new LinkedBlockingDeque(); + // } + // } + // + // public static class Bounded extends BlockingQueueTest { + // protected BlockingQueue emptyCollection() { + // return new LinkedBlockingDeque(SIZE); + // } + // } // // public static void main(String[] args) { // main(suite(), args); // } + // // public static Test suite() { // return newTestSuite(LinkedBlockingDequeTest.class, // new Unbounded().testSuite(), @@ -86,7 +87,7 @@ public class LinkedBlockingDequeTest extends JSR166TestCase { public void testSize() { LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(SIZE - i, q.size()); + assertEquals(SIZE-i, q.size()); q.removeFirst(); } for (int i = 0; i < SIZE; ++i) { @@ -151,7 +152,7 @@ public class LinkedBlockingDequeTest extends JSR166TestCase { */ public void testPollLast() { LinkedBlockingDeque q = populatedDeque(SIZE); - for (int i = SIZE - 1; i >= 0; --i) { + for (int i = SIZE-1; i >= 0; --i) { assertEquals(i, q.pollLast()); } assertNull(q.pollLast()); @@ -190,7 +191,7 @@ public class LinkedBlockingDequeTest extends JSR166TestCase { */ public void testPeekLast() { LinkedBlockingDeque q = populatedDeque(SIZE); - for (int i = SIZE - 1; i >= 0; --i) { + for (int i = SIZE-1; i >= 0; --i) { assertEquals(i, q.peekLast()); assertEquals(i, q.pollLast()); assertTrue(q.peekLast() == null || @@ -220,7 +221,7 @@ public class LinkedBlockingDequeTest extends JSR166TestCase { */ public void testLastElement() { LinkedBlockingDeque q = populatedDeque(SIZE); - for (int i = SIZE - 1; i >= 0; --i) { + for (int i = SIZE-1; i >= 0; --i) { assertEquals(i, q.getLast()); assertEquals(i, q.pollLast()); } @@ -285,7 +286,7 @@ public class LinkedBlockingDequeTest extends JSR166TestCase { } for (int i = 0; i < SIZE; i += 2) { assertTrue(q.removeFirstOccurrence(new Integer(i))); - assertFalse(q.removeFirstOccurrence(new Integer(i + 1))); + assertFalse(q.removeFirstOccurrence(new Integer(i+1))); } assertTrue(q.isEmpty()); } @@ -300,7 +301,7 @@ public class LinkedBlockingDequeTest extends JSR166TestCase { } for (int i = 0; i < SIZE; i += 2) { assertTrue(q.removeLastOccurrence(new Integer(i))); - assertFalse(q.removeLastOccurrence(new Integer(i + 1))); + assertFalse(q.removeLastOccurrence(new Integer(i+1))); } assertTrue(q.isEmpty()); } @@ -371,7 +372,7 @@ public class LinkedBlockingDequeTest extends JSR166TestCase { */ public void testConstructor5() { 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 { @@ -418,7 +419,7 @@ public class LinkedBlockingDequeTest extends JSR166TestCase { assertEquals(i, q.remove()); } for (int i = 0; i < SIZE; ++i) { - assertEquals(SIZE - i, q.remainingCapacity()); + assertEquals(SIZE-i, q.remainingCapacity()); assertEquals(SIZE, q.size() + q.remainingCapacity()); assertTrue(q.add(i)); } @@ -428,8 +429,8 @@ public class LinkedBlockingDequeTest extends JSR166TestCase { * push(null) throws NPE */ public void testPushNull() { - LinkedBlockingDeque q = new LinkedBlockingDeque(1); try { + LinkedBlockingDeque q = new LinkedBlockingDeque(1); q.push(null); shouldThrow(); } catch (NullPointerException success) {} @@ -439,14 +440,14 @@ public class LinkedBlockingDequeTest extends JSR166TestCase { * push succeeds if not full; throws ISE if full */ public void testPush() { - LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); - for (int i = 0; i < SIZE; ++i) { - Integer x = new Integer(i); - q.push(x); - assertEquals(x, q.peek()); - } - assertEquals(0, q.remainingCapacity()); try { + LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); + for (int i = 0; i < SIZE; ++i) { + Integer x = new Integer(i); + q.push(x); + assertEquals(x, q.peek()); + } + assertEquals(0, q.remainingCapacity()); q.push(new Integer(SIZE)); shouldThrow(); } catch (IllegalStateException success) {} @@ -517,7 +518,7 @@ public class LinkedBlockingDequeTest extends JSR166TestCase { public void testAddAll3() { LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE - 1; ++i) + for (int i = 0; i < SIZE-1; ++i) ints[i] = new Integer(i); Collection<Integer> elements = Arrays.asList(ints); try { @@ -755,23 +756,25 @@ public class LinkedBlockingDequeTest 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) { + 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); + assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); } }}); aboutToWait.await(); - waitForThreadToEnterWaitState(t, LONG_DELAY_MS); + waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); t.interrupt(); - awaitTermination(t); + awaitTermination(t, MEDIUM_DELAY_MS); checkEmpty(q); } @@ -1052,18 +1055,17 @@ public class LinkedBlockingDequeTest extends JSR166TestCase { * returning timeout status */ public void testInterruptedTimedPollFirst() throws InterruptedException { - final LinkedBlockingDeque q = populatedDeque(SIZE); final CountDownLatch pleaseInterrupt = new CountDownLatch(1); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { - long startTime = System.nanoTime(); + LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { assertEquals(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS)); } Thread.currentThread().interrupt(); try { - q.pollFirst(LONG_DELAY_MS, MILLISECONDS); + q.pollFirst(SMALL_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (InterruptedException success) {} assertFalse(Thread.interrupted()); @@ -1074,7 +1076,6 @@ public class LinkedBlockingDequeTest extends JSR166TestCase { shouldThrow(); } catch (InterruptedException success) {} assertFalse(Thread.interrupted()); - assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); }}); await(pleaseInterrupt); @@ -1250,7 +1251,7 @@ public class LinkedBlockingDequeTest extends JSR166TestCase { public void testTakeLast() throws InterruptedException { LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(SIZE - i - 1, q.takeLast()); + assertEquals(SIZE-i-1, q.takeLast()); } } @@ -1263,7 +1264,7 @@ public class LinkedBlockingDequeTest extends JSR166TestCase { Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { for (int i = 0; i < SIZE; ++i) { - assertEquals(SIZE - i - 1, q.takeLast()); + assertEquals(SIZE-i-1, q.takeLast()); } Thread.currentThread().interrupt(); @@ -1293,7 +1294,7 @@ public class LinkedBlockingDequeTest extends JSR166TestCase { public void testTimedPollLast0() throws InterruptedException { LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(SIZE - i - 1, q.pollLast(0, MILLISECONDS)); + assertEquals(SIZE-i-1, q.pollLast(0, MILLISECONDS)); } assertNull(q.pollLast(0, MILLISECONDS)); } @@ -1305,7 +1306,7 @@ public class LinkedBlockingDequeTest extends JSR166TestCase { LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { long startTime = System.nanoTime(); - assertEquals(SIZE - i - 1, q.pollLast(LONG_DELAY_MS, MILLISECONDS)); + assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS)); assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); } long startTime = System.nanoTime(); @@ -1319,14 +1320,12 @@ public class LinkedBlockingDequeTest extends JSR166TestCase { * returning timeout status */ public void testInterruptedTimedPollLast() throws InterruptedException { - final LinkedBlockingDeque q = populatedDeque(SIZE); final CountDownLatch pleaseInterrupt = new CountDownLatch(1); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { - long startTime = System.nanoTime(); + LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(SIZE - i - 1, - q.pollLast(LONG_DELAY_MS, MILLISECONDS)); + assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS)); } Thread.currentThread().interrupt(); @@ -1342,15 +1341,12 @@ public class LinkedBlockingDequeTest extends JSR166TestCase { shouldThrow(); } catch (InterruptedException success) {} assertFalse(Thread.interrupted()); - - assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); }}); await(pleaseInterrupt); assertThreadStaysAlive(t); t.interrupt(); awaitTermination(t); - checkEmpty(q); } /** @@ -1383,8 +1379,6 @@ public class LinkedBlockingDequeTest extends JSR166TestCase { shouldThrow(); } catch (InterruptedException success) {} assertFalse(Thread.interrupted()); - - assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); }}); barrier.await(); @@ -1469,7 +1463,7 @@ public class LinkedBlockingDequeTest extends JSR166TestCase { assertTrue(changed); assertTrue(q.containsAll(p)); - assertEquals(SIZE - i, q.size()); + assertEquals(SIZE-i, q.size()); p.remove(); } } @@ -1482,7 +1476,7 @@ public class LinkedBlockingDequeTest extends JSR166TestCase { LinkedBlockingDeque q = populatedDeque(SIZE); LinkedBlockingDeque p = populatedDeque(i); assertTrue(q.removeAll(p)); - assertEquals(SIZE - i, q.size()); + assertEquals(SIZE-i, q.size()); for (int j = 0; j < i; ++j) { Integer x = (Integer)(p.remove()); assertFalse(q.contains(x)); @@ -1681,23 +1675,23 @@ public class LinkedBlockingDequeTest extends JSR166TestCase { final LinkedBlockingDeque q = new LinkedBlockingDeque(2); q.add(one); q.add(two); + 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(three)); - threadsStarted.await(); - assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); - assertEquals(0, q.remainingCapacity()); - }}); - - executor.execute(new CheckedRunnable() { - public void realRun() throws InterruptedException { - threadsStarted.await(); - assertSame(one, q.take()); - }}); - } + executor.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { + assertFalse(q.offer(three)); + threadsStarted.await(); + assertTrue(q.offer(three, 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); } /** @@ -1706,22 +1700,22 @@ public class LinkedBlockingDequeTest extends JSR166TestCase { public void testPollInExecutor() { final LinkedBlockingDeque q = new LinkedBlockingDeque(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 { - 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); - }}); - } + 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)); + checkEmpty(q); + }}); + + executor.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { + threadsStarted.await(); + q.put(one); + }}); + + joinPool(executor); } /** @@ -1773,7 +1767,7 @@ public class LinkedBlockingDequeTest extends JSR166TestCase { final LinkedBlockingDeque q = populatedDeque(SIZE); Thread t = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { - q.put(new Integer(SIZE + 1)); + q.put(new Integer(SIZE+1)); }}); t.start(); @@ -1798,7 +1792,7 @@ public class LinkedBlockingDequeTest extends JSR166TestCase { q.drainTo(l, i); int k = (i < SIZE) ? i : SIZE; assertEquals(k, l.size()); - assertEquals(SIZE - k, q.size()); + assertEquals(SIZE-k, q.size()); for (int j = 0; j < k; ++j) assertEquals(l.get(j), new Integer(j)); do {} while (q.poll() != null); |