1 |
/* |
2 |
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
* public domain. Use, modify, and redistribute this code in any way |
4 |
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
6 |
*/ |
7 |
|
8 |
import junit.framework.*; |
9 |
import java.util.*; |
10 |
import java.util.concurrent.*; |
11 |
|
12 |
public class DelayQueueTest extends JSR166TestCase { |
13 |
public static void main(String[] args) { |
14 |
junit.textui.TestRunner.run (suite()); |
15 |
} |
16 |
|
17 |
public static Test suite() { |
18 |
return new TestSuite(DelayQueueTest.class); |
19 |
} |
20 |
|
21 |
private static final int NOCAP = Integer.MAX_VALUE; |
22 |
|
23 |
/** |
24 |
* A delayed implmentation for testing. |
25 |
* Most Q/BQ tests use Pseudodelays, where delays are all elapsed |
26 |
* (so, no blocking solely for delays) but are still ordered |
27 |
*/ |
28 |
static class PDelay implements Delayed { |
29 |
int pseudodelay; |
30 |
PDelay(int i) { pseudodelay = Integer.MIN_VALUE + i; } |
31 |
public int compareTo(Object y) { |
32 |
int i = pseudodelay; |
33 |
int j = ((PDelay)y).pseudodelay; |
34 |
if (i < j) return -1; |
35 |
if (i > j) return 1; |
36 |
return 0; |
37 |
} |
38 |
|
39 |
public int compareTo(PDelay y) { |
40 |
int i = pseudodelay; |
41 |
int j = ((PDelay)y).pseudodelay; |
42 |
if (i < j) return -1; |
43 |
if (i > j) return 1; |
44 |
return 0; |
45 |
} |
46 |
|
47 |
public boolean equals(Object other) { |
48 |
return ((PDelay)other).pseudodelay == pseudodelay; |
49 |
} |
50 |
public boolean equals(PDelay other) { |
51 |
return ((PDelay)other).pseudodelay == pseudodelay; |
52 |
} |
53 |
|
54 |
|
55 |
public long getDelay(TimeUnit ignore) { |
56 |
return pseudodelay; |
57 |
} |
58 |
public int intValue() { |
59 |
return pseudodelay; |
60 |
} |
61 |
|
62 |
public String toString() { |
63 |
return String.valueOf(pseudodelay); |
64 |
} |
65 |
} |
66 |
|
67 |
|
68 |
/** |
69 |
* Delayed implementation that actually delays |
70 |
*/ |
71 |
static class NanoDelay implements Delayed { |
72 |
long trigger; |
73 |
NanoDelay(long i) { |
74 |
trigger = System.nanoTime() + i; |
75 |
} |
76 |
public int compareTo(Object y) { |
77 |
long i = trigger; |
78 |
long j = ((NanoDelay)y).trigger; |
79 |
if (i < j) return -1; |
80 |
if (i > j) return 1; |
81 |
return 0; |
82 |
} |
83 |
|
84 |
public int compareTo(NanoDelay y) { |
85 |
long i = trigger; |
86 |
long j = ((NanoDelay)y).trigger; |
87 |
if (i < j) return -1; |
88 |
if (i > j) return 1; |
89 |
return 0; |
90 |
} |
91 |
|
92 |
public boolean equals(Object other) { |
93 |
return ((NanoDelay)other).trigger == trigger; |
94 |
} |
95 |
public boolean equals(NanoDelay other) { |
96 |
return ((NanoDelay)other).trigger == trigger; |
97 |
} |
98 |
|
99 |
public long getDelay(TimeUnit unit) { |
100 |
long n = trigger - System.nanoTime(); |
101 |
return unit.convert(n, TimeUnit.NANOSECONDS); |
102 |
} |
103 |
|
104 |
public long getTriggerTime() { |
105 |
return trigger; |
106 |
} |
107 |
|
108 |
public String toString() { |
109 |
return String.valueOf(trigger); |
110 |
} |
111 |
} |
112 |
|
113 |
|
114 |
/** |
115 |
* Create a queue of given size containing consecutive |
116 |
* PDelays 0 ... n. |
117 |
*/ |
118 |
private DelayQueue populatedQueue(int n) { |
119 |
DelayQueue 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(NOCAP, q.remainingCapacity()); |
127 |
assertEquals(n, q.size()); |
128 |
return q; |
129 |
} |
130 |
|
131 |
/** |
132 |
* |
133 |
*/ |
134 |
public void testConstructor1() { |
135 |
assertEquals(NOCAP, new DelayQueue().remainingCapacity()); |
136 |
} |
137 |
|
138 |
/** |
139 |
* |
140 |
*/ |
141 |
public void testConstructor3() { |
142 |
try { |
143 |
DelayQueue q = new DelayQueue(null); |
144 |
shouldThrow(); |
145 |
} |
146 |
catch (NullPointerException success) {} |
147 |
} |
148 |
|
149 |
/** |
150 |
* |
151 |
*/ |
152 |
public void testConstructor4() { |
153 |
try { |
154 |
PDelay[] ints = new PDelay[SIZE]; |
155 |
DelayQueue q = new DelayQueue(Arrays.asList(ints)); |
156 |
shouldThrow(); |
157 |
} |
158 |
catch (NullPointerException success) {} |
159 |
} |
160 |
|
161 |
/** |
162 |
* |
163 |
*/ |
164 |
public void testConstructor5() { |
165 |
try { |
166 |
PDelay[] ints = new PDelay[SIZE]; |
167 |
for (int i = 0; i < SIZE-1; ++i) |
168 |
ints[i] = new PDelay(i); |
169 |
DelayQueue q = new DelayQueue(Arrays.asList(ints)); |
170 |
shouldThrow(); |
171 |
} |
172 |
catch (NullPointerException success) {} |
173 |
} |
174 |
|
175 |
/** |
176 |
* |
177 |
*/ |
178 |
public void testConstructor6() { |
179 |
try { |
180 |
PDelay[] ints = new PDelay[SIZE]; |
181 |
for (int i = 0; i < SIZE; ++i) |
182 |
ints[i] = new PDelay(i); |
183 |
DelayQueue q = new DelayQueue(Arrays.asList(ints)); |
184 |
for (int i = 0; i < SIZE; ++i) |
185 |
assertEquals(ints[i], q.poll()); |
186 |
} |
187 |
finally {} |
188 |
} |
189 |
|
190 |
/** |
191 |
* |
192 |
*/ |
193 |
public void testEmpty() { |
194 |
DelayQueue q = new DelayQueue(); |
195 |
assertTrue(q.isEmpty()); |
196 |
assertEquals(NOCAP, q.remainingCapacity()); |
197 |
q.add(new PDelay(1)); |
198 |
assertFalse(q.isEmpty()); |
199 |
q.add(new PDelay(2)); |
200 |
q.remove(); |
201 |
q.remove(); |
202 |
assertTrue(q.isEmpty()); |
203 |
} |
204 |
|
205 |
/** |
206 |
* |
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 |
* |
224 |
*/ |
225 |
public void testOfferNull() { |
226 |
try { |
227 |
DelayQueue q = new DelayQueue(); |
228 |
q.offer(null); |
229 |
shouldThrow(); |
230 |
} catch (NullPointerException success) { } |
231 |
} |
232 |
|
233 |
/** |
234 |
* |
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 |
* |
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 |
* |
255 |
*/ |
256 |
public void testAddAll1() { |
257 |
try { |
258 |
DelayQueue q = new DelayQueue(); |
259 |
q.addAll(null); |
260 |
shouldThrow(); |
261 |
} |
262 |
catch (NullPointerException success) {} |
263 |
} |
264 |
/** |
265 |
* |
266 |
*/ |
267 |
public void testAddAll2() { |
268 |
try { |
269 |
DelayQueue q = new DelayQueue(); |
270 |
PDelay[] ints = new PDelay[SIZE]; |
271 |
q.addAll(Arrays.asList(ints)); |
272 |
shouldThrow(); |
273 |
} |
274 |
catch (NullPointerException success) {} |
275 |
} |
276 |
/** |
277 |
* |
278 |
*/ |
279 |
public void testAddAll3() { |
280 |
try { |
281 |
DelayQueue q = new DelayQueue(); |
282 |
PDelay[] ints = new PDelay[SIZE]; |
283 |
for (int i = 0; i < SIZE-1; ++i) |
284 |
ints[i] = new PDelay(i); |
285 |
q.addAll(Arrays.asList(ints)); |
286 |
shouldThrow(); |
287 |
} |
288 |
catch (NullPointerException success) {} |
289 |
} |
290 |
|
291 |
/** |
292 |
* |
293 |
*/ |
294 |
public void testAddAll5() { |
295 |
try { |
296 |
PDelay[] empty = new PDelay[0]; |
297 |
PDelay[] ints = new PDelay[SIZE]; |
298 |
for (int i = SIZE-1; i >= 0; --i) |
299 |
ints[i] = new PDelay(i); |
300 |
DelayQueue q = new DelayQueue(); |
301 |
assertFalse(q.addAll(Arrays.asList(empty))); |
302 |
assertTrue(q.addAll(Arrays.asList(ints))); |
303 |
for (int i = 0; i < SIZE; ++i) |
304 |
assertEquals(ints[i], q.poll()); |
305 |
} |
306 |
finally {} |
307 |
} |
308 |
|
309 |
/** |
310 |
* |
311 |
*/ |
312 |
public void testPutNull() { |
313 |
try { |
314 |
DelayQueue q = new DelayQueue(); |
315 |
q.put(null); |
316 |
shouldThrow(); |
317 |
} |
318 |
catch (NullPointerException success){ |
319 |
} |
320 |
} |
321 |
|
322 |
/** |
323 |
* |
324 |
*/ |
325 |
public void testPut() { |
326 |
try { |
327 |
DelayQueue q = new DelayQueue(); |
328 |
for (int i = 0; i < SIZE; ++i) { |
329 |
PDelay I = new PDelay(i); |
330 |
q.put(I); |
331 |
assertTrue(q.contains(I)); |
332 |
} |
333 |
assertEquals(SIZE, q.size()); |
334 |
} |
335 |
finally { |
336 |
} |
337 |
} |
338 |
|
339 |
/** |
340 |
* |
341 |
*/ |
342 |
public void testPutWithTake() { |
343 |
final DelayQueue q = new DelayQueue(); |
344 |
Thread t = new Thread(new Runnable() { |
345 |
public void run() { |
346 |
int added = 0; |
347 |
try { |
348 |
q.put(new PDelay(0)); |
349 |
++added; |
350 |
q.put(new PDelay(0)); |
351 |
++added; |
352 |
q.put(new PDelay(0)); |
353 |
++added; |
354 |
q.put(new PDelay(0)); |
355 |
++added; |
356 |
threadAssertTrue(added == 4); |
357 |
} finally { |
358 |
} |
359 |
} |
360 |
}); |
361 |
try { |
362 |
t.start(); |
363 |
Thread.sleep(SHORT_DELAY_MS); |
364 |
q.take(); |
365 |
t.interrupt(); |
366 |
t.join(); |
367 |
} catch (Exception e){ |
368 |
unexpectedException(); |
369 |
} |
370 |
} |
371 |
|
372 |
/** |
373 |
* |
374 |
*/ |
375 |
public void testTimedOffer() { |
376 |
final DelayQueue q = new DelayQueue(); |
377 |
Thread t = new Thread(new Runnable() { |
378 |
public void run() { |
379 |
try { |
380 |
q.put(new PDelay(0)); |
381 |
q.put(new PDelay(0)); |
382 |
threadAssertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
383 |
threadAssertTrue(q.offer(new PDelay(0), LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
384 |
} finally { } |
385 |
} |
386 |
}); |
387 |
|
388 |
try { |
389 |
t.start(); |
390 |
Thread.sleep(SMALL_DELAY_MS); |
391 |
t.interrupt(); |
392 |
t.join(); |
393 |
} catch (Exception e){ |
394 |
unexpectedException(); |
395 |
} |
396 |
} |
397 |
|
398 |
/** |
399 |
* |
400 |
*/ |
401 |
public void testTake() { |
402 |
try { |
403 |
DelayQueue q = populatedQueue(SIZE); |
404 |
for (int i = 0; i < SIZE; ++i) { |
405 |
assertEquals(new PDelay(i), ((PDelay)q.take())); |
406 |
} |
407 |
} catch (InterruptedException e){ |
408 |
unexpectedException(); |
409 |
} |
410 |
} |
411 |
|
412 |
/** |
413 |
* |
414 |
*/ |
415 |
public void testTakeFromEmpty() { |
416 |
final DelayQueue q = new DelayQueue(); |
417 |
Thread t = new Thread(new Runnable() { |
418 |
public void run() { |
419 |
try { |
420 |
q.take(); |
421 |
threadShouldThrow(); |
422 |
} catch (InterruptedException success){ } |
423 |
} |
424 |
}); |
425 |
try { |
426 |
t.start(); |
427 |
Thread.sleep(SHORT_DELAY_MS); |
428 |
t.interrupt(); |
429 |
t.join(); |
430 |
} catch (Exception e){ |
431 |
unexpectedException(); |
432 |
} |
433 |
} |
434 |
|
435 |
/** |
436 |
* |
437 |
*/ |
438 |
public void testBlockingTake() { |
439 |
Thread t = new Thread(new Runnable() { |
440 |
public void run() { |
441 |
try { |
442 |
DelayQueue q = populatedQueue(SIZE); |
443 |
for (int i = 0; i < SIZE; ++i) { |
444 |
threadAssertEquals(new PDelay(i), ((PDelay)q.take())); |
445 |
} |
446 |
q.take(); |
447 |
threadShouldThrow(); |
448 |
} catch (InterruptedException success){ |
449 |
} |
450 |
}}); |
451 |
t.start(); |
452 |
try { |
453 |
Thread.sleep(SHORT_DELAY_MS); |
454 |
t.interrupt(); |
455 |
t.join(); |
456 |
} |
457 |
catch (InterruptedException ie) { |
458 |
unexpectedException(); |
459 |
} |
460 |
} |
461 |
|
462 |
|
463 |
/** |
464 |
* |
465 |
*/ |
466 |
public void testPoll() { |
467 |
DelayQueue q = populatedQueue(SIZE); |
468 |
for (int i = 0; i < SIZE; ++i) { |
469 |
assertEquals(new PDelay(i), ((PDelay)q.poll())); |
470 |
} |
471 |
assertNull(q.poll()); |
472 |
} |
473 |
|
474 |
/** |
475 |
* |
476 |
*/ |
477 |
public void testTimedPoll0() { |
478 |
try { |
479 |
DelayQueue q = populatedQueue(SIZE); |
480 |
for (int i = 0; i < SIZE; ++i) { |
481 |
assertEquals(new PDelay(i), ((PDelay)q.poll(0, TimeUnit.MILLISECONDS))); |
482 |
} |
483 |
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
484 |
} catch (InterruptedException e){ |
485 |
unexpectedException(); |
486 |
} |
487 |
} |
488 |
|
489 |
/** |
490 |
* |
491 |
*/ |
492 |
public void testTimedPoll() { |
493 |
try { |
494 |
DelayQueue q = populatedQueue(SIZE); |
495 |
for (int i = 0; i < SIZE; ++i) { |
496 |
assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS))); |
497 |
} |
498 |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
499 |
} catch (InterruptedException e){ |
500 |
unexpectedException(); |
501 |
} |
502 |
} |
503 |
|
504 |
/** |
505 |
* |
506 |
*/ |
507 |
public void testInterruptedTimedPoll() { |
508 |
Thread t = new Thread(new Runnable() { |
509 |
public void run() { |
510 |
try { |
511 |
DelayQueue q = populatedQueue(SIZE); |
512 |
for (int i = 0; i < SIZE; ++i) { |
513 |
threadAssertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS))); |
514 |
} |
515 |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
516 |
} catch (InterruptedException success){ |
517 |
} |
518 |
}}); |
519 |
t.start(); |
520 |
try { |
521 |
Thread.sleep(SHORT_DELAY_MS); |
522 |
t.interrupt(); |
523 |
t.join(); |
524 |
} |
525 |
catch (InterruptedException ie) { |
526 |
unexpectedException(); |
527 |
} |
528 |
} |
529 |
|
530 |
/** |
531 |
* |
532 |
*/ |
533 |
public void testTimedPollWithOffer() { |
534 |
final DelayQueue q = new DelayQueue(); |
535 |
Thread t = new Thread(new Runnable() { |
536 |
public void run() { |
537 |
try { |
538 |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
539 |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
540 |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
541 |
threadFail("Should block"); |
542 |
} catch (InterruptedException success) { } |
543 |
} |
544 |
}); |
545 |
try { |
546 |
t.start(); |
547 |
Thread.sleep(SMALL_DELAY_MS); |
548 |
assertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
549 |
t.interrupt(); |
550 |
t.join(); |
551 |
} catch (Exception e){ |
552 |
unexpectedException(); |
553 |
} |
554 |
} |
555 |
|
556 |
|
557 |
/** |
558 |
* |
559 |
*/ |
560 |
public void testPeek() { |
561 |
DelayQueue q = populatedQueue(SIZE); |
562 |
for (int i = 0; i < SIZE; ++i) { |
563 |
assertEquals(new PDelay(i), ((PDelay)q.peek())); |
564 |
q.poll(); |
565 |
assertTrue(q.peek() == null || |
566 |
i != ((PDelay)q.peek()).intValue()); |
567 |
} |
568 |
assertNull(q.peek()); |
569 |
} |
570 |
|
571 |
/** |
572 |
* |
573 |
*/ |
574 |
public void testElement() { |
575 |
DelayQueue q = populatedQueue(SIZE); |
576 |
for (int i = 0; i < SIZE; ++i) { |
577 |
assertEquals(new PDelay(i), ((PDelay)q.element())); |
578 |
q.poll(); |
579 |
} |
580 |
try { |
581 |
q.element(); |
582 |
shouldThrow(); |
583 |
} |
584 |
catch (NoSuchElementException success) {} |
585 |
} |
586 |
|
587 |
/** |
588 |
* |
589 |
*/ |
590 |
public void testRemove() { |
591 |
DelayQueue q = populatedQueue(SIZE); |
592 |
for (int i = 0; i < SIZE; ++i) { |
593 |
assertEquals(new PDelay(i), ((PDelay)q.remove())); |
594 |
} |
595 |
try { |
596 |
q.remove(); |
597 |
shouldThrow(); |
598 |
} catch (NoSuchElementException success){ |
599 |
} |
600 |
} |
601 |
|
602 |
/** |
603 |
* |
604 |
*/ |
605 |
public void testRemoveElement() { |
606 |
DelayQueue q = populatedQueue(SIZE); |
607 |
for (int i = 1; i < SIZE; i+=2) { |
608 |
assertTrue(q.remove(new PDelay(i))); |
609 |
} |
610 |
for (int i = 0; i < SIZE; i+=2) { |
611 |
assertTrue(q.remove(new PDelay(i))); |
612 |
assertFalse(q.remove(new PDelay(i+1))); |
613 |
} |
614 |
assertTrue(q.isEmpty()); |
615 |
} |
616 |
|
617 |
/** |
618 |
* |
619 |
*/ |
620 |
public void testContains() { |
621 |
DelayQueue q = populatedQueue(SIZE); |
622 |
for (int i = 0; i < SIZE; ++i) { |
623 |
assertTrue(q.contains(new PDelay(i))); |
624 |
q.poll(); |
625 |
assertFalse(q.contains(new PDelay(i))); |
626 |
} |
627 |
} |
628 |
|
629 |
/** |
630 |
* |
631 |
*/ |
632 |
public void testClear() { |
633 |
DelayQueue q = populatedQueue(SIZE); |
634 |
q.clear(); |
635 |
assertTrue(q.isEmpty()); |
636 |
assertEquals(0, q.size()); |
637 |
assertEquals(NOCAP, q.remainingCapacity()); |
638 |
q.add(new PDelay(1)); |
639 |
assertFalse(q.isEmpty()); |
640 |
q.clear(); |
641 |
assertTrue(q.isEmpty()); |
642 |
} |
643 |
|
644 |
/** |
645 |
* |
646 |
*/ |
647 |
public void testContainsAll() { |
648 |
DelayQueue q = populatedQueue(SIZE); |
649 |
DelayQueue p = new DelayQueue(); |
650 |
for (int i = 0; i < SIZE; ++i) { |
651 |
assertTrue(q.containsAll(p)); |
652 |
assertFalse(p.containsAll(q)); |
653 |
p.add(new PDelay(i)); |
654 |
} |
655 |
assertTrue(p.containsAll(q)); |
656 |
} |
657 |
|
658 |
/** |
659 |
* |
660 |
*/ |
661 |
public void testRetainAll() { |
662 |
DelayQueue q = populatedQueue(SIZE); |
663 |
DelayQueue p = populatedQueue(SIZE); |
664 |
for (int i = 0; i < SIZE; ++i) { |
665 |
boolean changed = q.retainAll(p); |
666 |
if (i == 0) |
667 |
assertFalse(changed); |
668 |
else |
669 |
assertTrue(changed); |
670 |
|
671 |
assertTrue(q.containsAll(p)); |
672 |
assertEquals(SIZE-i, q.size()); |
673 |
p.remove(); |
674 |
} |
675 |
} |
676 |
|
677 |
/** |
678 |
* |
679 |
*/ |
680 |
public void testRemoveAll() { |
681 |
for (int i = 1; i < SIZE; ++i) { |
682 |
DelayQueue q = populatedQueue(SIZE); |
683 |
DelayQueue p = populatedQueue(i); |
684 |
assertTrue(q.removeAll(p)); |
685 |
assertEquals(SIZE-i, q.size()); |
686 |
for (int j = 0; j < i; ++j) { |
687 |
PDelay I = (PDelay)(p.remove()); |
688 |
assertFalse(q.contains(I)); |
689 |
} |
690 |
} |
691 |
} |
692 |
|
693 |
/** |
694 |
* |
695 |
*/ |
696 |
public void testToArray() { |
697 |
DelayQueue q = populatedQueue(SIZE); |
698 |
Object[] o = q.toArray(); |
699 |
Arrays.sort(o); |
700 |
try { |
701 |
for(int i = 0; i < o.length; i++) |
702 |
assertEquals(o[i], q.take()); |
703 |
} catch (InterruptedException e){ |
704 |
unexpectedException(); |
705 |
} |
706 |
} |
707 |
|
708 |
/** |
709 |
* |
710 |
*/ |
711 |
public void testToArray2() { |
712 |
DelayQueue q = populatedQueue(SIZE); |
713 |
PDelay[] ints = new PDelay[SIZE]; |
714 |
ints = (PDelay[])q.toArray(ints); |
715 |
Arrays.sort(ints); |
716 |
try { |
717 |
for(int i = 0; i < ints.length; i++) |
718 |
assertEquals(ints[i], q.take()); |
719 |
} catch (InterruptedException e){ |
720 |
unexpectedException(); |
721 |
} |
722 |
} |
723 |
|
724 |
/** |
725 |
* |
726 |
*/ |
727 |
public void testIterator() { |
728 |
DelayQueue q = populatedQueue(SIZE); |
729 |
int i = 0; |
730 |
Iterator it = q.iterator(); |
731 |
while(it.hasNext()) { |
732 |
assertTrue(q.contains(it.next())); |
733 |
++i; |
734 |
} |
735 |
assertEquals(i, SIZE); |
736 |
} |
737 |
|
738 |
/** |
739 |
* |
740 |
*/ |
741 |
public void testIteratorRemove () { |
742 |
|
743 |
final DelayQueue q = new DelayQueue(); |
744 |
|
745 |
q.add(new PDelay(2)); |
746 |
q.add(new PDelay(1)); |
747 |
q.add(new PDelay(3)); |
748 |
|
749 |
Iterator it = q.iterator(); |
750 |
it.next(); |
751 |
it.remove(); |
752 |
|
753 |
it = q.iterator(); |
754 |
assertEquals(it.next(), new PDelay(2)); |
755 |
assertEquals(it.next(), new PDelay(3)); |
756 |
assertFalse(it.hasNext()); |
757 |
} |
758 |
|
759 |
|
760 |
/** |
761 |
* |
762 |
*/ |
763 |
public void testToString() { |
764 |
DelayQueue q = populatedQueue(SIZE); |
765 |
String s = q.toString(); |
766 |
for (int i = 0; i < SIZE; ++i) { |
767 |
assertTrue(s.indexOf(String.valueOf(Integer.MIN_VALUE+i)) >= 0); |
768 |
} |
769 |
} |
770 |
|
771 |
/** |
772 |
* |
773 |
*/ |
774 |
public void testPollInExecutor() { |
775 |
|
776 |
final DelayQueue q = new DelayQueue(); |
777 |
|
778 |
ExecutorService executor = Executors.newFixedThreadPool(2); |
779 |
|
780 |
executor.execute(new Runnable() { |
781 |
public void run() { |
782 |
threadAssertNull(q.poll()); |
783 |
try { |
784 |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
785 |
threadAssertTrue(q.isEmpty()); |
786 |
} |
787 |
catch (InterruptedException e) { |
788 |
threadUnexpectedException(); |
789 |
} |
790 |
} |
791 |
}); |
792 |
|
793 |
executor.execute(new Runnable() { |
794 |
public void run() { |
795 |
try { |
796 |
Thread.sleep(SHORT_DELAY_MS); |
797 |
q.put(new PDelay(1)); |
798 |
} |
799 |
catch (InterruptedException e) { |
800 |
threadUnexpectedException(); |
801 |
} |
802 |
} |
803 |
}); |
804 |
|
805 |
joinPool(executor); |
806 |
|
807 |
} |
808 |
|
809 |
|
810 |
/** |
811 |
* |
812 |
*/ |
813 |
public void testDelay() { |
814 |
DelayQueue q = new DelayQueue(); |
815 |
NanoDelay[] elements = new NanoDelay[SIZE]; |
816 |
for (int i = 0; i < SIZE; ++i) { |
817 |
elements[i] = new NanoDelay(1000000000L + 1000000L * (SIZE - i)); |
818 |
} |
819 |
for (int i = 0; i < SIZE; ++i) { |
820 |
q.add(elements[i]); |
821 |
} |
822 |
|
823 |
try { |
824 |
long last = 0; |
825 |
for (int i = 0; i < SIZE; ++i) { |
826 |
NanoDelay e = (NanoDelay)(q.take()); |
827 |
long tt = e.getTriggerTime(); |
828 |
assertTrue(tt <= System.nanoTime()); |
829 |
if (i != 0) |
830 |
assertTrue(tt >= last); |
831 |
last = tt; |
832 |
} |
833 |
} |
834 |
catch(InterruptedException ie) { |
835 |
unexpectedException(); |
836 |
} |
837 |
} |
838 |
|
839 |
} |