ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/DelayQueueTest.java
Revision: 1.6
Committed: Sun Oct 5 23:00:40 2003 UTC (20 years, 7 months ago) by dl
Branch: MAIN
CVS Tags: JSR166_NOV3_FREEZE, JSR166_DEC9_PRE_ES_SUBMIT, JSR166_DEC9_POST_ES_SUBMIT
Changes since 1.5: +145 -0 lines
Log Message:
Added tests and documentation

File Contents

# Content
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 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 * A new queue has unbounded capacity
133 */
134 public void testConstructor1() {
135 assertEquals(NOCAP, new DelayQueue().remainingCapacity());
136 }
137
138 /**
139 * Initializing from null Collection throws NPE
140 */
141 public void testConstructor3() {
142 try {
143 DelayQueue q = new DelayQueue(null);
144 shouldThrow();
145 }
146 catch (NullPointerException success) {}
147 }
148
149 /**
150 * Initializing from Collection of null elements throws NPE
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 * 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 }
172 catch (NullPointerException success) {}
173 }
174
175 /**
176 * Queue contains all elements of collection used to initialize
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 * isEmpty is true before add, false after
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 * remainingCapacity does not change when elementa added or removed,
207 * but size does
208 */
209 public void testRemainingCapacity() {
210 DelayQueue q = populatedQueue(SIZE);
211 for (int i = 0; i < SIZE; ++i) {
212 assertEquals(NOCAP, q.remainingCapacity());
213 assertEquals(SIZE-i, q.size());
214 q.remove();
215 }
216 for (int i = 0; i < SIZE; ++i) {
217 assertEquals(NOCAP, q.remainingCapacity());
218 assertEquals(i, q.size());
219 q.add(new PDelay(i));
220 }
221 }
222
223 /**
224 * offer(null) throws NPE
225 */
226 public void testOfferNull() {
227 try {
228 DelayQueue q = new DelayQueue();
229 q.offer(null);
230 shouldThrow();
231 } catch (NullPointerException success) { }
232 }
233
234 /**
235 * add(null) throws NPE
236 */
237 public void testAddNull() {
238 try {
239 DelayQueue q = new DelayQueue();
240 q.add(null);
241 shouldThrow();
242 } catch (NullPointerException success) { }
243 }
244
245 /**
246 * offer non-null succeeds
247 */
248 public void testOffer() {
249 DelayQueue q = new DelayQueue();
250 assertTrue(q.offer(new PDelay(0)));
251 assertTrue(q.offer(new PDelay(1)));
252 }
253
254 /**
255 * add succeeds
256 */
257 public void testAdd() {
258 DelayQueue q = new DelayQueue();
259 for (int i = 0; i < SIZE; ++i) {
260 assertEquals(i, q.size());
261 assertTrue(q.add(new PDelay(i)));
262 }
263 }
264
265 /**
266 * addAll(null) throws NPE
267 */
268 public void testAddAll1() {
269 try {
270 DelayQueue q = new DelayQueue();
271 q.addAll(null);
272 shouldThrow();
273 }
274 catch (NullPointerException success) {}
275 }
276
277
278 /**
279 * addAll(this) throws IAE
280 */
281 public void testAddAllSelf() {
282 try {
283 DelayQueue q = populatedQueue(SIZE);
284 q.addAll(q);
285 shouldThrow();
286 }
287 catch (IllegalArgumentException success) {}
288 }
289
290 /**
291 * addAll of a collection with null elements throws NPE
292 */
293 public void testAddAll2() {
294 try {
295 DelayQueue q = new DelayQueue();
296 PDelay[] ints = new PDelay[SIZE];
297 q.addAll(Arrays.asList(ints));
298 shouldThrow();
299 }
300 catch (NullPointerException success) {}
301 }
302 /**
303 * addAll of a collection with any null elements throws NPE after
304 * possibly adding some elements
305 */
306 public void testAddAll3() {
307 try {
308 DelayQueue q = new DelayQueue();
309 PDelay[] ints = new PDelay[SIZE];
310 for (int i = 0; i < SIZE-1; ++i)
311 ints[i] = new PDelay(i);
312 q.addAll(Arrays.asList(ints));
313 shouldThrow();
314 }
315 catch (NullPointerException success) {}
316 }
317
318 /**
319 * Queue contains all elements of successful addAll
320 */
321 public void testAddAll5() {
322 try {
323 PDelay[] empty = new PDelay[0];
324 PDelay[] ints = new PDelay[SIZE];
325 for (int i = SIZE-1; i >= 0; --i)
326 ints[i] = new PDelay(i);
327 DelayQueue q = new DelayQueue();
328 assertFalse(q.addAll(Arrays.asList(empty)));
329 assertTrue(q.addAll(Arrays.asList(ints)));
330 for (int i = 0; i < SIZE; ++i)
331 assertEquals(ints[i], q.poll());
332 }
333 finally {}
334 }
335
336 /**
337 * put(null) throws NPE
338 */
339 public void testPutNull() {
340 try {
341 DelayQueue q = new DelayQueue();
342 q.put(null);
343 shouldThrow();
344 }
345 catch (NullPointerException success){
346 }
347 }
348
349 /**
350 * all elements successfully put are contained
351 */
352 public void testPut() {
353 try {
354 DelayQueue q = new DelayQueue();
355 for (int i = 0; i < SIZE; ++i) {
356 PDelay I = new PDelay(i);
357 q.put(I);
358 assertTrue(q.contains(I));
359 }
360 assertEquals(SIZE, q.size());
361 }
362 finally {
363 }
364 }
365
366 /**
367 * put doesn't block waiting for take
368 */
369 public void testPutWithTake() {
370 final DelayQueue q = new DelayQueue();
371 Thread t = new Thread(new Runnable() {
372 public void run() {
373 int added = 0;
374 try {
375 q.put(new PDelay(0));
376 ++added;
377 q.put(new PDelay(0));
378 ++added;
379 q.put(new PDelay(0));
380 ++added;
381 q.put(new PDelay(0));
382 ++added;
383 threadAssertTrue(added == 4);
384 } finally {
385 }
386 }
387 });
388 try {
389 t.start();
390 Thread.sleep(SHORT_DELAY_MS);
391 q.take();
392 t.interrupt();
393 t.join();
394 } catch (Exception e){
395 unexpectedException();
396 }
397 }
398
399 /**
400 * timed offer does not time out
401 */
402 public void testTimedOffer() {
403 final DelayQueue q = new DelayQueue();
404 Thread t = new Thread(new Runnable() {
405 public void run() {
406 try {
407 q.put(new PDelay(0));
408 q.put(new PDelay(0));
409 threadAssertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
410 threadAssertTrue(q.offer(new PDelay(0), LONG_DELAY_MS, TimeUnit.MILLISECONDS));
411 } finally { }
412 }
413 });
414
415 try {
416 t.start();
417 Thread.sleep(SMALL_DELAY_MS);
418 t.interrupt();
419 t.join();
420 } catch (Exception e){
421 unexpectedException();
422 }
423 }
424
425 /**
426 * take retrieves elements in priority order
427 */
428 public void testTake() {
429 try {
430 DelayQueue q = populatedQueue(SIZE);
431 for (int i = 0; i < SIZE; ++i) {
432 assertEquals(new PDelay(i), ((PDelay)q.take()));
433 }
434 } catch (InterruptedException e){
435 unexpectedException();
436 }
437 }
438
439 /**
440 * take blocks interruptibly when empty
441 */
442 public void testTakeFromEmpty() {
443 final DelayQueue q = new DelayQueue();
444 Thread t = new Thread(new Runnable() {
445 public void run() {
446 try {
447 q.take();
448 threadShouldThrow();
449 } catch (InterruptedException success){ }
450 }
451 });
452 try {
453 t.start();
454 Thread.sleep(SHORT_DELAY_MS);
455 t.interrupt();
456 t.join();
457 } catch (Exception e){
458 unexpectedException();
459 }
460 }
461
462 /**
463 * Take removes existing elements until empty, then blocks interruptibly
464 */
465 public void testBlockingTake() {
466 Thread t = new Thread(new Runnable() {
467 public void run() {
468 try {
469 DelayQueue q = populatedQueue(SIZE);
470 for (int i = 0; i < SIZE; ++i) {
471 threadAssertEquals(new PDelay(i), ((PDelay)q.take()));
472 }
473 q.take();
474 threadShouldThrow();
475 } catch (InterruptedException success){
476 }
477 }});
478 t.start();
479 try {
480 Thread.sleep(SHORT_DELAY_MS);
481 t.interrupt();
482 t.join();
483 }
484 catch (InterruptedException ie) {
485 unexpectedException();
486 }
487 }
488
489
490 /**
491 * poll succeeds unless empty
492 */
493 public void testPoll() {
494 DelayQueue q = populatedQueue(SIZE);
495 for (int i = 0; i < SIZE; ++i) {
496 assertEquals(new PDelay(i), ((PDelay)q.poll()));
497 }
498 assertNull(q.poll());
499 }
500
501 /**
502 * timed pool with zero timeout succeeds when non-empty, else times out
503 */
504 public void testTimedPoll0() {
505 try {
506 DelayQueue q = populatedQueue(SIZE);
507 for (int i = 0; i < SIZE; ++i) {
508 assertEquals(new PDelay(i), ((PDelay)q.poll(0, TimeUnit.MILLISECONDS)));
509 }
510 assertNull(q.poll(0, TimeUnit.MILLISECONDS));
511 } catch (InterruptedException e){
512 unexpectedException();
513 }
514 }
515
516 /**
517 * timed pool with nonzero timeout succeeds when non-empty, else times out
518 */
519 public void testTimedPoll() {
520 try {
521 DelayQueue q = populatedQueue(SIZE);
522 for (int i = 0; i < SIZE; ++i) {
523 assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)));
524 }
525 assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
526 } catch (InterruptedException e){
527 unexpectedException();
528 }
529 }
530
531 /**
532 * Interrupted timed poll throws InterruptedException instead of
533 * returning timeout status
534 */
535 public void testInterruptedTimedPoll() {
536 Thread t = new Thread(new Runnable() {
537 public void run() {
538 try {
539 DelayQueue q = populatedQueue(SIZE);
540 for (int i = 0; i < SIZE; ++i) {
541 threadAssertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)));
542 }
543 threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
544 } catch (InterruptedException success){
545 }
546 }});
547 t.start();
548 try {
549 Thread.sleep(SHORT_DELAY_MS);
550 t.interrupt();
551 t.join();
552 }
553 catch (InterruptedException ie) {
554 unexpectedException();
555 }
556 }
557
558 /**
559 * timed poll before a delayed offer fails; after offer succeeds;
560 * on interruption throws
561 */
562 public void testTimedPollWithOffer() {
563 final DelayQueue q = new DelayQueue();
564 Thread t = new Thread(new Runnable() {
565 public void run() {
566 try {
567 threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
568 q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
569 q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
570 threadFail("Should block");
571 } catch (InterruptedException success) { }
572 }
573 });
574 try {
575 t.start();
576 Thread.sleep(SMALL_DELAY_MS);
577 assertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
578 t.interrupt();
579 t.join();
580 } catch (Exception e){
581 unexpectedException();
582 }
583 }
584
585
586 /**
587 * peek returns next element, or null if empty
588 */
589 public void testPeek() {
590 DelayQueue q = populatedQueue(SIZE);
591 for (int i = 0; i < SIZE; ++i) {
592 assertEquals(new PDelay(i), ((PDelay)q.peek()));
593 q.poll();
594 assertTrue(q.peek() == null ||
595 i != ((PDelay)q.peek()).intValue());
596 }
597 assertNull(q.peek());
598 }
599
600 /**
601 * element returns next element, or throws NSEE if empty
602 */
603 public void testElement() {
604 DelayQueue q = populatedQueue(SIZE);
605 for (int i = 0; i < SIZE; ++i) {
606 assertEquals(new PDelay(i), ((PDelay)q.element()));
607 q.poll();
608 }
609 try {
610 q.element();
611 shouldThrow();
612 }
613 catch (NoSuchElementException success) {}
614 }
615
616 /**
617 * remove removes next element, or throws NSEE if empty
618 */
619 public void testRemove() {
620 DelayQueue q = populatedQueue(SIZE);
621 for (int i = 0; i < SIZE; ++i) {
622 assertEquals(new PDelay(i), ((PDelay)q.remove()));
623 }
624 try {
625 q.remove();
626 shouldThrow();
627 } catch (NoSuchElementException success){
628 }
629 }
630
631 /**
632 * remove(x) removes x and returns true if present
633 */
634 public void testRemoveElement() {
635 DelayQueue q = populatedQueue(SIZE);
636 for (int i = 1; i < SIZE; i+=2) {
637 assertTrue(q.remove(new PDelay(i)));
638 }
639 for (int i = 0; i < SIZE; i+=2) {
640 assertTrue(q.remove(new PDelay(i)));
641 assertFalse(q.remove(new PDelay(i+1)));
642 }
643 assertTrue(q.isEmpty());
644 }
645
646 /**
647 * contains(x) reports true when elements added but not yet removed
648 */
649 public void testContains() {
650 DelayQueue q = populatedQueue(SIZE);
651 for (int i = 0; i < SIZE; ++i) {
652 assertTrue(q.contains(new PDelay(i)));
653 q.poll();
654 assertFalse(q.contains(new PDelay(i)));
655 }
656 }
657
658 /**
659 * clear removes all elements
660 */
661 public void testClear() {
662 DelayQueue q = populatedQueue(SIZE);
663 q.clear();
664 assertTrue(q.isEmpty());
665 assertEquals(0, q.size());
666 assertEquals(NOCAP, q.remainingCapacity());
667 q.add(new PDelay(1));
668 assertFalse(q.isEmpty());
669 q.clear();
670 assertTrue(q.isEmpty());
671 }
672
673 /**
674 * containsAll(c) is true when c contains a subset of elements
675 */
676 public void testContainsAll() {
677 DelayQueue q = populatedQueue(SIZE);
678 DelayQueue p = new DelayQueue();
679 for (int i = 0; i < SIZE; ++i) {
680 assertTrue(q.containsAll(p));
681 assertFalse(p.containsAll(q));
682 p.add(new PDelay(i));
683 }
684 assertTrue(p.containsAll(q));
685 }
686
687 /**
688 * retainAll(c) retains only those elements of c and reports true if changed
689 */
690 public void testRetainAll() {
691 DelayQueue q = populatedQueue(SIZE);
692 DelayQueue p = populatedQueue(SIZE);
693 for (int i = 0; i < SIZE; ++i) {
694 boolean changed = q.retainAll(p);
695 if (i == 0)
696 assertFalse(changed);
697 else
698 assertTrue(changed);
699
700 assertTrue(q.containsAll(p));
701 assertEquals(SIZE-i, q.size());
702 p.remove();
703 }
704 }
705
706 /**
707 * removeAll(c) removes only those elements of c and reports true if changed
708 */
709 public void testRemoveAll() {
710 for (int i = 1; i < SIZE; ++i) {
711 DelayQueue q = populatedQueue(SIZE);
712 DelayQueue p = populatedQueue(i);
713 assertTrue(q.removeAll(p));
714 assertEquals(SIZE-i, q.size());
715 for (int j = 0; j < i; ++j) {
716 PDelay I = (PDelay)(p.remove());
717 assertFalse(q.contains(I));
718 }
719 }
720 }
721
722 /**
723 * toArray contains all elements
724 */
725 public void testToArray() {
726 DelayQueue q = populatedQueue(SIZE);
727 Object[] o = q.toArray();
728 Arrays.sort(o);
729 try {
730 for(int i = 0; i < o.length; i++)
731 assertEquals(o[i], q.take());
732 } catch (InterruptedException e){
733 unexpectedException();
734 }
735 }
736
737 /**
738 * toArray(a) contains all elements
739 */
740 public void testToArray2() {
741 DelayQueue q = populatedQueue(SIZE);
742 PDelay[] ints = new PDelay[SIZE];
743 ints = (PDelay[])q.toArray(ints);
744 Arrays.sort(ints);
745 try {
746 for(int i = 0; i < ints.length; i++)
747 assertEquals(ints[i], q.take());
748 } catch (InterruptedException e){
749 unexpectedException();
750 }
751 }
752
753
754 /**
755 * toArray(null) throws NPE
756 */
757 public void testToArray_BadArg() {
758 try {
759 DelayQueue q = populatedQueue(SIZE);
760 Object o[] = q.toArray(null);
761 shouldThrow();
762 } catch(NullPointerException success){}
763 }
764
765 /**
766 * toArray with incompatable array type throws CCE
767 */
768 public void testToArray1_BadArg() {
769 try {
770 DelayQueue q = populatedQueue(SIZE);
771 Object o[] = q.toArray(new String[10] );
772 shouldThrow();
773 } catch(ArrayStoreException success){}
774 }
775
776 /**
777 * iterator iterates through all elements
778 */
779 public void testIterator() {
780 DelayQueue q = populatedQueue(SIZE);
781 int i = 0;
782 Iterator it = q.iterator();
783 while(it.hasNext()) {
784 assertTrue(q.contains(it.next()));
785 ++i;
786 }
787 assertEquals(i, SIZE);
788 }
789
790 /**
791 * iterator.remove removes current element
792 */
793 public void testIteratorRemove () {
794 final DelayQueue q = new DelayQueue();
795 q.add(new PDelay(2));
796 q.add(new PDelay(1));
797 q.add(new PDelay(3));
798 Iterator it = q.iterator();
799 it.next();
800 it.remove();
801 it = q.iterator();
802 assertEquals(it.next(), new PDelay(2));
803 assertEquals(it.next(), new PDelay(3));
804 assertFalse(it.hasNext());
805 }
806
807
808 /**
809 * toString contains toStrings of elements
810 */
811 public void testToString() {
812 DelayQueue q = populatedQueue(SIZE);
813 String s = q.toString();
814 for (int i = 0; i < SIZE; ++i) {
815 assertTrue(s.indexOf(String.valueOf(Integer.MIN_VALUE+i)) >= 0);
816 }
817 }
818
819 /**
820 * offer transfers elements across Executor tasks
821 */
822 public void testPollInExecutor() {
823 final DelayQueue q = new DelayQueue();
824 ExecutorService executor = Executors.newFixedThreadPool(2);
825 executor.execute(new Runnable() {
826 public void run() {
827 threadAssertNull(q.poll());
828 try {
829 threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
830 threadAssertTrue(q.isEmpty());
831 }
832 catch (InterruptedException e) {
833 threadUnexpectedException();
834 }
835 }
836 });
837
838 executor.execute(new Runnable() {
839 public void run() {
840 try {
841 Thread.sleep(SHORT_DELAY_MS);
842 q.put(new PDelay(1));
843 }
844 catch (InterruptedException e) {
845 threadUnexpectedException();
846 }
847 }
848 });
849 joinPool(executor);
850
851 }
852
853
854 /**
855 * Dekayed actions do not occur until their delay elapses
856 */
857 public void testDelay() {
858 DelayQueue q = new DelayQueue();
859 NanoDelay[] elements = new NanoDelay[SIZE];
860 for (int i = 0; i < SIZE; ++i) {
861 elements[i] = new NanoDelay(1000000000L + 1000000L * (SIZE - i));
862 }
863 for (int i = 0; i < SIZE; ++i) {
864 q.add(elements[i]);
865 }
866
867 try {
868 long last = 0;
869 for (int i = 0; i < SIZE; ++i) {
870 NanoDelay e = (NanoDelay)(q.take());
871 long tt = e.getTriggerTime();
872 assertTrue(tt <= System.nanoTime());
873 if (i != 0)
874 assertTrue(tt >= last);
875 last = tt;
876 }
877 }
878 catch(InterruptedException ie) {
879 unexpectedException();
880 }
881 }
882
883
884 /**
885 * drainTo(null) throws NPE
886 */
887 public void testDrainToNull() {
888 DelayQueue q = populatedQueue(SIZE);
889 try {
890 q.drainTo(null);
891 shouldThrow();
892 } catch(NullPointerException success) {
893 }
894 }
895
896 /**
897 * drainTo(this) throws IAE
898 */
899 public void testDrainToSelf() {
900 DelayQueue q = populatedQueue(SIZE);
901 try {
902 q.drainTo(q);
903 shouldThrow();
904 } catch(IllegalArgumentException success) {
905 }
906 }
907
908 /**
909 * drainTo(c) empties queue into another collection c
910 */
911 public void testDrainTo() {
912 DelayQueue q = populatedQueue(SIZE);
913 ArrayList l = new ArrayList();
914 q.drainTo(l);
915 assertEquals(q.size(), 0);
916 assertEquals(l.size(), SIZE);
917 }
918
919 /**
920 * drainTo empties queue
921 */
922 public void testDrainToWithActivePut() {
923 final DelayQueue q = populatedQueue(SIZE);
924 Thread t = new Thread(new Runnable() {
925 public void run() {
926 q.put(new PDelay(SIZE+1));
927 }
928 });
929 try {
930 t.start();
931 ArrayList l = new ArrayList();
932 q.drainTo(l);
933 assertTrue(l.size() >= SIZE);
934 t.join();
935 assertTrue(q.size() + l.size() == SIZE+1);
936 } catch(Exception e){
937 unexpectedException();
938 }
939 }
940
941 /**
942 * drainTo(null, n) throws NPE
943 */
944 public void testDrainToNullN() {
945 DelayQueue q = populatedQueue(SIZE);
946 try {
947 q.drainTo(null, 0);
948 shouldThrow();
949 } catch(NullPointerException success) {
950 }
951 }
952
953 /**
954 * drainTo(this, n) throws IAE
955 */
956 public void testDrainToSelfN() {
957 DelayQueue q = populatedQueue(SIZE);
958 try {
959 q.drainTo(q, 0);
960 shouldThrow();
961 } catch(IllegalArgumentException success) {
962 }
963 }
964
965 /**
966 * drainTo(c, n) empties first max {n, size} elements of queue into c
967 */
968 public void testDrainToN() {
969 for (int i = 0; i < SIZE + 2; ++i) {
970 DelayQueue q = populatedQueue(SIZE);
971 ArrayList l = new ArrayList();
972 q.drainTo(l, i);
973 int k = (i < SIZE)? i : SIZE;
974 assertEquals(q.size(), SIZE-k);
975 assertEquals(l.size(), k);
976 }
977 }
978
979
980 }