ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/DelayQueueTest.java
Revision: 1.9
Committed: Wed Jan 7 01:13:50 2004 UTC (20 years, 4 months ago) by dl
Branch: MAIN
CVS Tags: JSR166_PFD
Changes since 1.8: +1 -1 lines
Log Message:
Fixed drainTo tests

File Contents

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