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