1 |
/* |
2 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
* Expert Group and released to the public domain, as explained at |
4 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
* Pat Fisher, Mike Judd. |
7 |
*/ |
8 |
|
9 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
|
11 |
import java.util.ArrayList; |
12 |
import java.util.Arrays; |
13 |
import java.util.Collection; |
14 |
import java.util.Iterator; |
15 |
import java.util.NoSuchElementException; |
16 |
import java.util.concurrent.BlockingQueue; |
17 |
import java.util.concurrent.CountDownLatch; |
18 |
import java.util.concurrent.Delayed; |
19 |
import java.util.concurrent.DelayQueue; |
20 |
import java.util.concurrent.Executors; |
21 |
import java.util.concurrent.ExecutorService; |
22 |
import java.util.concurrent.TimeUnit; |
23 |
|
24 |
import junit.framework.Test; |
25 |
|
26 |
public class DelayQueueTest extends JSR166TestCase { |
27 |
|
28 |
public static class Generic extends BlockingQueueTest { |
29 |
protected BlockingQueue emptyCollection() { |
30 |
return new DelayQueue(); |
31 |
} |
32 |
protected PDelay makeElement(int i) { |
33 |
return new PDelay(i); |
34 |
} |
35 |
} |
36 |
|
37 |
public static void main(String[] args) { |
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(), |
51 |
CollectionTest.testSuite(new Implementation())); |
52 |
} |
53 |
|
54 |
/** |
55 |
* A fake Delayed implementation for testing. |
56 |
* Most tests use PDelays, where delays are all elapsed |
57 |
* (so, no blocking solely for delays) but are still ordered |
58 |
*/ |
59 |
static class PDelay implements Delayed { |
60 |
final int pseudodelay; |
61 |
PDelay(int pseudodelay) { this.pseudodelay = pseudodelay; } |
62 |
public int compareTo(Delayed y) { |
63 |
return Integer.compare(this.pseudodelay, ((PDelay)y).pseudodelay); |
64 |
} |
65 |
public boolean equals(Object other) { |
66 |
return (other instanceof PDelay) && |
67 |
this.pseudodelay == ((PDelay)other).pseudodelay; |
68 |
} |
69 |
// suppress [overrides] javac warning |
70 |
public int hashCode() { return pseudodelay; } |
71 |
public long getDelay(TimeUnit ignore) { |
72 |
return Integer.MIN_VALUE + pseudodelay; |
73 |
} |
74 |
public String toString() { |
75 |
return String.valueOf(pseudodelay); |
76 |
} |
77 |
} |
78 |
|
79 |
/** |
80 |
* Delayed implementation that actually delays |
81 |
*/ |
82 |
static class NanoDelay implements Delayed { |
83 |
final long trigger; |
84 |
NanoDelay(long i) { |
85 |
trigger = System.nanoTime() + i; |
86 |
} |
87 |
|
88 |
public int compareTo(Delayed y) { |
89 |
return Long.compare(trigger, ((NanoDelay)y).trigger); |
90 |
} |
91 |
|
92 |
public boolean equals(Object other) { |
93 |
return (other instanceof NanoDelay) && |
94 |
this.trigger == ((NanoDelay)other).trigger; |
95 |
} |
96 |
|
97 |
// suppress [overrides] javac warning |
98 |
public int hashCode() { return (int) trigger; } |
99 |
|
100 |
public long getDelay(TimeUnit unit) { |
101 |
long n = trigger - System.nanoTime(); |
102 |
return unit.convert(n, TimeUnit.NANOSECONDS); |
103 |
} |
104 |
|
105 |
public long getTriggerTime() { |
106 |
return trigger; |
107 |
} |
108 |
|
109 |
public String toString() { |
110 |
return String.valueOf(trigger); |
111 |
} |
112 |
} |
113 |
|
114 |
/** |
115 |
* Returns a new queue of given size containing consecutive |
116 |
* PDelays 0 ... n - 1. |
117 |
*/ |
118 |
private static DelayQueue<PDelay> populatedQueue(int n) { |
119 |
DelayQueue<PDelay> q = new DelayQueue<>(); |
120 |
assertTrue(q.isEmpty()); |
121 |
for (int i = n - 1; i >= 0; i -= 2) |
122 |
assertTrue(q.offer(new PDelay(i))); |
123 |
for (int i = (n & 1); i < n; i += 2) |
124 |
assertTrue(q.offer(new PDelay(i))); |
125 |
assertFalse(q.isEmpty()); |
126 |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
127 |
assertEquals(n, q.size()); |
128 |
assertEquals(new PDelay(0), q.peek()); |
129 |
return q; |
130 |
} |
131 |
|
132 |
/** |
133 |
* A new queue has unbounded capacity |
134 |
*/ |
135 |
public void testConstructor1() { |
136 |
assertEquals(Integer.MAX_VALUE, new DelayQueue().remainingCapacity()); |
137 |
} |
138 |
|
139 |
/** |
140 |
* Initializing from null Collection throws NPE |
141 |
*/ |
142 |
public void testConstructor3() { |
143 |
try { |
144 |
new DelayQueue(null); |
145 |
shouldThrow(); |
146 |
} catch (NullPointerException success) {} |
147 |
} |
148 |
|
149 |
/** |
150 |
* Initializing from Collection of null elements throws NPE |
151 |
*/ |
152 |
public void testConstructor4() { |
153 |
try { |
154 |
new DelayQueue(Arrays.asList(new PDelay[SIZE])); |
155 |
shouldThrow(); |
156 |
} catch (NullPointerException success) {} |
157 |
} |
158 |
|
159 |
/** |
160 |
* Initializing from Collection with some null elements throws NPE |
161 |
*/ |
162 |
public void testConstructor5() { |
163 |
PDelay[] a = new PDelay[SIZE]; |
164 |
for (int i = 0; i < SIZE - 1; ++i) |
165 |
a[i] = new PDelay(i); |
166 |
try { |
167 |
new DelayQueue(Arrays.asList(a)); |
168 |
shouldThrow(); |
169 |
} catch (NullPointerException success) {} |
170 |
} |
171 |
|
172 |
/** |
173 |
* Queue contains all elements of collection used to initialize |
174 |
*/ |
175 |
public void testConstructor6() { |
176 |
PDelay[] ints = new PDelay[SIZE]; |
177 |
for (int i = 0; i < SIZE; ++i) |
178 |
ints[i] = new PDelay(i); |
179 |
DelayQueue q = new DelayQueue(Arrays.asList(ints)); |
180 |
for (int i = 0; i < SIZE; ++i) |
181 |
assertEquals(ints[i], q.poll()); |
182 |
} |
183 |
|
184 |
/** |
185 |
* isEmpty is true before add, false after |
186 |
*/ |
187 |
public void testEmpty() { |
188 |
DelayQueue q = new DelayQueue(); |
189 |
assertTrue(q.isEmpty()); |
190 |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
191 |
q.add(new PDelay(1)); |
192 |
assertFalse(q.isEmpty()); |
193 |
q.add(new PDelay(2)); |
194 |
q.remove(); |
195 |
q.remove(); |
196 |
assertTrue(q.isEmpty()); |
197 |
} |
198 |
|
199 |
/** |
200 |
* remainingCapacity() always returns Integer.MAX_VALUE |
201 |
*/ |
202 |
public void testRemainingCapacity() { |
203 |
BlockingQueue q = populatedQueue(SIZE); |
204 |
for (int i = 0; i < SIZE; ++i) { |
205 |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
206 |
assertEquals(SIZE - i, q.size()); |
207 |
assertTrue(q.remove() instanceof PDelay); |
208 |
} |
209 |
for (int i = 0; i < SIZE; ++i) { |
210 |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
211 |
assertEquals(i, q.size()); |
212 |
assertTrue(q.add(new PDelay(i))); |
213 |
} |
214 |
} |
215 |
|
216 |
/** |
217 |
* offer non-null succeeds |
218 |
*/ |
219 |
public void testOffer() { |
220 |
DelayQueue q = new DelayQueue(); |
221 |
assertTrue(q.offer(new PDelay(0))); |
222 |
assertTrue(q.offer(new PDelay(1))); |
223 |
} |
224 |
|
225 |
/** |
226 |
* add succeeds |
227 |
*/ |
228 |
public void testAdd() { |
229 |
DelayQueue q = new DelayQueue(); |
230 |
for (int i = 0; i < SIZE; ++i) { |
231 |
assertEquals(i, q.size()); |
232 |
assertTrue(q.add(new PDelay(i))); |
233 |
} |
234 |
} |
235 |
|
236 |
/** |
237 |
* addAll(this) throws IllegalArgumentException |
238 |
*/ |
239 |
public void testAddAllSelf() { |
240 |
DelayQueue q = populatedQueue(SIZE); |
241 |
try { |
242 |
q.addAll(q); |
243 |
shouldThrow(); |
244 |
} catch (IllegalArgumentException success) {} |
245 |
} |
246 |
|
247 |
/** |
248 |
* addAll of a collection with any null elements throws NPE after |
249 |
* possibly adding some elements |
250 |
*/ |
251 |
public void testAddAll3() { |
252 |
DelayQueue q = new DelayQueue(); |
253 |
PDelay[] a = new PDelay[SIZE]; |
254 |
for (int i = 0; i < SIZE - 1; ++i) |
255 |
a[i] = new PDelay(i); |
256 |
try { |
257 |
q.addAll(Arrays.asList(a)); |
258 |
shouldThrow(); |
259 |
} catch (NullPointerException success) {} |
260 |
} |
261 |
|
262 |
/** |
263 |
* Queue contains all elements of successful addAll |
264 |
*/ |
265 |
public void testAddAll5() { |
266 |
PDelay[] empty = new PDelay[0]; |
267 |
PDelay[] ints = new PDelay[SIZE]; |
268 |
for (int i = SIZE - 1; i >= 0; --i) |
269 |
ints[i] = new PDelay(i); |
270 |
DelayQueue q = new DelayQueue(); |
271 |
assertFalse(q.addAll(Arrays.asList(empty))); |
272 |
assertTrue(q.addAll(Arrays.asList(ints))); |
273 |
for (int i = 0; i < SIZE; ++i) |
274 |
assertEquals(ints[i], q.poll()); |
275 |
} |
276 |
|
277 |
/** |
278 |
* all elements successfully put are contained |
279 |
*/ |
280 |
public void testPut() { |
281 |
DelayQueue q = new DelayQueue(); |
282 |
for (int i = 0; i < SIZE; ++i) { |
283 |
PDelay x = new PDelay(i); |
284 |
q.put(x); |
285 |
assertTrue(q.contains(x)); |
286 |
} |
287 |
assertEquals(SIZE, q.size()); |
288 |
} |
289 |
|
290 |
/** |
291 |
* put doesn't block waiting for take |
292 |
*/ |
293 |
public void testPutWithTake() throws InterruptedException { |
294 |
final DelayQueue q = new DelayQueue(); |
295 |
Thread t = newStartedThread(new CheckedRunnable() { |
296 |
public void realRun() { |
297 |
q.put(new PDelay(0)); |
298 |
q.put(new PDelay(0)); |
299 |
q.put(new PDelay(0)); |
300 |
q.put(new PDelay(0)); |
301 |
}}); |
302 |
|
303 |
awaitTermination(t); |
304 |
assertEquals(4, q.size()); |
305 |
} |
306 |
|
307 |
/** |
308 |
* timed offer does not time out |
309 |
*/ |
310 |
public void testTimedOffer() throws InterruptedException { |
311 |
final DelayQueue q = new DelayQueue(); |
312 |
Thread t = newStartedThread(new CheckedRunnable() { |
313 |
public void realRun() throws InterruptedException { |
314 |
q.put(new PDelay(0)); |
315 |
q.put(new PDelay(0)); |
316 |
assertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, MILLISECONDS)); |
317 |
assertTrue(q.offer(new PDelay(0), LONG_DELAY_MS, MILLISECONDS)); |
318 |
}}); |
319 |
|
320 |
awaitTermination(t); |
321 |
} |
322 |
|
323 |
/** |
324 |
* take retrieves elements in priority order |
325 |
*/ |
326 |
public void testTake() throws InterruptedException { |
327 |
DelayQueue q = populatedQueue(SIZE); |
328 |
for (int i = 0; i < SIZE; ++i) { |
329 |
assertEquals(new PDelay(i), q.take()); |
330 |
} |
331 |
} |
332 |
|
333 |
/** |
334 |
* Take removes existing elements until empty, then blocks interruptibly |
335 |
*/ |
336 |
public void testBlockingTake() throws InterruptedException { |
337 |
final DelayQueue q = populatedQueue(SIZE); |
338 |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
339 |
Thread t = newStartedThread(new CheckedRunnable() { |
340 |
public void realRun() throws InterruptedException { |
341 |
for (int i = 0; i < SIZE; i++) |
342 |
assertEquals(new PDelay(i), ((PDelay)q.take())); |
343 |
|
344 |
Thread.currentThread().interrupt(); |
345 |
try { |
346 |
q.take(); |
347 |
shouldThrow(); |
348 |
} catch (InterruptedException success) {} |
349 |
assertFalse(Thread.interrupted()); |
350 |
|
351 |
pleaseInterrupt.countDown(); |
352 |
try { |
353 |
q.take(); |
354 |
shouldThrow(); |
355 |
} catch (InterruptedException success) {} |
356 |
assertFalse(Thread.interrupted()); |
357 |
}}); |
358 |
|
359 |
await(pleaseInterrupt); |
360 |
assertThreadBlocks(t, Thread.State.WAITING); |
361 |
t.interrupt(); |
362 |
awaitTermination(t); |
363 |
} |
364 |
|
365 |
/** |
366 |
* poll succeeds unless empty |
367 |
*/ |
368 |
public void testPoll() { |
369 |
DelayQueue q = populatedQueue(SIZE); |
370 |
for (int i = 0; i < SIZE; ++i) { |
371 |
assertEquals(new PDelay(i), q.poll()); |
372 |
} |
373 |
assertNull(q.poll()); |
374 |
} |
375 |
|
376 |
/** |
377 |
* timed poll with zero timeout succeeds when non-empty, else times out |
378 |
*/ |
379 |
public void testTimedPoll0() throws InterruptedException { |
380 |
DelayQueue q = populatedQueue(SIZE); |
381 |
for (int i = 0; i < SIZE; ++i) { |
382 |
assertEquals(new PDelay(i), q.poll(0, MILLISECONDS)); |
383 |
} |
384 |
assertNull(q.poll(0, MILLISECONDS)); |
385 |
} |
386 |
|
387 |
/** |
388 |
* timed poll with nonzero timeout succeeds when non-empty, else times out |
389 |
*/ |
390 |
public void testTimedPoll() throws InterruptedException { |
391 |
DelayQueue q = populatedQueue(SIZE); |
392 |
for (int i = 0; i < SIZE; ++i) { |
393 |
long startTime = System.nanoTime(); |
394 |
assertEquals(new PDelay(i), q.poll(LONG_DELAY_MS, MILLISECONDS)); |
395 |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
396 |
} |
397 |
long startTime = System.nanoTime(); |
398 |
assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
399 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
400 |
checkEmpty(q); |
401 |
} |
402 |
|
403 |
/** |
404 |
* Interrupted timed poll throws InterruptedException instead of |
405 |
* returning timeout status |
406 |
*/ |
407 |
public void testInterruptedTimedPoll() throws InterruptedException { |
408 |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
409 |
final DelayQueue q = populatedQueue(SIZE); |
410 |
Thread t = newStartedThread(new CheckedRunnable() { |
411 |
public void realRun() throws InterruptedException { |
412 |
long startTime = System.nanoTime(); |
413 |
for (int i = 0; i < SIZE; i++) |
414 |
assertEquals(new PDelay(i), |
415 |
((PDelay)q.poll(LONG_DELAY_MS, MILLISECONDS))); |
416 |
|
417 |
Thread.currentThread().interrupt(); |
418 |
try { |
419 |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
420 |
shouldThrow(); |
421 |
} catch (InterruptedException success) {} |
422 |
assertFalse(Thread.interrupted()); |
423 |
|
424 |
pleaseInterrupt.countDown(); |
425 |
try { |
426 |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
427 |
shouldThrow(); |
428 |
} catch (InterruptedException success) {} |
429 |
assertFalse(Thread.interrupted()); |
430 |
|
431 |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
432 |
}}); |
433 |
|
434 |
await(pleaseInterrupt); |
435 |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
436 |
t.interrupt(); |
437 |
awaitTermination(t); |
438 |
checkEmpty(q); |
439 |
} |
440 |
|
441 |
/** |
442 |
* peek returns next element, or null if empty |
443 |
*/ |
444 |
public void testPeek() { |
445 |
DelayQueue q = populatedQueue(SIZE); |
446 |
for (int i = 0; i < SIZE; ++i) { |
447 |
assertEquals(new PDelay(i), q.peek()); |
448 |
assertEquals(new PDelay(i), q.poll()); |
449 |
if (q.isEmpty()) |
450 |
assertNull(q.peek()); |
451 |
else |
452 |
assertFalse(new PDelay(i).equals(q.peek())); |
453 |
} |
454 |
assertNull(q.peek()); |
455 |
} |
456 |
|
457 |
/** |
458 |
* element returns next element, or throws NSEE if empty |
459 |
*/ |
460 |
public void testElement() { |
461 |
DelayQueue q = populatedQueue(SIZE); |
462 |
for (int i = 0; i < SIZE; ++i) { |
463 |
assertEquals(new PDelay(i), q.element()); |
464 |
q.poll(); |
465 |
} |
466 |
try { |
467 |
q.element(); |
468 |
shouldThrow(); |
469 |
} catch (NoSuchElementException success) {} |
470 |
} |
471 |
|
472 |
/** |
473 |
* remove removes next element, or throws NSEE if empty |
474 |
*/ |
475 |
public void testRemove() { |
476 |
DelayQueue q = populatedQueue(SIZE); |
477 |
for (int i = 0; i < SIZE; ++i) { |
478 |
assertEquals(new PDelay(i), q.remove()); |
479 |
} |
480 |
try { |
481 |
q.remove(); |
482 |
shouldThrow(); |
483 |
} catch (NoSuchElementException success) {} |
484 |
} |
485 |
|
486 |
/** |
487 |
* contains(x) reports true when elements added but not yet removed |
488 |
*/ |
489 |
public void testContains() { |
490 |
DelayQueue q = populatedQueue(SIZE); |
491 |
for (int i = 0; i < SIZE; ++i) { |
492 |
assertTrue(q.contains(new PDelay(i))); |
493 |
q.poll(); |
494 |
assertFalse(q.contains(new PDelay(i))); |
495 |
} |
496 |
} |
497 |
|
498 |
/** |
499 |
* clear removes all elements |
500 |
*/ |
501 |
public void testClear() { |
502 |
DelayQueue q = populatedQueue(SIZE); |
503 |
q.clear(); |
504 |
assertTrue(q.isEmpty()); |
505 |
assertEquals(0, q.size()); |
506 |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
507 |
PDelay x = new PDelay(1); |
508 |
q.add(x); |
509 |
assertFalse(q.isEmpty()); |
510 |
assertTrue(q.contains(x)); |
511 |
q.clear(); |
512 |
assertTrue(q.isEmpty()); |
513 |
} |
514 |
|
515 |
/** |
516 |
* containsAll(c) is true when c contains a subset of elements |
517 |
*/ |
518 |
public void testContainsAll() { |
519 |
DelayQueue q = populatedQueue(SIZE); |
520 |
DelayQueue p = new DelayQueue(); |
521 |
for (int i = 0; i < SIZE; ++i) { |
522 |
assertTrue(q.containsAll(p)); |
523 |
assertFalse(p.containsAll(q)); |
524 |
p.add(new PDelay(i)); |
525 |
} |
526 |
assertTrue(p.containsAll(q)); |
527 |
} |
528 |
|
529 |
/** |
530 |
* retainAll(c) retains only those elements of c and reports true if changed |
531 |
*/ |
532 |
public void testRetainAll() { |
533 |
DelayQueue q = populatedQueue(SIZE); |
534 |
DelayQueue p = populatedQueue(SIZE); |
535 |
for (int i = 0; i < SIZE; ++i) { |
536 |
boolean changed = q.retainAll(p); |
537 |
if (i == 0) |
538 |
assertFalse(changed); |
539 |
else |
540 |
assertTrue(changed); |
541 |
|
542 |
assertTrue(q.containsAll(p)); |
543 |
assertEquals(SIZE - i, q.size()); |
544 |
p.remove(); |
545 |
} |
546 |
} |
547 |
|
548 |
/** |
549 |
* removeAll(c) removes only those elements of c and reports true if changed |
550 |
*/ |
551 |
public void testRemoveAll() { |
552 |
for (int i = 1; i < SIZE; ++i) { |
553 |
DelayQueue q = populatedQueue(SIZE); |
554 |
DelayQueue p = populatedQueue(i); |
555 |
assertTrue(q.removeAll(p)); |
556 |
assertEquals(SIZE - i, q.size()); |
557 |
for (int j = 0; j < i; ++j) { |
558 |
PDelay x = (PDelay)(p.remove()); |
559 |
assertFalse(q.contains(x)); |
560 |
} |
561 |
} |
562 |
} |
563 |
|
564 |
/** |
565 |
* toArray contains all elements |
566 |
*/ |
567 |
public void testToArray() throws InterruptedException { |
568 |
DelayQueue q = populatedQueue(SIZE); |
569 |
Object[] a = q.toArray(); |
570 |
assertSame(Object[].class, a.getClass()); |
571 |
Arrays.sort(a); |
572 |
for (Object o : a) |
573 |
assertSame(o, q.take()); |
574 |
assertTrue(q.isEmpty()); |
575 |
} |
576 |
|
577 |
/** |
578 |
* toArray(a) contains all elements |
579 |
*/ |
580 |
public void testToArray2() { |
581 |
DelayQueue<PDelay> q = populatedQueue(SIZE); |
582 |
PDelay[] ints = new PDelay[SIZE]; |
583 |
PDelay[] array = q.toArray(ints); |
584 |
assertSame(ints, array); |
585 |
Arrays.sort(ints); |
586 |
for (PDelay o : ints) |
587 |
assertSame(o, q.remove()); |
588 |
assertTrue(q.isEmpty()); |
589 |
} |
590 |
|
591 |
/** |
592 |
* toArray(incompatible array type) throws ArrayStoreException |
593 |
*/ |
594 |
public void testToArray1_BadArg() { |
595 |
DelayQueue q = populatedQueue(SIZE); |
596 |
try { |
597 |
q.toArray(new String[10]); |
598 |
shouldThrow(); |
599 |
} catch (ArrayStoreException success) {} |
600 |
} |
601 |
|
602 |
/** |
603 |
* iterator iterates through all elements |
604 |
*/ |
605 |
public void testIterator() { |
606 |
DelayQueue q = populatedQueue(SIZE); |
607 |
int i = 0; |
608 |
Iterator it = q.iterator(); |
609 |
while (it.hasNext()) { |
610 |
assertTrue(q.contains(it.next())); |
611 |
++i; |
612 |
} |
613 |
assertEquals(i, SIZE); |
614 |
assertIteratorExhausted(it); |
615 |
} |
616 |
|
617 |
/** |
618 |
* iterator of empty collection has no elements |
619 |
*/ |
620 |
public void testEmptyIterator() { |
621 |
assertIteratorExhausted(new DelayQueue().iterator()); |
622 |
} |
623 |
|
624 |
/** |
625 |
* iterator.remove removes current element |
626 |
*/ |
627 |
public void testIteratorRemove() { |
628 |
final DelayQueue q = new DelayQueue(); |
629 |
q.add(new PDelay(2)); |
630 |
q.add(new PDelay(1)); |
631 |
q.add(new PDelay(3)); |
632 |
Iterator it = q.iterator(); |
633 |
it.next(); |
634 |
it.remove(); |
635 |
it = q.iterator(); |
636 |
assertEquals(new PDelay(2), it.next()); |
637 |
assertEquals(new PDelay(3), it.next()); |
638 |
assertFalse(it.hasNext()); |
639 |
} |
640 |
|
641 |
/** |
642 |
* toString contains toStrings of elements |
643 |
*/ |
644 |
public void testToString() { |
645 |
DelayQueue q = populatedQueue(SIZE); |
646 |
String s = q.toString(); |
647 |
for (Object e : q) |
648 |
assertTrue(s.contains(e.toString())); |
649 |
} |
650 |
|
651 |
/** |
652 |
* timed poll transfers elements across Executor tasks |
653 |
*/ |
654 |
public void testPollInExecutor() { |
655 |
final DelayQueue q = new DelayQueue(); |
656 |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
657 |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
658 |
try (PoolCleaner cleaner = cleaner(executor)) { |
659 |
executor.execute(new CheckedRunnable() { |
660 |
public void realRun() throws InterruptedException { |
661 |
assertNull(q.poll()); |
662 |
threadsStarted.await(); |
663 |
assertNotNull(q.poll(LONG_DELAY_MS, MILLISECONDS)); |
664 |
checkEmpty(q); |
665 |
}}); |
666 |
|
667 |
executor.execute(new CheckedRunnable() { |
668 |
public void realRun() throws InterruptedException { |
669 |
threadsStarted.await(); |
670 |
q.put(new PDelay(1)); |
671 |
}}); |
672 |
} |
673 |
} |
674 |
|
675 |
/** |
676 |
* Delayed actions do not occur until their delay elapses |
677 |
*/ |
678 |
public void testDelay() throws InterruptedException { |
679 |
DelayQueue<NanoDelay> q = new DelayQueue<>(); |
680 |
for (int i = 0; i < SIZE; ++i) |
681 |
q.add(new NanoDelay(1000000L * (SIZE - i))); |
682 |
|
683 |
long last = 0; |
684 |
for (int i = 0; i < SIZE; ++i) { |
685 |
NanoDelay e = q.take(); |
686 |
long tt = e.getTriggerTime(); |
687 |
assertTrue(System.nanoTime() - tt >= 0); |
688 |
if (i != 0) |
689 |
assertTrue(tt >= last); |
690 |
last = tt; |
691 |
} |
692 |
assertTrue(q.isEmpty()); |
693 |
} |
694 |
|
695 |
/** |
696 |
* peek of a non-empty queue returns non-null even if not expired |
697 |
*/ |
698 |
public void testPeekDelayed() { |
699 |
DelayQueue q = new DelayQueue(); |
700 |
q.add(new NanoDelay(Long.MAX_VALUE)); |
701 |
assertNotNull(q.peek()); |
702 |
} |
703 |
|
704 |
/** |
705 |
* poll of a non-empty queue returns null if no expired elements. |
706 |
*/ |
707 |
public void testPollDelayed() { |
708 |
DelayQueue q = new DelayQueue(); |
709 |
q.add(new NanoDelay(Long.MAX_VALUE)); |
710 |
assertNull(q.poll()); |
711 |
} |
712 |
|
713 |
/** |
714 |
* timed poll of a non-empty queue returns null if no expired elements. |
715 |
*/ |
716 |
public void testTimedPollDelayed() throws InterruptedException { |
717 |
DelayQueue q = new DelayQueue(); |
718 |
q.add(new NanoDelay(LONG_DELAY_MS * 1000000L)); |
719 |
long startTime = System.nanoTime(); |
720 |
assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
721 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
722 |
} |
723 |
|
724 |
/** |
725 |
* drainTo(c) empties queue into another collection c |
726 |
*/ |
727 |
public void testDrainTo() { |
728 |
DelayQueue q = new DelayQueue(); |
729 |
PDelay[] elems = new PDelay[SIZE]; |
730 |
for (int i = 0; i < SIZE; ++i) { |
731 |
elems[i] = new PDelay(i); |
732 |
q.add(elems[i]); |
733 |
} |
734 |
ArrayList l = new ArrayList(); |
735 |
q.drainTo(l); |
736 |
assertEquals(0, q.size()); |
737 |
for (int i = 0; i < SIZE; ++i) |
738 |
assertEquals(elems[i], l.get(i)); |
739 |
q.add(elems[0]); |
740 |
q.add(elems[1]); |
741 |
assertFalse(q.isEmpty()); |
742 |
assertTrue(q.contains(elems[0])); |
743 |
assertTrue(q.contains(elems[1])); |
744 |
l.clear(); |
745 |
q.drainTo(l); |
746 |
assertEquals(0, q.size()); |
747 |
assertEquals(2, l.size()); |
748 |
for (int i = 0; i < 2; ++i) |
749 |
assertEquals(elems[i], l.get(i)); |
750 |
} |
751 |
|
752 |
/** |
753 |
* drainTo empties queue |
754 |
*/ |
755 |
public void testDrainToWithActivePut() throws InterruptedException { |
756 |
final DelayQueue q = populatedQueue(SIZE); |
757 |
Thread t = new Thread(new CheckedRunnable() { |
758 |
public void realRun() { |
759 |
q.put(new PDelay(SIZE + 1)); |
760 |
}}); |
761 |
|
762 |
t.start(); |
763 |
ArrayList l = new ArrayList(); |
764 |
q.drainTo(l); |
765 |
assertTrue(l.size() >= SIZE); |
766 |
t.join(); |
767 |
assertTrue(q.size() + l.size() >= SIZE); |
768 |
} |
769 |
|
770 |
/** |
771 |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
772 |
*/ |
773 |
public void testDrainToN() { |
774 |
for (int i = 0; i < SIZE + 2; ++i) { |
775 |
DelayQueue q = populatedQueue(SIZE); |
776 |
ArrayList l = new ArrayList(); |
777 |
q.drainTo(l, i); |
778 |
int k = (i < SIZE) ? i : SIZE; |
779 |
assertEquals(SIZE - k, q.size()); |
780 |
assertEquals(k, l.size()); |
781 |
} |
782 |
} |
783 |
|
784 |
/** |
785 |
* remove(null), contains(null) always return false |
786 |
*/ |
787 |
public void testNeverContainsNull() { |
788 |
Collection<?> q = populatedQueue(SIZE); |
789 |
assertFalse(q.contains(null)); |
790 |
assertFalse(q.remove(null)); |
791 |
} |
792 |
} |