ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/DelayQueueTest.java
Revision: 1.20
Committed: Sat Nov 21 09:28:16 2009 UTC (14 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.19: +156 -262 lines
Log Message:
improve exception handling

File Contents

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