--- jsr166/src/test/tck/LinkedBlockingDequeTest.java 2015/04/25 04:55:31 1.57 +++ jsr166/src/test/tck/LinkedBlockingDequeTest.java 2015/10/04 18:49:02 1.61 @@ -82,7 +82,7 @@ public class LinkedBlockingDequeTest ext 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) { @@ -147,7 +147,7 @@ public class LinkedBlockingDequeTest ext */ 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()); @@ -186,7 +186,7 @@ public class LinkedBlockingDequeTest ext */ 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 || @@ -216,7 +216,7 @@ public class LinkedBlockingDequeTest ext */ 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()); } @@ -281,7 +281,7 @@ public class LinkedBlockingDequeTest ext } 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()); } @@ -296,7 +296,7 @@ public class LinkedBlockingDequeTest ext } 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()); } @@ -367,7 +367,7 @@ public class LinkedBlockingDequeTest ext */ 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 elements = Arrays.asList(ints); try { @@ -414,7 +414,7 @@ public class LinkedBlockingDequeTest ext 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)); } @@ -424,8 +424,8 @@ public class LinkedBlockingDequeTest ext * 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) {} @@ -435,14 +435,14 @@ public class LinkedBlockingDequeTest ext * 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) {} @@ -513,7 +513,7 @@ public class LinkedBlockingDequeTest ext 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 elements = Arrays.asList(ints); try { @@ -1246,7 +1246,7 @@ public class LinkedBlockingDequeTest ext 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()); } } @@ -1259,7 +1259,7 @@ public class LinkedBlockingDequeTest ext 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(); @@ -1289,7 +1289,7 @@ public class LinkedBlockingDequeTest ext 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)); } @@ -1301,7 +1301,7 @@ public class LinkedBlockingDequeTest ext 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(); @@ -1320,7 +1320,8 @@ public class LinkedBlockingDequeTest ext public void realRun() throws InterruptedException { 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(); @@ -1458,7 +1459,7 @@ public class LinkedBlockingDequeTest ext assertTrue(changed); assertTrue(q.containsAll(p)); - assertEquals(SIZE-i, q.size()); + assertEquals(SIZE - i, q.size()); p.remove(); } } @@ -1471,7 +1472,7 @@ public class LinkedBlockingDequeTest ext 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)); @@ -1670,23 +1671,23 @@ public class LinkedBlockingDequeTest ext final LinkedBlockingDeque q = new LinkedBlockingDeque(2); q.add(one); q.add(two); - ExecutorService executor = Executors.newFixedThreadPool(2); final CheckedBarrier threadsStarted = new CheckedBarrier(2); - 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); + 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()); + }}); + } } /** @@ -1695,22 +1696,22 @@ public class LinkedBlockingDequeTest ext public void testPollInExecutor() { final LinkedBlockingDeque q = new LinkedBlockingDeque(2); 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)); - checkEmpty(q); - }}); - - 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)); + checkEmpty(q); + }}); + + executor.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { + threadsStarted.await(); + q.put(one); + }}); + } } /** @@ -1762,7 +1763,7 @@ public class LinkedBlockingDequeTest ext 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(); @@ -1787,7 +1788,7 @@ public class LinkedBlockingDequeTest ext 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);