ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/DelayQueueTest.java
Revision: 1.27
Committed: Tue Dec 1 06:03:49 2009 UTC (14 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.26: +3 -3 lines
Log Message:
Use MILLISECONDS.toNanos instead of multiplying by 1000*1000; use explicit assertEquals instead of assertTrue(...!= null); improve testPutWithTake

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