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

# User Rev Content
1 dl 1.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 dl 1.3 public class DelayQueueTest extends JSR166TestCase {
13 dl 1.1 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 dl 1.3 private static final int NOCAP = Integer.MAX_VALUE;
22    
23 dl 1.4 /**
24     * A delayed implmentation for testing.
25 dl 1.5 * Most tests use Pseudodelays, where delays are all elapsed
26 dl 1.4 * (so, no blocking solely for delays) but are still ordered
27     */
28 dl 1.1 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 dl 1.4 /**
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 dl 1.1
113    
114     /**
115     * Create a queue of given size containing consecutive
116     * PDelays 0 ... n.
117     */
118 dl 1.3 private DelayQueue populatedQueue(int n) {
119 dl 1.1 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 dl 1.4 /**
132 dl 1.5 * A new queue has unbounded capacity
133 dl 1.4 */
134     public void testConstructor1() {
135 dl 1.1 assertEquals(NOCAP, new DelayQueue().remainingCapacity());
136     }
137    
138 dl 1.4 /**
139 dl 1.5 * Initializing from null Collection throws NPE
140 dl 1.4 */
141     public void testConstructor3() {
142 dl 1.1 try {
143     DelayQueue q = new DelayQueue(null);
144 dl 1.4 shouldThrow();
145 dl 1.1 }
146     catch (NullPointerException success) {}
147     }
148    
149 dl 1.4 /**
150 dl 1.5 * Initializing from Collection of null elements throws NPE
151 dl 1.4 */
152     public void testConstructor4() {
153 dl 1.1 try {
154 dl 1.3 PDelay[] ints = new PDelay[SIZE];
155 dl 1.1 DelayQueue q = new DelayQueue(Arrays.asList(ints));
156 dl 1.4 shouldThrow();
157 dl 1.1 }
158     catch (NullPointerException success) {}
159     }
160    
161 dl 1.4 /**
162 dl 1.5 * Initializing from Collection with some null elements throws NPE
163 dl 1.4 */
164     public void testConstructor5() {
165 dl 1.1 try {
166 dl 1.3 PDelay[] ints = new PDelay[SIZE];
167     for (int i = 0; i < SIZE-1; ++i)
168 dl 1.1 ints[i] = new PDelay(i);
169     DelayQueue q = new DelayQueue(Arrays.asList(ints));
170 dl 1.4 shouldThrow();
171 dl 1.1 }
172     catch (NullPointerException success) {}
173     }
174    
175 dl 1.4 /**
176 dl 1.5 * Queue contains all elements of collection used to initialize
177 dl 1.4 */
178     public void testConstructor6() {
179 dl 1.1 try {
180 dl 1.3 PDelay[] ints = new PDelay[SIZE];
181     for (int i = 0; i < SIZE; ++i)
182 dl 1.1 ints[i] = new PDelay(i);
183     DelayQueue q = new DelayQueue(Arrays.asList(ints));
184 dl 1.3 for (int i = 0; i < SIZE; ++i)
185 dl 1.1 assertEquals(ints[i], q.poll());
186     }
187     finally {}
188     }
189    
190 dl 1.4 /**
191 dl 1.5 * isEmpty is true before add, false after
192 dl 1.4 */
193 dl 1.1 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 dl 1.4 /**
206 dl 1.5 * remainingCapacity does not change when elementa added or removed,
207     * but size does
208 dl 1.4 */
209     public void testRemainingCapacity() {
210 dl 1.3 DelayQueue q = populatedQueue(SIZE);
211     for (int i = 0; i < SIZE; ++i) {
212 dl 1.1 assertEquals(NOCAP, q.remainingCapacity());
213 dl 1.3 assertEquals(SIZE-i, q.size());
214 dl 1.1 q.remove();
215     }
216 dl 1.3 for (int i = 0; i < SIZE; ++i) {
217 dl 1.1 assertEquals(NOCAP, q.remainingCapacity());
218     assertEquals(i, q.size());
219     q.add(new PDelay(i));
220     }
221     }
222    
223 dl 1.4 /**
224 dl 1.5 * offer(null) throws NPE
225 dl 1.4 */
226     public void testOfferNull() {
227 dl 1.1 try {
228     DelayQueue q = new DelayQueue();
229     q.offer(null);
230 dl 1.4 shouldThrow();
231 dl 1.1 } catch (NullPointerException success) { }
232     }
233    
234 dl 1.4 /**
235 dl 1.6 * 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 dl 1.5 * offer non-null succeeds
247 dl 1.4 */
248 dl 1.1 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 dl 1.4 /**
255 dl 1.5 * add succeeds
256 dl 1.4 */
257     public void testAdd() {
258 dl 1.1 DelayQueue q = new DelayQueue();
259 dl 1.3 for (int i = 0; i < SIZE; ++i) {
260 dl 1.1 assertEquals(i, q.size());
261     assertTrue(q.add(new PDelay(i)));
262     }
263     }
264    
265 dl 1.4 /**
266 dl 1.5 * addAll(null) throws NPE
267 dl 1.4 */
268     public void testAddAll1() {
269 dl 1.1 try {
270     DelayQueue q = new DelayQueue();
271     q.addAll(null);
272 dl 1.4 shouldThrow();
273 dl 1.1 }
274     catch (NullPointerException success) {}
275     }
276 dl 1.6
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 dl 1.4 /**
291 dl 1.5 * addAll of a collection with null elements throws NPE
292 dl 1.4 */
293     public void testAddAll2() {
294 dl 1.1 try {
295     DelayQueue q = new DelayQueue();
296 dl 1.3 PDelay[] ints = new PDelay[SIZE];
297 dl 1.1 q.addAll(Arrays.asList(ints));
298 dl 1.4 shouldThrow();
299 dl 1.1 }
300     catch (NullPointerException success) {}
301     }
302 dl 1.4 /**
303 dl 1.5 * addAll of a collection with any null elements throws NPE after
304     * possibly adding some elements
305 dl 1.4 */
306     public void testAddAll3() {
307 dl 1.1 try {
308     DelayQueue q = new DelayQueue();
309 dl 1.3 PDelay[] ints = new PDelay[SIZE];
310     for (int i = 0; i < SIZE-1; ++i)
311 dl 1.1 ints[i] = new PDelay(i);
312     q.addAll(Arrays.asList(ints));
313 dl 1.4 shouldThrow();
314 dl 1.1 }
315     catch (NullPointerException success) {}
316     }
317    
318 dl 1.4 /**
319 dl 1.5 * Queue contains all elements of successful addAll
320 dl 1.4 */
321     public void testAddAll5() {
322 dl 1.1 try {
323     PDelay[] empty = new PDelay[0];
324 dl 1.3 PDelay[] ints = new PDelay[SIZE];
325     for (int i = SIZE-1; i >= 0; --i)
326 dl 1.1 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 dl 1.3 for (int i = 0; i < SIZE; ++i)
331 dl 1.1 assertEquals(ints[i], q.poll());
332     }
333     finally {}
334     }
335    
336 dl 1.4 /**
337 dl 1.5 * put(null) throws NPE
338 dl 1.4 */
339 dl 1.1 public void testPutNull() {
340     try {
341     DelayQueue q = new DelayQueue();
342     q.put(null);
343 dl 1.4 shouldThrow();
344 dl 1.1 }
345     catch (NullPointerException success){
346     }
347     }
348    
349 dl 1.4 /**
350 dl 1.5 * all elements successfully put are contained
351 dl 1.4 */
352 dl 1.1 public void testPut() {
353     try {
354     DelayQueue q = new DelayQueue();
355 dl 1.3 for (int i = 0; i < SIZE; ++i) {
356 dl 1.1 PDelay I = new PDelay(i);
357     q.put(I);
358     assertTrue(q.contains(I));
359     }
360 dl 1.3 assertEquals(SIZE, q.size());
361 dl 1.1 }
362     finally {
363     }
364     }
365    
366 dl 1.4 /**
367 dl 1.5 * put doesn't block waiting for take
368 dl 1.4 */
369 dl 1.1 public void testPutWithTake() {
370     final DelayQueue q = new DelayQueue();
371     Thread t = new Thread(new Runnable() {
372 dl 1.4 public void run() {
373 dl 1.1 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 dl 1.3 threadAssertTrue(added == 4);
384 dl 1.1 } 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 dl 1.4 unexpectedException();
396 dl 1.1 }
397     }
398    
399 dl 1.4 /**
400 dl 1.5 * timed offer does not time out
401 dl 1.4 */
402 dl 1.1 public void testTimedOffer() {
403     final DelayQueue q = new DelayQueue();
404     Thread t = new Thread(new Runnable() {
405 dl 1.4 public void run() {
406 dl 1.1 try {
407     q.put(new PDelay(0));
408     q.put(new PDelay(0));
409 dl 1.3 threadAssertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
410     threadAssertTrue(q.offer(new PDelay(0), LONG_DELAY_MS, TimeUnit.MILLISECONDS));
411 dl 1.1 } finally { }
412     }
413     });
414    
415     try {
416     t.start();
417 dl 1.3 Thread.sleep(SMALL_DELAY_MS);
418 dl 1.1 t.interrupt();
419     t.join();
420     } catch (Exception e){
421 dl 1.4 unexpectedException();
422 dl 1.1 }
423     }
424    
425 dl 1.4 /**
426 dl 1.5 * take retrieves elements in priority order
427 dl 1.4 */
428     public void testTake() {
429 dl 1.1 try {
430 dl 1.3 DelayQueue q = populatedQueue(SIZE);
431     for (int i = 0; i < SIZE; ++i) {
432 dl 1.1 assertEquals(new PDelay(i), ((PDelay)q.take()));
433     }
434     } catch (InterruptedException e){
435 dl 1.4 unexpectedException();
436 dl 1.1 }
437     }
438    
439 dl 1.4 /**
440 dl 1.5 * take blocks interruptibly when empty
441 dl 1.4 */
442 dl 1.1 public void testTakeFromEmpty() {
443     final DelayQueue q = new DelayQueue();
444     Thread t = new Thread(new Runnable() {
445 dl 1.4 public void run() {
446 dl 1.1 try {
447     q.take();
448 dl 1.4 threadShouldThrow();
449 dl 1.1 } 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 dl 1.4 unexpectedException();
459 dl 1.1 }
460     }
461    
462 dl 1.4 /**
463 dl 1.5 * Take removes existing elements until empty, then blocks interruptibly
464 dl 1.4 */
465     public void testBlockingTake() {
466 dl 1.1 Thread t = new Thread(new Runnable() {
467     public void run() {
468     try {
469 dl 1.3 DelayQueue q = populatedQueue(SIZE);
470     for (int i = 0; i < SIZE; ++i) {
471     threadAssertEquals(new PDelay(i), ((PDelay)q.take()));
472 dl 1.1 }
473     q.take();
474 dl 1.4 threadShouldThrow();
475 dl 1.1 } 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 dl 1.4 unexpectedException();
486 dl 1.1 }
487     }
488    
489    
490 dl 1.4 /**
491 dl 1.5 * poll succeeds unless empty
492 dl 1.4 */
493     public void testPoll() {
494 dl 1.3 DelayQueue q = populatedQueue(SIZE);
495     for (int i = 0; i < SIZE; ++i) {
496 dl 1.1 assertEquals(new PDelay(i), ((PDelay)q.poll()));
497     }
498     assertNull(q.poll());
499     }
500    
501 dl 1.4 /**
502 dl 1.5 * timed pool with zero timeout succeeds when non-empty, else times out
503 dl 1.4 */
504 dl 1.1 public void testTimedPoll0() {
505     try {
506 dl 1.3 DelayQueue q = populatedQueue(SIZE);
507     for (int i = 0; i < SIZE; ++i) {
508 dl 1.1 assertEquals(new PDelay(i), ((PDelay)q.poll(0, TimeUnit.MILLISECONDS)));
509     }
510     assertNull(q.poll(0, TimeUnit.MILLISECONDS));
511     } catch (InterruptedException e){
512 dl 1.4 unexpectedException();
513 dl 1.1 }
514     }
515    
516 dl 1.4 /**
517 dl 1.5 * timed pool with nonzero timeout succeeds when non-empty, else times out
518 dl 1.4 */
519 dl 1.1 public void testTimedPoll() {
520     try {
521 dl 1.3 DelayQueue q = populatedQueue(SIZE);
522     for (int i = 0; i < SIZE; ++i) {
523 dl 1.1 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 dl 1.4 unexpectedException();
528 dl 1.1 }
529     }
530    
531 dl 1.4 /**
532 dl 1.5 * Interrupted timed poll throws InterruptedException instead of
533     * returning timeout status
534 dl 1.4 */
535     public void testInterruptedTimedPoll() {
536 dl 1.1 Thread t = new Thread(new Runnable() {
537     public void run() {
538     try {
539 dl 1.3 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 dl 1.1 }
543 dl 1.3 threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
544 dl 1.1 } 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 dl 1.4 unexpectedException();
555 dl 1.1 }
556     }
557    
558 dl 1.4 /**
559 dl 1.5 * timed poll before a delayed offer fails; after offer succeeds;
560     * on interruption throws
561 dl 1.4 */
562     public void testTimedPollWithOffer() {
563 dl 1.1 final DelayQueue q = new DelayQueue();
564     Thread t = new Thread(new Runnable() {
565 dl 1.4 public void run() {
566 dl 1.1 try {
567 dl 1.3 threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
568 dl 1.1 q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
569     q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
570 dl 1.3 threadFail("Should block");
571 dl 1.1 } catch (InterruptedException success) { }
572     }
573     });
574     try {
575     t.start();
576 dl 1.3 Thread.sleep(SMALL_DELAY_MS);
577 dl 1.1 assertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
578     t.interrupt();
579     t.join();
580     } catch (Exception e){
581 dl 1.4 unexpectedException();
582 dl 1.1 }
583     }
584    
585    
586 dl 1.4 /**
587 dl 1.5 * peek returns next element, or null if empty
588 dl 1.4 */
589     public void testPeek() {
590 dl 1.3 DelayQueue q = populatedQueue(SIZE);
591     for (int i = 0; i < SIZE; ++i) {
592 dl 1.1 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 dl 1.4 /**
601 dl 1.5 * element returns next element, or throws NSEE if empty
602 dl 1.4 */
603     public void testElement() {
604 dl 1.3 DelayQueue q = populatedQueue(SIZE);
605     for (int i = 0; i < SIZE; ++i) {
606 dl 1.1 assertEquals(new PDelay(i), ((PDelay)q.element()));
607     q.poll();
608     }
609     try {
610     q.element();
611 dl 1.4 shouldThrow();
612 dl 1.1 }
613     catch (NoSuchElementException success) {}
614     }
615    
616 dl 1.4 /**
617 dl 1.5 * remove removes next element, or throws NSEE if empty
618 dl 1.4 */
619     public void testRemove() {
620 dl 1.3 DelayQueue q = populatedQueue(SIZE);
621     for (int i = 0; i < SIZE; ++i) {
622 dl 1.1 assertEquals(new PDelay(i), ((PDelay)q.remove()));
623     }
624     try {
625     q.remove();
626 dl 1.4 shouldThrow();
627 dl 1.1 } catch (NoSuchElementException success){
628     }
629     }
630    
631 dl 1.4 /**
632 dl 1.5 * remove(x) removes x and returns true if present
633 dl 1.4 */
634     public void testRemoveElement() {
635 dl 1.3 DelayQueue q = populatedQueue(SIZE);
636     for (int i = 1; i < SIZE; i+=2) {
637 dl 1.1 assertTrue(q.remove(new PDelay(i)));
638     }
639 dl 1.3 for (int i = 0; i < SIZE; i+=2) {
640 dl 1.1 assertTrue(q.remove(new PDelay(i)));
641     assertFalse(q.remove(new PDelay(i+1)));
642     }
643 dl 1.2 assertTrue(q.isEmpty());
644 dl 1.1 }
645    
646 dl 1.4 /**
647 dl 1.5 * contains(x) reports true when elements added but not yet removed
648 dl 1.4 */
649     public void testContains() {
650 dl 1.3 DelayQueue q = populatedQueue(SIZE);
651     for (int i = 0; i < SIZE; ++i) {
652 dl 1.1 assertTrue(q.contains(new PDelay(i)));
653     q.poll();
654     assertFalse(q.contains(new PDelay(i)));
655     }
656     }
657    
658 dl 1.4 /**
659 dl 1.5 * clear removes all elements
660 dl 1.4 */
661     public void testClear() {
662 dl 1.3 DelayQueue q = populatedQueue(SIZE);
663 dl 1.1 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 dl 1.4 /**
674 dl 1.5 * containsAll(c) is true when c contains a subset of elements
675 dl 1.4 */
676     public void testContainsAll() {
677 dl 1.3 DelayQueue q = populatedQueue(SIZE);
678 dl 1.1 DelayQueue p = new DelayQueue();
679 dl 1.3 for (int i = 0; i < SIZE; ++i) {
680 dl 1.1 assertTrue(q.containsAll(p));
681     assertFalse(p.containsAll(q));
682     p.add(new PDelay(i));
683     }
684     assertTrue(p.containsAll(q));
685     }
686    
687 dl 1.4 /**
688 dl 1.5 * retainAll(c) retains only those elements of c and reports true if changed
689 dl 1.4 */
690     public void testRetainAll() {
691 dl 1.3 DelayQueue q = populatedQueue(SIZE);
692     DelayQueue p = populatedQueue(SIZE);
693     for (int i = 0; i < SIZE; ++i) {
694 dl 1.1 boolean changed = q.retainAll(p);
695     if (i == 0)
696     assertFalse(changed);
697     else
698     assertTrue(changed);
699    
700     assertTrue(q.containsAll(p));
701 dl 1.3 assertEquals(SIZE-i, q.size());
702 dl 1.1 p.remove();
703     }
704     }
705    
706 dl 1.4 /**
707 dl 1.5 * removeAll(c) removes only those elements of c and reports true if changed
708 dl 1.4 */
709     public void testRemoveAll() {
710 dl 1.3 for (int i = 1; i < SIZE; ++i) {
711     DelayQueue q = populatedQueue(SIZE);
712     DelayQueue p = populatedQueue(i);
713 dl 1.1 assertTrue(q.removeAll(p));
714 dl 1.3 assertEquals(SIZE-i, q.size());
715 dl 1.1 for (int j = 0; j < i; ++j) {
716     PDelay I = (PDelay)(p.remove());
717     assertFalse(q.contains(I));
718     }
719     }
720     }
721    
722 dl 1.4 /**
723 dl 1.5 * toArray contains all elements
724 dl 1.4 */
725     public void testToArray() {
726 dl 1.3 DelayQueue q = populatedQueue(SIZE);
727 dl 1.1 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 dl 1.4 unexpectedException();
734 dl 1.1 }
735     }
736    
737 dl 1.4 /**
738 dl 1.5 * toArray(a) contains all elements
739 dl 1.4 */
740     public void testToArray2() {
741 dl 1.3 DelayQueue q = populatedQueue(SIZE);
742     PDelay[] ints = new PDelay[SIZE];
743 dl 1.1 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 dl 1.4 unexpectedException();
750 dl 1.1 }
751     }
752 dl 1.6
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 dl 1.1
776 dl 1.4 /**
777 dl 1.5 * iterator iterates through all elements
778 dl 1.4 */
779     public void testIterator() {
780 dl 1.3 DelayQueue q = populatedQueue(SIZE);
781 dl 1.1 int i = 0;
782     Iterator it = q.iterator();
783     while(it.hasNext()) {
784     assertTrue(q.contains(it.next()));
785     ++i;
786     }
787 dl 1.3 assertEquals(i, SIZE);
788 dl 1.1 }
789    
790 dl 1.4 /**
791 dl 1.5 * iterator.remove removes current element
792 dl 1.4 */
793 dl 1.1 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 dl 1.4 /**
809 dl 1.5 * toString contains toStrings of elements
810 dl 1.4 */
811     public void testToString() {
812 dl 1.3 DelayQueue q = populatedQueue(SIZE);
813 dl 1.1 String s = q.toString();
814 dl 1.3 for (int i = 0; i < SIZE; ++i) {
815     assertTrue(s.indexOf(String.valueOf(Integer.MIN_VALUE+i)) >= 0);
816 dl 1.1 }
817     }
818    
819 dl 1.4 /**
820 dl 1.5 * offer transfers elements across Executor tasks
821 dl 1.4 */
822 dl 1.1 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 dl 1.3 threadAssertNull(q.poll());
828 dl 1.1 try {
829 dl 1.3 threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
830     threadAssertTrue(q.isEmpty());
831 dl 1.1 }
832     catch (InterruptedException e) {
833 dl 1.4 threadUnexpectedException();
834 dl 1.1 }
835     }
836     });
837    
838     executor.execute(new Runnable() {
839     public void run() {
840     try {
841 dl 1.3 Thread.sleep(SHORT_DELAY_MS);
842 dl 1.1 q.put(new PDelay(1));
843     }
844     catch (InterruptedException e) {
845 dl 1.4 threadUnexpectedException();
846 dl 1.1 }
847     }
848     });
849 dl 1.3 joinPool(executor);
850 dl 1.1
851     }
852    
853    
854 dl 1.4 /**
855 dl 1.5 * Dekayed actions do not occur until their delay elapses
856 dl 1.4 */
857 dl 1.1 public void testDelay() {
858     DelayQueue q = new DelayQueue();
859 dl 1.3 NanoDelay[] elements = new NanoDelay[SIZE];
860     for (int i = 0; i < SIZE; ++i) {
861     elements[i] = new NanoDelay(1000000000L + 1000000L * (SIZE - i));
862 dl 1.1 }
863 dl 1.3 for (int i = 0; i < SIZE; ++i) {
864 dl 1.1 q.add(elements[i]);
865     }
866    
867     try {
868     long last = 0;
869 dl 1.3 for (int i = 0; i < SIZE; ++i) {
870 dl 1.1 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 dl 1.4 unexpectedException();
880 dl 1.1 }
881     }
882 dl 1.6
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 dl 1.1
980     }