OSDN Git Service

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