--- jsr166/src/test/tck/PriorityBlockingQueueTest.java 2015/05/24 01:42:14 1.64 +++ jsr166/src/test/tck/PriorityBlockingQueueTest.java 2017/05/14 01:30:34 1.76 @@ -42,9 +42,17 @@ public class PriorityBlockingQueueTest e } public static Test suite() { + class Implementation implements CollectionImplementation { + public Class klazz() { return PriorityBlockingQueue.class; } + public Collection emptyCollection() { return new PriorityBlockingQueue(); } + public Object makeElement(int i) { return i; } + public boolean isConcurrent() { return true; } + public boolean permitsNulls() { return false; } + } return newTestSuite(PriorityBlockingQueueTest.class, new Generic().testSuite(), - new InitialCapacity().testSuite()); + new InitialCapacity().testSuite(), + CollectionTest.testSuite(new Implementation())); } /** Sample Comparator */ @@ -56,9 +64,9 @@ public class PriorityBlockingQueueTest e /** * Returns a new queue of given size containing consecutive - * Integers 0 ... n. + * Integers 0 ... n - 1. */ - private PriorityBlockingQueue populatedQueue(int n) { + private static PriorityBlockingQueue populatedQueue(int n) { PriorityBlockingQueue q = new PriorityBlockingQueue(n); assertTrue(q.isEmpty()); @@ -69,6 +77,7 @@ public class PriorityBlockingQueueTest e assertFalse(q.isEmpty()); assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); assertEquals(n, q.size()); + assertEquals((Integer) 0, q.peek()); return q; } @@ -200,9 +209,12 @@ public class PriorityBlockingQueueTest e PriorityBlockingQueue q = new PriorityBlockingQueue(1); try { q.offer(new Object()); - q.offer(new Object()); shouldThrow(); - } catch (ClassCastException success) {} + } catch (ClassCastException success) { + assertTrue(q.isEmpty()); + assertEquals(0, q.size()); + assertNull(q.poll()); + } } /** @@ -321,9 +333,7 @@ public class PriorityBlockingQueueTest e final CountDownLatch pleaseInterrupt = new CountDownLatch(1); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { - for (int i = 0; i < SIZE; ++i) { - assertEquals(i, q.take()); - } + for (int i = 0; i < SIZE; i++) assertEquals(i, q.take()); Thread.currentThread().interrupt(); try { @@ -341,7 +351,7 @@ public class PriorityBlockingQueueTest e }}); await(pleaseInterrupt); - assertThreadStaysAlive(t); + assertThreadBlocks(t, Thread.State.WAITING); t.interrupt(); awaitTermination(t); } @@ -390,28 +400,34 @@ public class PriorityBlockingQueueTest e */ public void testInterruptedTimedPoll() throws InterruptedException { final BlockingQueue q = populatedQueue(SIZE); - final CountDownLatch aboutToWait = new CountDownLatch(1); + final CountDownLatch pleaseInterrupt = 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(); + + Thread.currentThread().interrupt(); + try { + q.poll(LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + + pleaseInterrupt.countDown(); try { q.poll(LONG_DELAY_MS, MILLISECONDS); shouldThrow(); - } catch (InterruptedException success) { - assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); - } + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); }}); - aboutToWait.await(); - waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); + await(pleaseInterrupt); + assertThreadBlocks(t, Thread.State.TIMED_WAITING); t.interrupt(); - awaitTermination(t, MEDIUM_DELAY_MS); + awaitTermination(t); } /** @@ -624,22 +640,22 @@ public class PriorityBlockingQueueTest e public void testPollInExecutor() { final PriorityBlockingQueue q = new PriorityBlockingQueue(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); + }}); + } } /**