--- jsr166/src/test/tck/DelayQueueTest.java 2014/12/31 20:17:39 1.66 +++ jsr166/src/test/tck/DelayQueueTest.java 2021/01/26 13:33:05 1.94 @@ -35,31 +35,32 @@ public class DelayQueueTest extends JSR1 } public static void main(String[] args) { - junit.textui.TestRunner.run(suite()); + main(suite(), args); } public static Test suite() { + class Implementation implements CollectionImplementation { + public Class klazz() { return DelayQueue.class; } + public Collection emptyCollection() { return new DelayQueue(); } + public Object makeElement(int i) { return new PDelay(i); } + public boolean isConcurrent() { return true; } + public boolean permitsNulls() { return false; } + } return newTestSuite(DelayQueueTest.class, - new Generic().testSuite()); + new Generic().testSuite(), + CollectionTest.testSuite(new Implementation())); } - private static final int NOCAP = Integer.MAX_VALUE; - /** - * A delayed implementation for testing. - * Most tests use Pseudodelays, where delays are all elapsed + * A fake Delayed implementation for testing. + * Most tests use PDelays, where delays are all elapsed * (so, no blocking solely for delays) but are still ordered */ static class PDelay implements Delayed { - int pseudodelay; - PDelay(int i) { pseudodelay = i; } - public int compareTo(PDelay other) { - int a = this.pseudodelay; - int b = other.pseudodelay; - return (a < b) ? -1 : (a > b) ? 1 : 0; - } + final int pseudodelay; + PDelay(int pseudodelay) { this.pseudodelay = pseudodelay; } public int compareTo(Delayed y) { - return compareTo((PDelay)y); + return Integer.compare(this.pseudodelay, ((PDelay)y).pseudodelay); } public boolean equals(Object other) { return (other instanceof PDelay) && @@ -68,7 +69,7 @@ public class DelayQueueTest extends JSR1 // suppress [overrides] javac warning public int hashCode() { return pseudodelay; } public long getDelay(TimeUnit ignore) { - return Integer.MIN_VALUE + pseudodelay; + return (long) Integer.MIN_VALUE + pseudodelay; } public String toString() { return String.valueOf(pseudodelay); @@ -79,27 +80,18 @@ public class DelayQueueTest extends JSR1 * Delayed implementation that actually delays */ static class NanoDelay implements Delayed { - long trigger; + final long trigger; NanoDelay(long i) { trigger = System.nanoTime() + i; } - public int compareTo(NanoDelay y) { - long i = trigger; - long j = y.trigger; - if (i < j) return -1; - if (i > j) return 1; - return 0; - } public int compareTo(Delayed y) { - return compareTo((NanoDelay)y); + return Long.compare(trigger, ((NanoDelay)y).trigger); } 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 @@ -121,18 +113,19 @@ public class DelayQueueTest extends JSR1 /** * Returns a new queue of given size containing consecutive - * PDelays 0 ... n. + * PDelays 0 ... n - 1. */ - private DelayQueue populatedQueue(int n) { - DelayQueue q = new DelayQueue(); + private static 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))); assertFalse(q.isEmpty()); - assertEquals(NOCAP, q.remainingCapacity()); - assertEquals(n, q.size()); + mustEqual(Integer.MAX_VALUE, q.remainingCapacity()); + mustEqual(n, q.size()); + mustEqual(new PDelay(0), q.peek()); return q; } @@ -140,7 +133,7 @@ public class DelayQueueTest extends JSR1 * A new queue has unbounded capacity */ public void testConstructor1() { - assertEquals(NOCAP, new DelayQueue().remainingCapacity()); + mustEqual(Integer.MAX_VALUE, new DelayQueue().remainingCapacity()); } /** @@ -148,7 +141,7 @@ public class DelayQueueTest extends JSR1 */ public void testConstructor3() { try { - DelayQueue q = new DelayQueue(null); + new DelayQueue(null); shouldThrow(); } catch (NullPointerException success) {} } @@ -158,8 +151,7 @@ public class DelayQueueTest extends JSR1 */ public void testConstructor4() { try { - PDelay[] ints = new PDelay[SIZE]; - DelayQueue q = new DelayQueue(Arrays.asList(ints)); + new DelayQueue(Arrays.asList(new PDelay[SIZE])); shouldThrow(); } catch (NullPointerException success) {} } @@ -168,11 +160,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); - DelayQueue q = new DelayQueue(Arrays.asList(ints)); + new DelayQueue(Arrays.asList(a)); shouldThrow(); } catch (NullPointerException success) {} } @@ -181,21 +173,21 @@ public class DelayQueueTest extends JSR1 * Queue contains all elements of collection used to initialize */ public void testConstructor6() { - PDelay[] ints = new PDelay[SIZE]; + PDelay[] items = new PDelay[SIZE]; for (int i = 0; i < SIZE; ++i) - ints[i] = new PDelay(i); - DelayQueue q = new DelayQueue(Arrays.asList(ints)); + items[i] = new PDelay(i); + DelayQueue q = new DelayQueue(Arrays.asList(items)); for (int i = 0; i < SIZE; ++i) - assertEquals(ints[i], q.poll()); + mustEqual(items[i], q.poll()); } /** * isEmpty is true before add, false after */ public void testEmpty() { - DelayQueue q = new DelayQueue(); + DelayQueue q = new DelayQueue(); assertTrue(q.isEmpty()); - assertEquals(NOCAP, q.remainingCapacity()); + mustEqual(Integer.MAX_VALUE, q.remainingCapacity()); q.add(new PDelay(1)); assertFalse(q.isEmpty()); q.add(new PDelay(2)); @@ -205,20 +197,19 @@ public class DelayQueueTest extends JSR1 } /** - * remainingCapacity does not change when elements added or removed, - * but size does + * remainingCapacity() always returns Integer.MAX_VALUE */ public void testRemainingCapacity() { - DelayQueue q = populatedQueue(SIZE); + BlockingQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(NOCAP, q.remainingCapacity()); - assertEquals(SIZE-i, q.size()); - q.remove(); + mustEqual(Integer.MAX_VALUE, q.remainingCapacity()); + mustEqual(SIZE - i, q.size()); + assertTrue(q.remove() instanceof PDelay); } for (int i = 0; i < SIZE; ++i) { - assertEquals(NOCAP, q.remainingCapacity()); - assertEquals(i, q.size()); - q.add(new PDelay(i)); + mustEqual(Integer.MAX_VALUE, q.remainingCapacity()); + mustEqual(i, q.size()); + assertTrue(q.add(new PDelay(i))); } } @@ -226,7 +217,7 @@ public class DelayQueueTest extends JSR1 * offer non-null succeeds */ public void testOffer() { - DelayQueue q = new DelayQueue(); + DelayQueue q = new DelayQueue(); assertTrue(q.offer(new PDelay(0))); assertTrue(q.offer(new PDelay(1))); } @@ -235,19 +226,19 @@ public class DelayQueueTest extends JSR1 * add succeeds */ public void testAdd() { - DelayQueue q = new DelayQueue(); + DelayQueue q = new DelayQueue(); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, q.size()); + mustEqual(i, q.size()); assertTrue(q.add(new PDelay(i))); } } /** - * addAll(this) throws IAE + * addAll(this) throws IllegalArgumentException */ public void testAddAllSelf() { + DelayQueue q = populatedQueue(SIZE); try { - DelayQueue q = populatedQueue(SIZE); q.addAll(q); shouldThrow(); } catch (IllegalArgumentException success) {} @@ -258,12 +249,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) {} } @@ -273,34 +264,34 @@ 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) - ints[i] = new PDelay(i); - DelayQueue q = new DelayQueue(); + PDelay[] items = new PDelay[SIZE]; + for (int i = SIZE - 1; i >= 0; --i) + items[i] = new PDelay(i); + DelayQueue q = new DelayQueue(); assertFalse(q.addAll(Arrays.asList(empty))); - assertTrue(q.addAll(Arrays.asList(ints))); + assertTrue(q.addAll(Arrays.asList(items))); for (int i = 0; i < SIZE; ++i) - assertEquals(ints[i], q.poll()); + mustEqual(items[i], q.poll()); } /** * all elements successfully put are contained */ public void testPut() { - DelayQueue q = new DelayQueue(); + DelayQueue q = new DelayQueue(); for (int i = 0; i < SIZE; ++i) { PDelay x = new PDelay(i); q.put(x); assertTrue(q.contains(x)); } - assertEquals(SIZE, q.size()); + mustEqual(SIZE, q.size()); } /** * put doesn't block waiting for take */ public void testPutWithTake() throws InterruptedException { - final DelayQueue q = new DelayQueue(); + final DelayQueue q = new DelayQueue(); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() { q.put(new PDelay(0)); @@ -310,14 +301,14 @@ public class DelayQueueTest extends JSR1 }}); awaitTermination(t); - assertEquals(4, q.size()); + mustEqual(4, q.size()); } /** - * timed offer does not time out + * Queue is unbounded, so timed offer never times out */ public void testTimedOffer() throws InterruptedException { - final DelayQueue q = new DelayQueue(); + final DelayQueue q = new DelayQueue(); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { q.put(new PDelay(0)); @@ -333,9 +324,9 @@ public class DelayQueueTest extends JSR1 * take retrieves elements in priority order */ public void testTake() throws InterruptedException { - DelayQueue q = populatedQueue(SIZE); + DelayQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(new PDelay(i), ((PDelay)q.take())); + mustEqual(new PDelay(i), q.take()); } } @@ -343,13 +334,12 @@ public class DelayQueueTest extends JSR1 * Take removes existing elements until empty, then blocks interruptibly */ public void testBlockingTake() throws InterruptedException { - final DelayQueue q = populatedQueue(SIZE); + final DelayQueue q = populatedQueue(SIZE); final CountDownLatch pleaseInterrupt = new CountDownLatch(1); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { - for (int i = 0; i < SIZE; ++i) { - assertEquals(new PDelay(i), ((PDelay)q.take())); - } + for (int i = 0; i < SIZE; i++) + mustEqual(new PDelay(i), q.take()); Thread.currentThread().interrupt(); try { @@ -367,7 +357,7 @@ public class DelayQueueTest extends JSR1 }}); await(pleaseInterrupt); - assertThreadStaysAlive(t); + if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); t.interrupt(); awaitTermination(t); } @@ -376,9 +366,9 @@ public class DelayQueueTest extends JSR1 * poll succeeds unless empty */ public void testPoll() { - DelayQueue q = populatedQueue(SIZE); + DelayQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(new PDelay(i), ((PDelay)q.poll())); + mustEqual(new PDelay(i), q.poll()); } assertNull(q.poll()); } @@ -387,9 +377,9 @@ public class DelayQueueTest extends JSR1 * timed poll with zero timeout succeeds when non-empty, else times out */ public void testTimedPoll0() throws InterruptedException { - DelayQueue q = populatedQueue(SIZE); + DelayQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(new PDelay(i), ((PDelay)q.poll(0, MILLISECONDS))); + mustEqual(new PDelay(i), q.poll(0, MILLISECONDS)); } assertNull(q.poll(0, MILLISECONDS)); } @@ -398,10 +388,10 @@ public class DelayQueueTest extends JSR1 * timed poll with nonzero timeout succeeds when non-empty, else times out */ public void testTimedPoll() throws InterruptedException { - DelayQueue q = populatedQueue(SIZE); + DelayQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { long startTime = System.nanoTime(); - assertEquals(new PDelay(i), ((PDelay)q.poll(LONG_DELAY_MS, MILLISECONDS))); + mustEqual(new PDelay(i), q.poll(LONG_DELAY_MS, MILLISECONDS)); assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); } long startTime = System.nanoTime(); @@ -416,42 +406,43 @@ 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); - for (int i = 0; i < SIZE; ++i) { - assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS))); - } + for (int i = 0; i < SIZE; i++) + mustEqual(new PDelay(i), + q.poll(LONG_DELAY_MS, MILLISECONDS)); Thread.currentThread().interrupt(); try { - q.poll(LONG_DELAY_MS, MILLISECONDS); + q.poll(randomTimeout(), randomTimeUnit()); shouldThrow(); } catch (InterruptedException success) {} assertFalse(Thread.interrupted()); pleaseInterrupt.countDown(); try { - q.poll(LONG_DELAY_MS, MILLISECONDS); + q.poll(LONGER_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (InterruptedException success) {} assertFalse(Thread.interrupted()); }}); await(pleaseInterrupt); - assertThreadStaysAlive(t); + if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); t.interrupt(); awaitTermination(t); + checkEmpty(q); } /** * peek returns next element, or null if empty */ public void testPeek() { - DelayQueue q = populatedQueue(SIZE); + DelayQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(new PDelay(i), ((PDelay)q.peek())); - assertEquals(new PDelay(i), ((PDelay)q.poll())); + mustEqual(new PDelay(i), q.peek()); + mustEqual(new PDelay(i), q.poll()); if (q.isEmpty()) assertNull(q.peek()); else @@ -464,9 +455,9 @@ public class DelayQueueTest extends JSR1 * element returns next element, or throws NSEE if empty */ public void testElement() { - DelayQueue q = populatedQueue(SIZE); + DelayQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(new PDelay(i), ((PDelay)q.element())); + mustEqual(new PDelay(i), q.element()); q.poll(); } try { @@ -479,9 +470,9 @@ public class DelayQueueTest extends JSR1 * remove removes next element, or throws NSEE if empty */ public void testRemove() { - DelayQueue q = populatedQueue(SIZE); + DelayQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(new PDelay(i), ((PDelay)q.remove())); + mustEqual(new PDelay(i), q.remove()); } try { q.remove(); @@ -493,7 +484,7 @@ public class DelayQueueTest extends JSR1 * contains(x) reports true when elements added but not yet removed */ public void testContains() { - DelayQueue q = populatedQueue(SIZE); + DelayQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { assertTrue(q.contains(new PDelay(i))); q.poll(); @@ -505,11 +496,11 @@ public class DelayQueueTest extends JSR1 * clear removes all elements */ public void testClear() { - DelayQueue q = populatedQueue(SIZE); + DelayQueue q = populatedQueue(SIZE); q.clear(); assertTrue(q.isEmpty()); - assertEquals(0, q.size()); - assertEquals(NOCAP, q.remainingCapacity()); + mustEqual(0, q.size()); + mustEqual(Integer.MAX_VALUE, q.remainingCapacity()); PDelay x = new PDelay(1); q.add(x); assertFalse(q.isEmpty()); @@ -522,8 +513,8 @@ public class DelayQueueTest extends JSR1 * containsAll(c) is true when c contains a subset of elements */ public void testContainsAll() { - DelayQueue q = populatedQueue(SIZE); - DelayQueue p = new DelayQueue(); + DelayQueue q = populatedQueue(SIZE); + DelayQueue p = new DelayQueue(); for (int i = 0; i < SIZE; ++i) { assertTrue(q.containsAll(p)); assertFalse(p.containsAll(q)); @@ -536,8 +527,8 @@ public class DelayQueueTest extends JSR1 * retainAll(c) retains only those elements of c and reports true if changed */ public void testRetainAll() { - DelayQueue q = populatedQueue(SIZE); - DelayQueue p = populatedQueue(SIZE); + DelayQueue q = populatedQueue(SIZE); + DelayQueue p = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { boolean changed = q.retainAll(p); if (i == 0) @@ -546,7 +537,7 @@ public class DelayQueueTest extends JSR1 assertTrue(changed); assertTrue(q.containsAll(p)); - assertEquals(SIZE-i, q.size()); + mustEqual(SIZE - i, q.size()); p.remove(); } } @@ -556,13 +547,12 @@ public class DelayQueueTest extends JSR1 */ public void testRemoveAll() { for (int i = 1; i < SIZE; ++i) { - DelayQueue q = populatedQueue(SIZE); - DelayQueue p = populatedQueue(i); + DelayQueue q = populatedQueue(SIZE); + DelayQueue p = populatedQueue(i); assertTrue(q.removeAll(p)); - assertEquals(SIZE-i, q.size()); + mustEqual(SIZE - i, q.size()); for (int j = 0; j < i; ++j) { - PDelay x = (PDelay)(p.remove()); - assertFalse(q.contains(x)); + assertFalse(q.contains(p.remove())); } } } @@ -571,11 +561,13 @@ public class DelayQueueTest extends JSR1 * toArray contains all elements */ public void testToArray() throws InterruptedException { - DelayQueue q = populatedQueue(SIZE); - Object[] o = q.toArray(); - Arrays.sort(o); - for (int i = 0; i < o.length; i++) - assertSame(o[i], q.take()); + DelayQueue q = populatedQueue(SIZE); + Object[] a = q.toArray(); + assertSame(Object[].class, a.getClass()); + Arrays.sort(a); + for (Object o : a) + assertSame(o, q.take()); + assertTrue(q.isEmpty()); } /** @@ -583,19 +575,20 @@ public class DelayQueueTest extends JSR1 */ public void testToArray2() { DelayQueue q = populatedQueue(SIZE); - PDelay[] ints = new PDelay[SIZE]; - PDelay[] array = q.toArray(ints); - assertSame(ints, array); - Arrays.sort(ints); - for (int i = 0; i < ints.length; i++) - assertSame(ints[i], q.remove()); + PDelay[] items = new PDelay[SIZE]; + PDelay[] array = q.toArray(items); + assertSame(items, array); + Arrays.sort(items); + for (PDelay o : items) + assertSame(o, q.remove()); + assertTrue(q.isEmpty()); } /** * toArray(incompatible array type) throws ArrayStoreException */ public void testToArray1_BadArg() { - DelayQueue q = populatedQueue(SIZE); + DelayQueue q = populatedQueue(SIZE); try { q.toArray(new String[10]); shouldThrow(); @@ -606,30 +599,38 @@ public class DelayQueueTest extends JSR1 * iterator iterates through all elements */ public void testIterator() { - DelayQueue q = populatedQueue(SIZE); + DelayQueue q = populatedQueue(SIZE); int i = 0; - Iterator it = q.iterator(); + Iterator it = q.iterator(); while (it.hasNext()) { assertTrue(q.contains(it.next())); ++i; } - assertEquals(i, SIZE); + mustEqual(i, SIZE); + assertIteratorExhausted(it); + } + + /** + * iterator of empty collection has no elements + */ + public void testEmptyIterator() { + assertIteratorExhausted(new DelayQueue().iterator()); } /** * iterator.remove removes current element */ public void testIteratorRemove() { - final DelayQueue q = new DelayQueue(); + final DelayQueue q = new DelayQueue(); q.add(new PDelay(2)); q.add(new PDelay(1)); q.add(new PDelay(3)); - Iterator it = q.iterator(); + Iterator it = q.iterator(); it.next(); it.remove(); it = q.iterator(); - assertEquals(new PDelay(2), it.next()); - assertEquals(new PDelay(3), it.next()); + mustEqual(new PDelay(2), it.next()); + mustEqual(new PDelay(3), it.next()); assertFalse(it.hasNext()); } @@ -637,7 +638,7 @@ public class DelayQueueTest extends JSR1 * toString contains toStrings of elements */ public void testToString() { - DelayQueue q = populatedQueue(SIZE); + DelayQueue q = populatedQueue(SIZE); String s = q.toString(); for (Object e : q) assertTrue(s.contains(e.toString())); @@ -647,31 +648,31 @@ public class DelayQueueTest extends JSR1 * timed poll transfers elements across Executor tasks */ public void testPollInExecutor() { - final DelayQueue q = new DelayQueue(); + 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)); + }}); + } } /** * Delayed actions do not occur until their delay elapses */ public void testDelay() throws InterruptedException { - DelayQueue q = new DelayQueue(); + DelayQueue q = new DelayQueue<>(); for (int i = 0; i < SIZE; ++i) q.add(new NanoDelay(1000000L * (SIZE - i))); @@ -691,7 +692,7 @@ public class DelayQueueTest extends JSR1 * peek of a non-empty queue returns non-null even if not expired */ public void testPeekDelayed() { - DelayQueue q = new DelayQueue(); + DelayQueue q = new DelayQueue(); q.add(new NanoDelay(Long.MAX_VALUE)); assertNotNull(q.peek()); } @@ -700,7 +701,7 @@ public class DelayQueueTest extends JSR1 * poll of a non-empty queue returns null if no expired elements. */ public void testPollDelayed() { - DelayQueue q = new DelayQueue(); + DelayQueue q = new DelayQueue(); q.add(new NanoDelay(Long.MAX_VALUE)); assertNull(q.poll()); } @@ -709,26 +710,28 @@ public class DelayQueueTest extends JSR1 * timed poll of a non-empty queue returns null if no expired elements. */ public void testTimedPollDelayed() throws InterruptedException { - DelayQueue q = new DelayQueue(); + DelayQueue q = new DelayQueue(); q.add(new NanoDelay(LONG_DELAY_MS * 1000000L)); + long startTime = System.nanoTime(); assertNull(q.poll(timeoutMillis(), MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); } /** * drainTo(c) empties queue into another collection c */ public void testDrainTo() { - DelayQueue q = new DelayQueue(); + DelayQueue q = new DelayQueue(); PDelay[] elems = new PDelay[SIZE]; for (int i = 0; i < SIZE; ++i) { elems[i] = new PDelay(i); q.add(elems[i]); } - ArrayList l = new ArrayList(); + ArrayList l = new ArrayList(); q.drainTo(l); - assertEquals(0, q.size()); + mustEqual(0, q.size()); for (int i = 0; i < SIZE; ++i) - assertEquals(elems[i], l.get(i)); + mustEqual(elems[i], l.get(i)); q.add(elems[0]); q.add(elems[1]); assertFalse(q.isEmpty()); @@ -736,24 +739,24 @@ public class DelayQueueTest extends JSR1 assertTrue(q.contains(elems[1])); l.clear(); q.drainTo(l); - assertEquals(0, q.size()); - assertEquals(2, l.size()); + mustEqual(0, q.size()); + mustEqual(2, l.size()); for (int i = 0; i < 2; ++i) - assertEquals(elems[i], l.get(i)); + mustEqual(elems[i], l.get(i)); } /** * drainTo empties queue */ public void testDrainToWithActivePut() throws InterruptedException { - final DelayQueue q = populatedQueue(SIZE); + 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(); - ArrayList l = new ArrayList(); + ArrayList l = new ArrayList(); q.drainTo(l); assertTrue(l.size() >= SIZE); t.join(); @@ -765,12 +768,12 @@ public class DelayQueueTest extends JSR1 */ public void testDrainToN() { for (int i = 0; i < SIZE + 2; ++i) { - DelayQueue q = populatedQueue(SIZE); - ArrayList l = new ArrayList(); + DelayQueue q = populatedQueue(SIZE); + ArrayList l = new ArrayList(); q.drainTo(l, i); int k = (i < SIZE) ? i : SIZE; - assertEquals(SIZE-k, q.size()); - assertEquals(k, l.size()); + mustEqual(SIZE - k, q.size()); + mustEqual(k, l.size()); } }