--- jsr166/src/test/tck/DelayQueueTest.java 2015/02/28 19:59:23 1.70 +++ jsr166/src/test/tck/DelayQueueTest.java 2016/09/15 17:07:16 1.77 @@ -35,7 +35,7 @@ public class DelayQueueTest extends JSR1 } public static void main(String[] args) { - junit.textui.TestRunner.run(suite()); + main(suite(), args); } public static Test suite() { @@ -94,10 +94,8 @@ public class DelayQueueTest extends JSR1 } public boolean equals(Object other) { - return equals((NanoDelay)other); - } - public boolean equals(NanoDelay other) { - return other.trigger == trigger; + return (other instanceof NanoDelay) && + this.trigger == ((NanoDelay)other).trigger; } // suppress [overrides] javac warning @@ -124,7 +122,7 @@ public class DelayQueueTest extends JSR1 private DelayQueue populatedQueue(int n) { DelayQueue q = new DelayQueue(); assertTrue(q.isEmpty()); - for (int i = n-1; i >= 0; i -= 2) + for (int i = n - 1; i >= 0; i -= 2) assertTrue(q.offer(new PDelay(i))); for (int i = (n & 1); i < n; i += 2) assertTrue(q.offer(new PDelay(i))); @@ -156,8 +154,7 @@ public class DelayQueueTest extends JSR1 */ public void testConstructor4() { try { - PDelay[] ints = new PDelay[SIZE]; - new DelayQueue(Arrays.asList(ints)); + new DelayQueue(Arrays.asList(new PDelay[SIZE])); shouldThrow(); } catch (NullPointerException success) {} } @@ -166,11 +163,11 @@ public class DelayQueueTest extends JSR1 * Initializing from Collection with some null elements throws NPE */ public void testConstructor5() { + PDelay[] a = new PDelay[SIZE]; + for (int i = 0; i < SIZE - 1; ++i) + a[i] = new PDelay(i); try { - PDelay[] ints = new PDelay[SIZE]; - for (int i = 0; i < SIZE-1; ++i) - ints[i] = new PDelay(i); - new DelayQueue(Arrays.asList(ints)); + new DelayQueue(Arrays.asList(a)); shouldThrow(); } catch (NullPointerException success) {} } @@ -209,7 +206,7 @@ public class DelayQueueTest extends JSR1 BlockingQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); - assertEquals(SIZE-i, q.size()); + assertEquals(SIZE - i, q.size()); assertTrue(q.remove() instanceof PDelay); } for (int i = 0; i < SIZE; ++i) { @@ -243,8 +240,8 @@ public class DelayQueueTest extends JSR1 * addAll(this) throws IAE */ public void testAddAllSelf() { + DelayQueue q = populatedQueue(SIZE); try { - DelayQueue q = populatedQueue(SIZE); q.addAll(q); shouldThrow(); } catch (IllegalArgumentException success) {} @@ -255,12 +252,12 @@ public class DelayQueueTest extends JSR1 * possibly adding some elements */ public void testAddAll3() { + DelayQueue q = new DelayQueue(); + PDelay[] a = new PDelay[SIZE]; + for (int i = 0; i < SIZE - 1; ++i) + a[i] = new PDelay(i); try { - DelayQueue q = new DelayQueue(); - PDelay[] ints = new PDelay[SIZE]; - for (int i = 0; i < SIZE-1; ++i) - ints[i] = new PDelay(i); - q.addAll(Arrays.asList(ints)); + q.addAll(Arrays.asList(a)); shouldThrow(); } catch (NullPointerException success) {} } @@ -271,7 +268,7 @@ public class DelayQueueTest extends JSR1 public void testAddAll5() { PDelay[] empty = new PDelay[0]; PDelay[] ints = new PDelay[SIZE]; - for (int i = SIZE-1; i >= 0; --i) + for (int i = SIZE - 1; i >= 0; --i) ints[i] = new PDelay(i); DelayQueue q = new DelayQueue(); assertFalse(q.addAll(Arrays.asList(empty))); @@ -413,11 +410,13 @@ public class DelayQueueTest extends JSR1 */ public void testInterruptedTimedPoll() throws InterruptedException { final CountDownLatch pleaseInterrupt = new CountDownLatch(1); + final DelayQueue q = populatedQueue(SIZE); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { - DelayQueue q = populatedQueue(SIZE); + long startTime = System.nanoTime(); for (int i = 0; i < SIZE; ++i) { - assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS))); + assertEquals(new PDelay(i), + ((PDelay)q.poll(LONG_DELAY_MS, MILLISECONDS))); } Thread.currentThread().interrupt(); @@ -433,12 +432,14 @@ public class DelayQueueTest extends JSR1 shouldThrow(); } catch (InterruptedException success) {} assertFalse(Thread.interrupted()); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); }}); await(pleaseInterrupt); assertThreadStaysAlive(t); t.interrupt(); awaitTermination(t); + checkEmpty(q); } /** @@ -543,7 +544,7 @@ public class DelayQueueTest extends JSR1 assertTrue(changed); assertTrue(q.containsAll(p)); - assertEquals(SIZE-i, q.size()); + assertEquals(SIZE - i, q.size()); p.remove(); } } @@ -556,7 +557,7 @@ public class DelayQueueTest extends JSR1 DelayQueue q = populatedQueue(SIZE); DelayQueue p = populatedQueue(i); assertTrue(q.removeAll(p)); - assertEquals(SIZE-i, q.size()); + assertEquals(SIZE - i, q.size()); for (int j = 0; j < i; ++j) { PDelay x = (PDelay)(p.remove()); assertFalse(q.contains(x)); @@ -654,22 +655,22 @@ public class DelayQueueTest extends JSR1 public void testPollInExecutor() { final DelayQueue q = new DelayQueue(); 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(); - assertNotNull(q.poll(LONG_DELAY_MS, MILLISECONDS)); - checkEmpty(q); - }}); - - executor.execute(new CheckedRunnable() { - public void realRun() throws InterruptedException { - threadsStarted.await(); - q.put(new PDelay(1)); - }}); - - 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(); + assertNotNull(q.poll(LONG_DELAY_MS, MILLISECONDS)); + checkEmpty(q); + }}); + + executor.execute(new CheckedRunnable() { + public void realRun() throws InterruptedException { + threadsStarted.await(); + q.put(new PDelay(1)); + }}); + } } /** @@ -754,7 +755,7 @@ public class DelayQueueTest extends JSR1 final DelayQueue q = populatedQueue(SIZE); Thread t = new Thread(new CheckedRunnable() { public void realRun() { - q.put(new PDelay(SIZE+1)); + q.put(new PDelay(SIZE + 1)); }}); t.start(); @@ -774,7 +775,7 @@ public class DelayQueueTest extends JSR1 ArrayList l = new ArrayList(); q.drainTo(l, i); int k = (i < SIZE) ? i : SIZE; - assertEquals(SIZE-k, q.size()); + assertEquals(SIZE - k, q.size()); assertEquals(k, l.size()); } }