35 |
|
} |
36 |
|
|
37 |
|
public static void main(String[] args) { |
38 |
< |
junit.textui.TestRunner.run(suite()); |
38 |
> |
main(suite(), args); |
39 |
|
} |
40 |
|
|
41 |
|
public static Test suite() { |
42 |
+ |
class Implementation implements CollectionImplementation { |
43 |
+ |
public Class<?> klazz() { return DelayQueue.class; } |
44 |
+ |
public Collection emptyCollection() { return new DelayQueue(); } |
45 |
+ |
public Object makeElement(int i) { return new PDelay(i); } |
46 |
+ |
public boolean isConcurrent() { return true; } |
47 |
+ |
public boolean permitsNulls() { return false; } |
48 |
+ |
} |
49 |
|
return newTestSuite(DelayQueueTest.class, |
50 |
< |
new Generic().testSuite()); |
50 |
> |
new Generic().testSuite(), |
51 |
> |
CollectionTest.testSuite(new Implementation())); |
52 |
|
} |
53 |
|
|
46 |
– |
private static final int NOCAP = Integer.MAX_VALUE; |
47 |
– |
|
54 |
|
/** |
55 |
|
* A delayed implementation for testing. |
56 |
|
* Most tests use Pseudodelays, where delays are all elapsed |
102 |
|
} |
103 |
|
|
104 |
|
public boolean equals(Object other) { |
105 |
< |
return equals((NanoDelay)other); |
106 |
< |
} |
101 |
< |
public boolean equals(NanoDelay other) { |
102 |
< |
return other.trigger == trigger; |
105 |
> |
return (other instanceof NanoDelay) && |
106 |
> |
this.trigger == ((NanoDelay)other).trigger; |
107 |
|
} |
108 |
|
|
109 |
|
// suppress [overrides] javac warning |
125 |
|
|
126 |
|
/** |
127 |
|
* Returns a new queue of given size containing consecutive |
128 |
< |
* PDelays 0 ... n. |
128 |
> |
* PDelays 0 ... n - 1. |
129 |
|
*/ |
130 |
|
private DelayQueue<PDelay> populatedQueue(int n) { |
131 |
|
DelayQueue<PDelay> q = new DelayQueue<PDelay>(); |
132 |
|
assertTrue(q.isEmpty()); |
133 |
< |
for (int i = n-1; i >= 0; i-=2) |
133 |
> |
for (int i = n - 1; i >= 0; i -= 2) |
134 |
|
assertTrue(q.offer(new PDelay(i))); |
135 |
< |
for (int i = (n & 1); i < n; i+=2) |
135 |
> |
for (int i = (n & 1); i < n; i += 2) |
136 |
|
assertTrue(q.offer(new PDelay(i))); |
137 |
|
assertFalse(q.isEmpty()); |
138 |
< |
assertEquals(NOCAP, q.remainingCapacity()); |
138 |
> |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
139 |
|
assertEquals(n, q.size()); |
140 |
+ |
assertEquals(new PDelay(0), q.peek()); |
141 |
|
return q; |
142 |
|
} |
143 |
|
|
145 |
|
* A new queue has unbounded capacity |
146 |
|
*/ |
147 |
|
public void testConstructor1() { |
148 |
< |
assertEquals(NOCAP, new DelayQueue().remainingCapacity()); |
148 |
> |
assertEquals(Integer.MAX_VALUE, new DelayQueue().remainingCapacity()); |
149 |
|
} |
150 |
|
|
151 |
|
/** |
153 |
|
*/ |
154 |
|
public void testConstructor3() { |
155 |
|
try { |
156 |
< |
DelayQueue q = new DelayQueue(null); |
156 |
> |
new DelayQueue(null); |
157 |
|
shouldThrow(); |
158 |
|
} catch (NullPointerException success) {} |
159 |
|
} |
163 |
|
*/ |
164 |
|
public void testConstructor4() { |
165 |
|
try { |
166 |
< |
PDelay[] ints = new PDelay[SIZE]; |
162 |
< |
DelayQueue q = new DelayQueue(Arrays.asList(ints)); |
166 |
> |
new DelayQueue(Arrays.asList(new PDelay[SIZE])); |
167 |
|
shouldThrow(); |
168 |
|
} catch (NullPointerException success) {} |
169 |
|
} |
172 |
|
* Initializing from Collection with some null elements throws NPE |
173 |
|
*/ |
174 |
|
public void testConstructor5() { |
175 |
+ |
PDelay[] a = new PDelay[SIZE]; |
176 |
+ |
for (int i = 0; i < SIZE - 1; ++i) |
177 |
+ |
a[i] = new PDelay(i); |
178 |
|
try { |
179 |
< |
PDelay[] ints = new PDelay[SIZE]; |
173 |
< |
for (int i = 0; i < SIZE-1; ++i) |
174 |
< |
ints[i] = new PDelay(i); |
175 |
< |
DelayQueue q = new DelayQueue(Arrays.asList(ints)); |
179 |
> |
new DelayQueue(Arrays.asList(a)); |
180 |
|
shouldThrow(); |
181 |
|
} catch (NullPointerException success) {} |
182 |
|
} |
199 |
|
public void testEmpty() { |
200 |
|
DelayQueue q = new DelayQueue(); |
201 |
|
assertTrue(q.isEmpty()); |
202 |
< |
assertEquals(NOCAP, q.remainingCapacity()); |
202 |
> |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
203 |
|
q.add(new PDelay(1)); |
204 |
|
assertFalse(q.isEmpty()); |
205 |
|
q.add(new PDelay(2)); |
209 |
|
} |
210 |
|
|
211 |
|
/** |
212 |
< |
* remainingCapacity does not change when elements added or removed, |
209 |
< |
* but size does |
212 |
> |
* remainingCapacity() always returns Integer.MAX_VALUE |
213 |
|
*/ |
214 |
|
public void testRemainingCapacity() { |
215 |
< |
DelayQueue q = populatedQueue(SIZE); |
215 |
> |
BlockingQueue q = populatedQueue(SIZE); |
216 |
|
for (int i = 0; i < SIZE; ++i) { |
217 |
< |
assertEquals(NOCAP, q.remainingCapacity()); |
218 |
< |
assertEquals(SIZE-i, q.size()); |
219 |
< |
q.remove(); |
217 |
> |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
218 |
> |
assertEquals(SIZE - i, q.size()); |
219 |
> |
assertTrue(q.remove() instanceof PDelay); |
220 |
|
} |
221 |
|
for (int i = 0; i < SIZE; ++i) { |
222 |
< |
assertEquals(NOCAP, q.remainingCapacity()); |
222 |
> |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
223 |
|
assertEquals(i, q.size()); |
224 |
< |
q.add(new PDelay(i)); |
224 |
> |
assertTrue(q.add(new PDelay(i))); |
225 |
|
} |
226 |
|
} |
227 |
|
|
249 |
|
* addAll(this) throws IAE |
250 |
|
*/ |
251 |
|
public void testAddAllSelf() { |
252 |
+ |
DelayQueue q = populatedQueue(SIZE); |
253 |
|
try { |
250 |
– |
DelayQueue q = populatedQueue(SIZE); |
254 |
|
q.addAll(q); |
255 |
|
shouldThrow(); |
256 |
|
} catch (IllegalArgumentException success) {} |
261 |
|
* possibly adding some elements |
262 |
|
*/ |
263 |
|
public void testAddAll3() { |
264 |
+ |
DelayQueue q = new DelayQueue(); |
265 |
+ |
PDelay[] a = new PDelay[SIZE]; |
266 |
+ |
for (int i = 0; i < SIZE - 1; ++i) |
267 |
+ |
a[i] = new PDelay(i); |
268 |
|
try { |
269 |
< |
DelayQueue q = new DelayQueue(); |
263 |
< |
PDelay[] ints = new PDelay[SIZE]; |
264 |
< |
for (int i = 0; i < SIZE-1; ++i) |
265 |
< |
ints[i] = new PDelay(i); |
266 |
< |
q.addAll(Arrays.asList(ints)); |
269 |
> |
q.addAll(Arrays.asList(a)); |
270 |
|
shouldThrow(); |
271 |
|
} catch (NullPointerException success) {} |
272 |
|
} |
277 |
|
public void testAddAll5() { |
278 |
|
PDelay[] empty = new PDelay[0]; |
279 |
|
PDelay[] ints = new PDelay[SIZE]; |
280 |
< |
for (int i = SIZE-1; i >= 0; --i) |
280 |
> |
for (int i = SIZE - 1; i >= 0; --i) |
281 |
|
ints[i] = new PDelay(i); |
282 |
|
DelayQueue q = new DelayQueue(); |
283 |
|
assertFalse(q.addAll(Arrays.asList(empty))); |
292 |
|
public void testPut() { |
293 |
|
DelayQueue q = new DelayQueue(); |
294 |
|
for (int i = 0; i < SIZE; ++i) { |
295 |
< |
PDelay I = new PDelay(i); |
296 |
< |
q.put(I); |
297 |
< |
assertTrue(q.contains(I)); |
295 |
> |
PDelay x = new PDelay(i); |
296 |
> |
q.put(x); |
297 |
> |
assertTrue(q.contains(x)); |
298 |
|
} |
299 |
|
assertEquals(SIZE, q.size()); |
300 |
|
} |
338 |
|
public void testTake() throws InterruptedException { |
339 |
|
DelayQueue q = populatedQueue(SIZE); |
340 |
|
for (int i = 0; i < SIZE; ++i) { |
341 |
< |
assertEquals(new PDelay(i), ((PDelay)q.take())); |
341 |
> |
assertEquals(new PDelay(i), q.take()); |
342 |
|
} |
343 |
|
} |
344 |
|
|
381 |
|
public void testPoll() { |
382 |
|
DelayQueue q = populatedQueue(SIZE); |
383 |
|
for (int i = 0; i < SIZE; ++i) { |
384 |
< |
assertEquals(new PDelay(i), ((PDelay)q.poll())); |
384 |
> |
assertEquals(new PDelay(i), q.poll()); |
385 |
|
} |
386 |
|
assertNull(q.poll()); |
387 |
|
} |
392 |
|
public void testTimedPoll0() throws InterruptedException { |
393 |
|
DelayQueue q = populatedQueue(SIZE); |
394 |
|
for (int i = 0; i < SIZE; ++i) { |
395 |
< |
assertEquals(new PDelay(i), ((PDelay)q.poll(0, MILLISECONDS))); |
395 |
> |
assertEquals(new PDelay(i), q.poll(0, MILLISECONDS)); |
396 |
|
} |
397 |
|
assertNull(q.poll(0, MILLISECONDS)); |
398 |
|
} |
404 |
|
DelayQueue q = populatedQueue(SIZE); |
405 |
|
for (int i = 0; i < SIZE; ++i) { |
406 |
|
long startTime = System.nanoTime(); |
407 |
< |
assertEquals(new PDelay(i), ((PDelay)q.poll(LONG_DELAY_MS, MILLISECONDS))); |
407 |
> |
assertEquals(new PDelay(i), q.poll(LONG_DELAY_MS, MILLISECONDS)); |
408 |
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
409 |
|
} |
410 |
|
long startTime = System.nanoTime(); |
419 |
|
*/ |
420 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
421 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
422 |
+ |
final DelayQueue q = populatedQueue(SIZE); |
423 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
424 |
|
public void realRun() throws InterruptedException { |
425 |
< |
DelayQueue q = populatedQueue(SIZE); |
425 |
> |
long startTime = System.nanoTime(); |
426 |
|
for (int i = 0; i < SIZE; ++i) { |
427 |
< |
assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS))); |
427 |
> |
assertEquals(new PDelay(i), |
428 |
> |
((PDelay)q.poll(LONG_DELAY_MS, MILLISECONDS))); |
429 |
|
} |
430 |
|
|
431 |
|
Thread.currentThread().interrupt(); |
441 |
|
shouldThrow(); |
442 |
|
} catch (InterruptedException success) {} |
443 |
|
assertFalse(Thread.interrupted()); |
444 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
445 |
|
}}); |
446 |
|
|
447 |
|
await(pleaseInterrupt); |
448 |
|
assertThreadStaysAlive(t); |
449 |
|
t.interrupt(); |
450 |
|
awaitTermination(t); |
451 |
+ |
checkEmpty(q); |
452 |
|
} |
453 |
|
|
454 |
|
/** |
457 |
|
public void testPeek() { |
458 |
|
DelayQueue q = populatedQueue(SIZE); |
459 |
|
for (int i = 0; i < SIZE; ++i) { |
460 |
< |
assertEquals(new PDelay(i), ((PDelay)q.peek())); |
461 |
< |
assertEquals(new PDelay(i), ((PDelay)q.poll())); |
460 |
> |
assertEquals(new PDelay(i), q.peek()); |
461 |
> |
assertEquals(new PDelay(i), q.poll()); |
462 |
|
if (q.isEmpty()) |
463 |
|
assertNull(q.peek()); |
464 |
|
else |
473 |
|
public void testElement() { |
474 |
|
DelayQueue q = populatedQueue(SIZE); |
475 |
|
for (int i = 0; i < SIZE; ++i) { |
476 |
< |
assertEquals(new PDelay(i), ((PDelay)q.element())); |
476 |
> |
assertEquals(new PDelay(i), q.element()); |
477 |
|
q.poll(); |
478 |
|
} |
479 |
|
try { |
488 |
|
public void testRemove() { |
489 |
|
DelayQueue q = populatedQueue(SIZE); |
490 |
|
for (int i = 0; i < SIZE; ++i) { |
491 |
< |
assertEquals(new PDelay(i), ((PDelay)q.remove())); |
491 |
> |
assertEquals(new PDelay(i), q.remove()); |
492 |
|
} |
493 |
|
try { |
494 |
|
q.remove(); |
516 |
|
q.clear(); |
517 |
|
assertTrue(q.isEmpty()); |
518 |
|
assertEquals(0, q.size()); |
519 |
< |
assertEquals(NOCAP, q.remainingCapacity()); |
519 |
> |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
520 |
|
PDelay x = new PDelay(1); |
521 |
|
q.add(x); |
522 |
|
assertFalse(q.isEmpty()); |
553 |
|
assertTrue(changed); |
554 |
|
|
555 |
|
assertTrue(q.containsAll(p)); |
556 |
< |
assertEquals(SIZE-i, q.size()); |
556 |
> |
assertEquals(SIZE - i, q.size()); |
557 |
|
p.remove(); |
558 |
|
} |
559 |
|
} |
566 |
|
DelayQueue q = populatedQueue(SIZE); |
567 |
|
DelayQueue p = populatedQueue(i); |
568 |
|
assertTrue(q.removeAll(p)); |
569 |
< |
assertEquals(SIZE-i, q.size()); |
569 |
> |
assertEquals(SIZE - i, q.size()); |
570 |
|
for (int j = 0; j < i; ++j) { |
571 |
< |
PDelay I = (PDelay)(p.remove()); |
572 |
< |
assertFalse(q.contains(I)); |
571 |
> |
PDelay x = (PDelay)(p.remove()); |
572 |
> |
assertFalse(q.contains(x)); |
573 |
|
} |
574 |
|
} |
575 |
|
} |
621 |
|
++i; |
622 |
|
} |
623 |
|
assertEquals(i, SIZE); |
624 |
+ |
assertIteratorExhausted(it); |
625 |
+ |
} |
626 |
+ |
|
627 |
+ |
/** |
628 |
+ |
* iterator of empty collection has no elements |
629 |
+ |
*/ |
630 |
+ |
public void testEmptyIterator() { |
631 |
+ |
assertIteratorExhausted(new DelayQueue().iterator()); |
632 |
|
} |
633 |
|
|
634 |
|
/** |
664 |
|
public void testPollInExecutor() { |
665 |
|
final DelayQueue q = new DelayQueue(); |
666 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
667 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
668 |
< |
executor.execute(new CheckedRunnable() { |
669 |
< |
public void realRun() throws InterruptedException { |
670 |
< |
assertNull(q.poll()); |
671 |
< |
threadsStarted.await(); |
672 |
< |
assertNotNull(q.poll(LONG_DELAY_MS, MILLISECONDS)); |
673 |
< |
checkEmpty(q); |
674 |
< |
}}); |
675 |
< |
|
676 |
< |
executor.execute(new CheckedRunnable() { |
677 |
< |
public void realRun() throws InterruptedException { |
678 |
< |
threadsStarted.await(); |
679 |
< |
q.put(new PDelay(1)); |
680 |
< |
}}); |
681 |
< |
|
682 |
< |
joinPool(executor); |
667 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
668 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
669 |
> |
executor.execute(new CheckedRunnable() { |
670 |
> |
public void realRun() throws InterruptedException { |
671 |
> |
assertNull(q.poll()); |
672 |
> |
threadsStarted.await(); |
673 |
> |
assertNotNull(q.poll(LONG_DELAY_MS, MILLISECONDS)); |
674 |
> |
checkEmpty(q); |
675 |
> |
}}); |
676 |
> |
|
677 |
> |
executor.execute(new CheckedRunnable() { |
678 |
> |
public void realRun() throws InterruptedException { |
679 |
> |
threadsStarted.await(); |
680 |
> |
q.put(new PDelay(1)); |
681 |
> |
}}); |
682 |
> |
} |
683 |
|
} |
684 |
|
|
685 |
|
/** |
764 |
|
final DelayQueue q = populatedQueue(SIZE); |
765 |
|
Thread t = new Thread(new CheckedRunnable() { |
766 |
|
public void realRun() { |
767 |
< |
q.put(new PDelay(SIZE+1)); |
767 |
> |
q.put(new PDelay(SIZE + 1)); |
768 |
|
}}); |
769 |
|
|
770 |
|
t.start(); |
784 |
|
ArrayList l = new ArrayList(); |
785 |
|
q.drainTo(l, i); |
786 |
|
int k = (i < SIZE) ? i : SIZE; |
787 |
< |
assertEquals(SIZE-k, q.size()); |
787 |
> |
assertEquals(SIZE - k, q.size()); |
788 |
|
assertEquals(k, l.size()); |
789 |
|
} |
790 |
|
} |