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