ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/DelayQueueTest.java
Revision: 1.20
Committed: Sat Nov 21 09:28:16 2009 UTC (14 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.19: +156 -262 lines
Log Message:
improve exception handling

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