--- jsr166/src/test/tck/ArrayBlockingQueueTest.java 2015/05/23 00:53:08 1.66 +++ jsr166/src/test/tck/ArrayBlockingQueueTest.java 2016/10/17 01:52:04 1.73 @@ -41,14 +41,22 @@ public class ArrayBlockingQueueTest exte } public static Test suite() { + class Implementation implements CollectionImplementation { + public Class klazz() { return ArrayBlockingQueue.class; } + public Collection emptyCollection() { return new ArrayBlockingQueue(SIZE, false); } + public Object makeElement(int i) { return i; } + public boolean isConcurrent() { return true; } + public boolean permitsNulls() { return false; } + } return newTestSuite(ArrayBlockingQueueTest.class, new Fair().testSuite(), - new NonFair().testSuite()); + new NonFair().testSuite(), + CollectionTest.testSuite(new Implementation())); } /** * Returns a new queue of given size containing consecutive - * Integers 0 ... n. + * Integers 0 ... n - 1. */ private ArrayBlockingQueue populatedQueue(int n) { ArrayBlockingQueue q = new ArrayBlockingQueue(n); @@ -58,6 +66,7 @@ public class ArrayBlockingQueueTest exte assertFalse(q.isEmpty()); assertEquals(0, q.remainingCapacity()); assertEquals(n, q.size()); + assertEquals((Integer) 0, q.peek()); return q; } @@ -451,25 +460,23 @@ public class ArrayBlockingQueueTest exte 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(MEDIUM_DELAY_MS, MILLISECONDS); + q.poll(LONG_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (InterruptedException success) { - assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); } }}); - aboutToWait.await(); - waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); + await(aboutToWait); + waitForThreadToEnterWaitState(t); t.interrupt(); - awaitTermination(t, MEDIUM_DELAY_MS); + awaitTermination(t); checkEmpty(q); } @@ -629,13 +636,13 @@ public class ArrayBlockingQueueTest exte void checkToArray2(ArrayBlockingQueue q) { int size = q.size(); - Integer[] a1 = size == 0 ? null : new Integer[size-1]; + Integer[] a1 = (size == 0) ? null : new Integer[size - 1]; Integer[] a2 = new Integer[size]; - Integer[] a3 = new Integer[size+2]; + Integer[] a3 = new Integer[size + 2]; if (size > 0) Arrays.fill(a1, 42); Arrays.fill(a2, 42); Arrays.fill(a3, 42); - Integer[] b1 = size == 0 ? null : (Integer[]) q.toArray(a1); + Integer[] b1 = (size == 0) ? null : (Integer[]) q.toArray(a1); Integer[] b2 = (Integer[]) q.toArray(a2); Integer[] b3 = (Integer[]) q.toArray(a3); assertSame(a2, b2); @@ -649,7 +656,7 @@ public class ArrayBlockingQueueTest exte assertSame(b3[i], x); } assertNull(a3[size]); - assertEquals(42, (int) a3[size+1]); + assertEquals(42, (int) a3[size + 1]); if (size > 0) { assertNotSame(a1, b1); assertEquals(size, b1.length); @@ -788,24 +795,24 @@ public class ArrayBlockingQueueTest exte final ArrayBlockingQueue q = new ArrayBlockingQueue(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(); - assertEquals(0, q.remainingCapacity()); - 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(); + assertEquals(0, q.remainingCapacity()); + assertSame(one, q.take()); + }}); + } } /** @@ -814,22 +821,22 @@ public class ArrayBlockingQueueTest exte public void testPollInExecutor() { final ArrayBlockingQueue q = new ArrayBlockingQueue(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); + }}); + } } /**