ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/DelayQueueTest.java
Revision: 1.63
Committed: Sun Nov 23 22:27:06 2014 UTC (9 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.62: +9 -0 lines
Log Message:
add tests for contains(null), remove(null)

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 jsr166 1.47 * http://creativecommons.org/publicdomain/zero/1.0/
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 jsr166 1.53 import java.util.Arrays;
11     import java.util.ArrayList;
12 jsr166 1.63 import java.util.Collection;
13 jsr166 1.53 import java.util.Iterator;
14     import java.util.NoSuchElementException;
15     import java.util.concurrent.BlockingQueue;
16     import java.util.concurrent.CountDownLatch;
17     import java.util.concurrent.Delayed;
18     import java.util.concurrent.DelayQueue;
19     import java.util.concurrent.Executors;
20     import java.util.concurrent.ExecutorService;
21     import java.util.concurrent.TimeUnit;
22 jsr166 1.19 import static java.util.concurrent.TimeUnit.MILLISECONDS;
23 dl 1.1
24 dl 1.3 public class DelayQueueTest extends JSR166TestCase {
25 jsr166 1.53
26     public static class Generic extends BlockingQueueTest {
27     protected BlockingQueue emptyCollection() {
28     return new DelayQueue();
29     }
30     protected PDelay makeElement(int i) {
31     return new PDelay(i);
32     }
33     }
34    
35 dl 1.1 public static void main(String[] args) {
36 jsr166 1.31 junit.textui.TestRunner.run(suite());
37 dl 1.1 }
38    
39     public static Test suite() {
40 jsr166 1.53 return newTestSuite(DelayQueueTest.class,
41     new Generic().testSuite());
42 dl 1.1 }
43    
44 dl 1.3 private static final int NOCAP = Integer.MAX_VALUE;
45    
46 dl 1.4 /**
47 dl 1.8 * A delayed implementation for testing.
48 jsr166 1.28 * Most tests use Pseudodelays, where delays are all elapsed
49 dl 1.4 * (so, no blocking solely for delays) but are still ordered
50 jsr166 1.15 */
51     static class PDelay implements Delayed {
52 dl 1.1 int pseudodelay;
53 jsr166 1.55 PDelay(int i) { pseudodelay = i; }
54     public int compareTo(PDelay other) {
55     int a = this.pseudodelay;
56     int b = other.pseudodelay;
57     return (a < b) ? -1 : (a > b) ? 1 : 0;
58 dl 1.1 }
59 dl 1.10 public int compareTo(Delayed y) {
60 jsr166 1.28 return compareTo((PDelay)y);
61 dl 1.1 }
62     public boolean equals(Object other) {
63 jsr166 1.55 return (other instanceof PDelay) &&
64     this.pseudodelay == ((PDelay)other).pseudodelay;
65 dl 1.1 }
66 jsr166 1.61 // suppress [overrides] javac warning
67     public int hashCode() { return pseudodelay; }
68 dl 1.1 public long getDelay(TimeUnit ignore) {
69 jsr166 1.55 return Integer.MIN_VALUE + pseudodelay;
70 dl 1.1 }
71     public String toString() {
72     return String.valueOf(pseudodelay);
73     }
74     }
75    
76 dl 1.4 /**
77     * Delayed implementation that actually delays
78     */
79 jsr166 1.15 static class NanoDelay implements Delayed {
80 dl 1.4 long trigger;
81 jsr166 1.15 NanoDelay(long i) {
82 dl 1.4 trigger = System.nanoTime() + i;
83     }
84 dl 1.10 public int compareTo(NanoDelay y) {
85 dl 1.4 long i = trigger;
86 jsr166 1.28 long j = y.trigger;
87 dl 1.4 if (i < j) return -1;
88     if (i > j) return 1;
89     return 0;
90     }
91    
92 dl 1.10 public int compareTo(Delayed y) {
93 jsr166 1.28 return compareTo((NanoDelay)y);
94 dl 1.4 }
95    
96     public boolean equals(Object other) {
97 jsr166 1.28 return equals((NanoDelay)other);
98 dl 1.4 }
99     public boolean equals(NanoDelay other) {
100 jsr166 1.28 return other.trigger == trigger;
101 dl 1.4 }
102    
103 jsr166 1.61 // suppress [overrides] javac warning
104     public int hashCode() { return (int) trigger; }
105    
106 dl 1.4 public long getDelay(TimeUnit unit) {
107     long n = trigger - System.nanoTime();
108     return unit.convert(n, TimeUnit.NANOSECONDS);
109     }
110    
111     public long getTriggerTime() {
112     return trigger;
113     }
114    
115     public String toString() {
116     return String.valueOf(trigger);
117     }
118     }
119 dl 1.1
120     /**
121 jsr166 1.60 * Returns a new queue of given size containing consecutive
122 dl 1.1 * PDelays 0 ... n.
123     */
124 jsr166 1.45 private DelayQueue<PDelay> populatedQueue(int n) {
125     DelayQueue<PDelay> q = new DelayQueue<PDelay>();
126 dl 1.1 assertTrue(q.isEmpty());
127 jsr166 1.18 for (int i = n-1; i >= 0; i-=2)
128     assertTrue(q.offer(new PDelay(i)));
129     for (int i = (n & 1); i < n; i+=2)
130     assertTrue(q.offer(new PDelay(i)));
131 dl 1.1 assertFalse(q.isEmpty());
132     assertEquals(NOCAP, q.remainingCapacity());
133 jsr166 1.18 assertEquals(n, q.size());
134 dl 1.1 return q;
135     }
136 jsr166 1.15
137 dl 1.4 /**
138 dl 1.5 * A new queue has unbounded capacity
139 dl 1.4 */
140     public void testConstructor1() {
141 dl 1.1 assertEquals(NOCAP, new DelayQueue().remainingCapacity());
142     }
143    
144 dl 1.4 /**
145 dl 1.5 * Initializing from null Collection throws NPE
146 dl 1.4 */
147     public void testConstructor3() {
148 dl 1.1 try {
149     DelayQueue q = new DelayQueue(null);
150 dl 1.4 shouldThrow();
151 jsr166 1.20 } catch (NullPointerException success) {}
152 dl 1.1 }
153    
154 dl 1.4 /**
155 dl 1.5 * Initializing from Collection of null elements throws NPE
156 dl 1.4 */
157     public void testConstructor4() {
158 dl 1.1 try {
159 dl 1.3 PDelay[] ints = new PDelay[SIZE];
160 dl 1.1 DelayQueue q = new DelayQueue(Arrays.asList(ints));
161 dl 1.4 shouldThrow();
162 jsr166 1.20 } catch (NullPointerException success) {}
163 dl 1.1 }
164    
165 dl 1.4 /**
166 dl 1.5 * Initializing from Collection with some null elements throws NPE
167 dl 1.4 */
168     public void testConstructor5() {
169 dl 1.1 try {
170 dl 1.3 PDelay[] ints = new PDelay[SIZE];
171     for (int i = 0; i < SIZE-1; ++i)
172 dl 1.1 ints[i] = new PDelay(i);
173     DelayQueue q = new DelayQueue(Arrays.asList(ints));
174 dl 1.4 shouldThrow();
175 jsr166 1.20 } catch (NullPointerException success) {}
176 dl 1.1 }
177    
178 dl 1.4 /**
179 dl 1.5 * Queue contains all elements of collection used to initialize
180 dl 1.4 */
181     public void testConstructor6() {
182 jsr166 1.21 PDelay[] ints = new PDelay[SIZE];
183     for (int i = 0; i < SIZE; ++i)
184     ints[i] = new PDelay(i);
185     DelayQueue q = new DelayQueue(Arrays.asList(ints));
186     for (int i = 0; i < SIZE; ++i)
187     assertEquals(ints[i], q.poll());
188 dl 1.1 }
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 jsr166 1.30 * remainingCapacity does not change when elements added or removed,
207 dl 1.5 * 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 non-null succeeds
225 dl 1.4 */
226 dl 1.1 public void testOffer() {
227     DelayQueue q = new DelayQueue();
228     assertTrue(q.offer(new PDelay(0)));
229     assertTrue(q.offer(new PDelay(1)));
230     }
231    
232 dl 1.4 /**
233 dl 1.5 * add succeeds
234 dl 1.4 */
235     public void testAdd() {
236 dl 1.1 DelayQueue q = new DelayQueue();
237 dl 1.3 for (int i = 0; i < SIZE; ++i) {
238 dl 1.1 assertEquals(i, q.size());
239     assertTrue(q.add(new PDelay(i)));
240     }
241     }
242    
243 dl 1.4 /**
244 dl 1.6 * addAll(this) throws IAE
245     */
246     public void testAddAllSelf() {
247     try {
248     DelayQueue q = populatedQueue(SIZE);
249     q.addAll(q);
250     shouldThrow();
251 jsr166 1.20 } catch (IllegalArgumentException success) {}
252 dl 1.6 }
253    
254 dl 1.4 /**
255 dl 1.5 * addAll of a collection with any null elements throws NPE after
256     * possibly adding some elements
257 dl 1.4 */
258     public void testAddAll3() {
259 dl 1.1 try {
260     DelayQueue q = new DelayQueue();
261 dl 1.3 PDelay[] ints = new PDelay[SIZE];
262     for (int i = 0; i < SIZE-1; ++i)
263 dl 1.1 ints[i] = new PDelay(i);
264     q.addAll(Arrays.asList(ints));
265 dl 1.4 shouldThrow();
266 jsr166 1.20 } catch (NullPointerException success) {}
267 dl 1.1 }
268    
269 dl 1.4 /**
270 dl 1.5 * Queue contains all elements of successful addAll
271 dl 1.4 */
272     public void testAddAll5() {
273 jsr166 1.21 PDelay[] empty = new PDelay[0];
274     PDelay[] ints = new PDelay[SIZE];
275     for (int i = SIZE-1; i >= 0; --i)
276     ints[i] = new PDelay(i);
277     DelayQueue q = new DelayQueue();
278     assertFalse(q.addAll(Arrays.asList(empty)));
279     assertTrue(q.addAll(Arrays.asList(ints)));
280     for (int i = 0; i < SIZE; ++i)
281     assertEquals(ints[i], q.poll());
282 dl 1.1 }
283    
284 dl 1.4 /**
285 dl 1.5 * all elements successfully put are contained
286 dl 1.4 */
287 jsr166 1.48 public void testPut() {
288     DelayQueue q = new DelayQueue();
289     for (int i = 0; i < SIZE; ++i) {
290     PDelay I = new PDelay(i);
291     q.put(I);
292     assertTrue(q.contains(I));
293     }
294     assertEquals(SIZE, q.size());
295 dl 1.1 }
296    
297 dl 1.4 /**
298 dl 1.5 * put doesn't block waiting for take
299 dl 1.4 */
300 jsr166 1.20 public void testPutWithTake() throws InterruptedException {
301 dl 1.1 final DelayQueue q = new DelayQueue();
302 jsr166 1.50 Thread t = newStartedThread(new CheckedRunnable() {
303 jsr166 1.20 public void realRun() {
304     q.put(new PDelay(0));
305     q.put(new PDelay(0));
306     q.put(new PDelay(0));
307     q.put(new PDelay(0));
308     }});
309    
310 jsr166 1.50 awaitTermination(t);
311     assertEquals(4, q.size());
312 dl 1.1 }
313    
314 dl 1.4 /**
315 dl 1.5 * timed offer does not time out
316 dl 1.4 */
317 jsr166 1.20 public void testTimedOffer() throws InterruptedException {
318 dl 1.1 final DelayQueue q = new DelayQueue();
319 jsr166 1.50 Thread t = newStartedThread(new CheckedRunnable() {
320 jsr166 1.20 public void realRun() throws InterruptedException {
321     q.put(new PDelay(0));
322     q.put(new PDelay(0));
323 jsr166 1.24 assertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, MILLISECONDS));
324     assertTrue(q.offer(new PDelay(0), LONG_DELAY_MS, MILLISECONDS));
325 jsr166 1.20 }});
326 jsr166 1.15
327 jsr166 1.50 awaitTermination(t);
328 dl 1.1 }
329    
330 dl 1.4 /**
331 dl 1.5 * take retrieves elements in priority order
332 dl 1.4 */
333 jsr166 1.20 public void testTake() throws InterruptedException {
334     DelayQueue q = populatedQueue(SIZE);
335     for (int i = 0; i < SIZE; ++i) {
336     assertEquals(new PDelay(i), ((PDelay)q.take()));
337 jsr166 1.18 }
338 dl 1.1 }
339    
340 dl 1.4 /**
341 dl 1.5 * Take removes existing elements until empty, then blocks interruptibly
342 dl 1.4 */
343 jsr166 1.20 public void testBlockingTake() throws InterruptedException {
344 jsr166 1.25 final DelayQueue q = populatedQueue(SIZE);
345 jsr166 1.50 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
346     Thread t = newStartedThread(new CheckedRunnable() {
347 jsr166 1.20 public void realRun() throws InterruptedException {
348     for (int i = 0; i < SIZE; ++i) {
349 jsr166 1.25 assertEquals(new PDelay(i), ((PDelay)q.take()));
350 jsr166 1.20 }
351 jsr166 1.50
352     Thread.currentThread().interrupt();
353     try {
354     q.take();
355     shouldThrow();
356     } catch (InterruptedException success) {}
357     assertFalse(Thread.interrupted());
358    
359     pleaseInterrupt.countDown();
360 jsr166 1.25 try {
361     q.take();
362     shouldThrow();
363     } catch (InterruptedException success) {}
364 jsr166 1.50 assertFalse(Thread.interrupted());
365 jsr166 1.25 }});
366 jsr166 1.20
367 jsr166 1.50 await(pleaseInterrupt);
368     assertThreadStaysAlive(t);
369 jsr166 1.20 t.interrupt();
370 jsr166 1.50 awaitTermination(t);
371 dl 1.1 }
372    
373 dl 1.4 /**
374 dl 1.5 * poll succeeds unless empty
375 dl 1.4 */
376     public void testPoll() {
377 dl 1.3 DelayQueue q = populatedQueue(SIZE);
378     for (int i = 0; i < SIZE; ++i) {
379 dl 1.1 assertEquals(new PDelay(i), ((PDelay)q.poll()));
380     }
381 jsr166 1.18 assertNull(q.poll());
382 dl 1.1 }
383    
384 dl 1.4 /**
385 jsr166 1.41 * timed poll with zero timeout succeeds when non-empty, else times out
386 dl 1.4 */
387 jsr166 1.20 public void testTimedPoll0() throws InterruptedException {
388     DelayQueue q = populatedQueue(SIZE);
389     for (int i = 0; i < SIZE; ++i) {
390     assertEquals(new PDelay(i), ((PDelay)q.poll(0, MILLISECONDS)));
391 jsr166 1.18 }
392 jsr166 1.20 assertNull(q.poll(0, MILLISECONDS));
393 dl 1.1 }
394    
395 dl 1.4 /**
396 jsr166 1.41 * timed poll with nonzero timeout succeeds when non-empty, else times out
397 dl 1.4 */
398 jsr166 1.20 public void testTimedPoll() throws InterruptedException {
399     DelayQueue q = populatedQueue(SIZE);
400     for (int i = 0; i < SIZE; ++i) {
401 jsr166 1.51 long startTime = System.nanoTime();
402     assertEquals(new PDelay(i), ((PDelay)q.poll(LONG_DELAY_MS, MILLISECONDS)));
403     assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
404 jsr166 1.18 }
405 jsr166 1.51 long startTime = System.nanoTime();
406     assertNull(q.poll(timeoutMillis(), MILLISECONDS));
407     assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
408     checkEmpty(q);
409 dl 1.1 }
410    
411 dl 1.4 /**
412 dl 1.5 * Interrupted timed poll throws InterruptedException instead of
413     * returning timeout status
414 dl 1.4 */
415 jsr166 1.20 public void testInterruptedTimedPoll() throws InterruptedException {
416 jsr166 1.50 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
417     Thread t = newStartedThread(new CheckedRunnable() {
418 jsr166 1.20 public void realRun() throws InterruptedException {
419     DelayQueue q = populatedQueue(SIZE);
420     for (int i = 0; i < SIZE; ++i) {
421     assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS)));
422     }
423 jsr166 1.50
424     Thread.currentThread().interrupt();
425     try {
426     q.poll(LONG_DELAY_MS, MILLISECONDS);
427     shouldThrow();
428     } catch (InterruptedException success) {}
429     assertFalse(Thread.interrupted());
430    
431     pleaseInterrupt.countDown();
432 jsr166 1.20 try {
433 jsr166 1.50 q.poll(LONG_DELAY_MS, MILLISECONDS);
434 jsr166 1.20 shouldThrow();
435     } catch (InterruptedException success) {}
436 jsr166 1.50 assertFalse(Thread.interrupted());
437 jsr166 1.20 }});
438    
439 jsr166 1.50 await(pleaseInterrupt);
440     assertThreadStaysAlive(t);
441 jsr166 1.20 t.interrupt();
442 jsr166 1.50 awaitTermination(t);
443 dl 1.1 }
444    
445 dl 1.4 /**
446 dl 1.5 * peek returns next element, or null if empty
447 dl 1.4 */
448     public void testPeek() {
449 dl 1.3 DelayQueue q = populatedQueue(SIZE);
450     for (int i = 0; i < SIZE; ++i) {
451 dl 1.1 assertEquals(new PDelay(i), ((PDelay)q.peek()));
452 jsr166 1.26 assertEquals(new PDelay(i), ((PDelay)q.poll()));
453 dl 1.12 if (q.isEmpty())
454     assertNull(q.peek());
455     else
456 jsr166 1.26 assertFalse(new PDelay(i).equals(q.peek()));
457 dl 1.1 }
458 jsr166 1.18 assertNull(q.peek());
459 dl 1.1 }
460    
461 dl 1.4 /**
462 dl 1.5 * element returns next element, or throws NSEE if empty
463 dl 1.4 */
464     public void testElement() {
465 dl 1.3 DelayQueue q = populatedQueue(SIZE);
466     for (int i = 0; i < SIZE; ++i) {
467 dl 1.1 assertEquals(new PDelay(i), ((PDelay)q.element()));
468     q.poll();
469     }
470     try {
471     q.element();
472 dl 1.4 shouldThrow();
473 jsr166 1.20 } catch (NoSuchElementException success) {}
474 dl 1.1 }
475    
476 dl 1.4 /**
477 dl 1.5 * remove removes next element, or throws NSEE if empty
478 dl 1.4 */
479     public void testRemove() {
480 dl 1.3 DelayQueue q = populatedQueue(SIZE);
481     for (int i = 0; i < SIZE; ++i) {
482 dl 1.1 assertEquals(new PDelay(i), ((PDelay)q.remove()));
483     }
484     try {
485     q.remove();
486 dl 1.4 shouldThrow();
487 jsr166 1.20 } catch (NoSuchElementException success) {}
488 dl 1.1 }
489    
490 dl 1.4 /**
491 dl 1.5 * contains(x) reports true when elements added but not yet removed
492 dl 1.4 */
493     public void testContains() {
494 dl 1.3 DelayQueue q = populatedQueue(SIZE);
495     for (int i = 0; i < SIZE; ++i) {
496 dl 1.1 assertTrue(q.contains(new PDelay(i)));
497     q.poll();
498     assertFalse(q.contains(new PDelay(i)));
499     }
500     }
501    
502 dl 1.4 /**
503 dl 1.5 * clear removes all elements
504 dl 1.4 */
505     public void testClear() {
506 dl 1.3 DelayQueue q = populatedQueue(SIZE);
507 dl 1.1 q.clear();
508     assertTrue(q.isEmpty());
509     assertEquals(0, q.size());
510     assertEquals(NOCAP, q.remainingCapacity());
511 dl 1.11 PDelay x = new PDelay(1);
512     q.add(x);
513 dl 1.1 assertFalse(q.isEmpty());
514 dl 1.11 assertTrue(q.contains(x));
515 dl 1.1 q.clear();
516     assertTrue(q.isEmpty());
517     }
518    
519 dl 1.4 /**
520 dl 1.5 * containsAll(c) is true when c contains a subset of elements
521 dl 1.4 */
522     public void testContainsAll() {
523 dl 1.3 DelayQueue q = populatedQueue(SIZE);
524 dl 1.1 DelayQueue p = new DelayQueue();
525 dl 1.3 for (int i = 0; i < SIZE; ++i) {
526 dl 1.1 assertTrue(q.containsAll(p));
527     assertFalse(p.containsAll(q));
528     p.add(new PDelay(i));
529     }
530     assertTrue(p.containsAll(q));
531     }
532    
533 dl 1.4 /**
534 dl 1.5 * retainAll(c) retains only those elements of c and reports true if changed
535 dl 1.4 */
536     public void testRetainAll() {
537 dl 1.3 DelayQueue q = populatedQueue(SIZE);
538     DelayQueue p = populatedQueue(SIZE);
539     for (int i = 0; i < SIZE; ++i) {
540 dl 1.1 boolean changed = q.retainAll(p);
541     if (i == 0)
542     assertFalse(changed);
543     else
544     assertTrue(changed);
545    
546     assertTrue(q.containsAll(p));
547 dl 1.3 assertEquals(SIZE-i, q.size());
548 dl 1.1 p.remove();
549     }
550     }
551    
552 dl 1.4 /**
553 dl 1.5 * removeAll(c) removes only those elements of c and reports true if changed
554 dl 1.4 */
555     public void testRemoveAll() {
556 dl 1.3 for (int i = 1; i < SIZE; ++i) {
557     DelayQueue q = populatedQueue(SIZE);
558     DelayQueue p = populatedQueue(i);
559 dl 1.1 assertTrue(q.removeAll(p));
560 dl 1.3 assertEquals(SIZE-i, q.size());
561 dl 1.1 for (int j = 0; j < i; ++j) {
562     PDelay I = (PDelay)(p.remove());
563     assertFalse(q.contains(I));
564     }
565     }
566     }
567    
568 dl 1.4 /**
569 dl 1.5 * toArray contains all elements
570 dl 1.4 */
571 jsr166 1.20 public void testToArray() throws InterruptedException {
572 dl 1.3 DelayQueue q = populatedQueue(SIZE);
573 jsr166 1.18 Object[] o = q.toArray();
574 dl 1.1 Arrays.sort(o);
575 jsr166 1.18 for (int i = 0; i < o.length; i++)
576 jsr166 1.44 assertSame(o[i], q.take());
577 dl 1.1 }
578    
579 dl 1.4 /**
580 dl 1.5 * toArray(a) contains all elements
581 dl 1.4 */
582 jsr166 1.44 public void testToArray2() {
583 jsr166 1.45 DelayQueue<PDelay> q = populatedQueue(SIZE);
584 jsr166 1.18 PDelay[] ints = new PDelay[SIZE];
585 jsr166 1.45 PDelay[] array = q.toArray(ints);
586     assertSame(ints, array);
587 dl 1.1 Arrays.sort(ints);
588 jsr166 1.20 for (int i = 0; i < ints.length; i++)
589 jsr166 1.44 assertSame(ints[i], q.remove());
590 dl 1.1 }
591 dl 1.6
592     /**
593 jsr166 1.42 * toArray(incompatible array type) throws ArrayStoreException
594 dl 1.6 */
595     public void testToArray1_BadArg() {
596 jsr166 1.26 DelayQueue q = populatedQueue(SIZE);
597 jsr166 1.18 try {
598 jsr166 1.42 q.toArray(new String[10]);
599 jsr166 1.18 shouldThrow();
600 jsr166 1.20 } catch (ArrayStoreException success) {}
601 dl 1.6 }
602 jsr166 1.15
603 dl 1.4 /**
604 dl 1.5 * iterator iterates through all elements
605 dl 1.4 */
606     public void testIterator() {
607 dl 1.3 DelayQueue q = populatedQueue(SIZE);
608 dl 1.1 int i = 0;
609 jsr166 1.18 Iterator it = q.iterator();
610 jsr166 1.16 while (it.hasNext()) {
611 dl 1.1 assertTrue(q.contains(it.next()));
612     ++i;
613     }
614 dl 1.3 assertEquals(i, SIZE);
615 dl 1.1 }
616    
617 dl 1.4 /**
618 dl 1.5 * iterator.remove removes current element
619 dl 1.4 */
620 jsr166 1.31 public void testIteratorRemove() {
621 dl 1.1 final DelayQueue q = new DelayQueue();
622     q.add(new PDelay(2));
623     q.add(new PDelay(1));
624     q.add(new PDelay(3));
625     Iterator it = q.iterator();
626     it.next();
627     it.remove();
628     it = q.iterator();
629 jsr166 1.58 assertEquals(new PDelay(2), it.next());
630     assertEquals(new PDelay(3), it.next());
631 dl 1.1 assertFalse(it.hasNext());
632     }
633    
634 dl 1.4 /**
635 dl 1.5 * toString contains toStrings of elements
636 dl 1.4 */
637     public void testToString() {
638 dl 1.3 DelayQueue q = populatedQueue(SIZE);
639 dl 1.1 String s = q.toString();
640 jsr166 1.55 for (Object e : q)
641     assertTrue(s.contains(e.toString()));
642 jsr166 1.15 }
643 dl 1.1
644 dl 1.4 /**
645 jsr166 1.50 * timed poll transfers elements across Executor tasks
646 dl 1.4 */
647 dl 1.1 public void testPollInExecutor() {
648     final DelayQueue q = new DelayQueue();
649 jsr166 1.50 final CheckedBarrier threadsStarted = new CheckedBarrier(2);
650 dl 1.1 ExecutorService executor = Executors.newFixedThreadPool(2);
651 jsr166 1.20 executor.execute(new CheckedRunnable() {
652     public void realRun() throws InterruptedException {
653 jsr166 1.27 assertNull(q.poll());
654 jsr166 1.50 threadsStarted.await();
655 jsr166 1.62 assertNotNull(q.poll(LONG_DELAY_MS, MILLISECONDS));
656 jsr166 1.50 checkEmpty(q);
657 jsr166 1.20 }});
658    
659     executor.execute(new CheckedRunnable() {
660     public void realRun() throws InterruptedException {
661 jsr166 1.50 threadsStarted.await();
662 jsr166 1.20 q.put(new PDelay(1));
663     }});
664 dl 1.1
665 dl 1.3 joinPool(executor);
666 dl 1.1 }
667    
668 dl 1.4 /**
669 dl 1.8 * Delayed actions do not occur until their delay elapses
670 dl 1.4 */
671 jsr166 1.20 public void testDelay() throws InterruptedException {
672 jsr166 1.37 DelayQueue<NanoDelay> q = new DelayQueue<NanoDelay>();
673     for (int i = 0; i < SIZE; ++i)
674     q.add(new NanoDelay(1000000L * (SIZE - i)));
675 dl 1.1
676 jsr166 1.20 long last = 0;
677     for (int i = 0; i < SIZE; ++i) {
678 jsr166 1.37 NanoDelay e = q.take();
679 jsr166 1.20 long tt = e.getTriggerTime();
680 jsr166 1.37 assertTrue(System.nanoTime() - tt >= 0);
681 jsr166 1.20 if (i != 0)
682     assertTrue(tt >= last);
683     last = tt;
684 dl 1.1 }
685 jsr166 1.37 assertTrue(q.isEmpty());
686 dl 1.1 }
687 dl 1.6
688 dl 1.12 /**
689     * peek of a non-empty queue returns non-null even if not expired
690     */
691     public void testPeekDelayed() {
692     DelayQueue q = new DelayQueue();
693     q.add(new NanoDelay(Long.MAX_VALUE));
694 jsr166 1.33 assertNotNull(q.peek());
695 dl 1.12 }
696    
697     /**
698     * poll of a non-empty queue returns null if no expired elements.
699     */
700     public void testPollDelayed() {
701     DelayQueue q = new DelayQueue();
702     q.add(new NanoDelay(Long.MAX_VALUE));
703     assertNull(q.poll());
704     }
705 dl 1.6
706     /**
707 dl 1.13 * timed poll of a non-empty queue returns null if no expired elements.
708     */
709 jsr166 1.20 public void testTimedPollDelayed() throws InterruptedException {
710 dl 1.13 DelayQueue q = new DelayQueue();
711 dl 1.14 q.add(new NanoDelay(LONG_DELAY_MS * 1000000L));
712 jsr166 1.52 assertNull(q.poll(timeoutMillis(), MILLISECONDS));
713 dl 1.13 }
714    
715     /**
716 dl 1.6 * drainTo(c) empties queue into another collection c
717 jsr166 1.15 */
718 dl 1.6 public void testDrainTo() {
719 dl 1.11 DelayQueue q = new DelayQueue();
720     PDelay[] elems = new PDelay[SIZE];
721     for (int i = 0; i < SIZE; ++i) {
722     elems[i] = new PDelay(i);
723     q.add(elems[i]);
724     }
725 dl 1.6 ArrayList l = new ArrayList();
726     q.drainTo(l);
727 jsr166 1.57 assertEquals(0, q.size());
728 jsr166 1.15 for (int i = 0; i < SIZE; ++i)
729 jsr166 1.58 assertEquals(elems[i], l.get(i));
730 dl 1.11 q.add(elems[0]);
731     q.add(elems[1]);
732     assertFalse(q.isEmpty());
733     assertTrue(q.contains(elems[0]));
734     assertTrue(q.contains(elems[1]));
735     l.clear();
736     q.drainTo(l);
737 jsr166 1.57 assertEquals(0, q.size());
738     assertEquals(2, l.size());
739 jsr166 1.15 for (int i = 0; i < 2; ++i)
740 jsr166 1.58 assertEquals(elems[i], l.get(i));
741 dl 1.6 }
742    
743     /**
744     * drainTo empties queue
745 jsr166 1.15 */
746 jsr166 1.20 public void testDrainToWithActivePut() throws InterruptedException {
747 dl 1.6 final DelayQueue q = populatedQueue(SIZE);
748 jsr166 1.20 Thread t = new Thread(new CheckedRunnable() {
749     public void realRun() {
750     q.put(new PDelay(SIZE+1));
751     }});
752    
753     t.start();
754     ArrayList l = new ArrayList();
755     q.drainTo(l);
756     assertTrue(l.size() >= SIZE);
757     t.join();
758     assertTrue(q.size() + l.size() >= SIZE);
759 dl 1.6 }
760    
761     /**
762 jsr166 1.38 * drainTo(c, n) empties first min(n, size) elements of queue into c
763 jsr166 1.15 */
764 dl 1.6 public void testDrainToN() {
765     for (int i = 0; i < SIZE + 2; ++i) {
766     DelayQueue q = populatedQueue(SIZE);
767     ArrayList l = new ArrayList();
768     q.drainTo(l, i);
769 jsr166 1.39 int k = (i < SIZE) ? i : SIZE;
770 jsr166 1.58 assertEquals(SIZE-k, q.size());
771     assertEquals(k, l.size());
772 dl 1.6 }
773     }
774    
775 jsr166 1.63 /**
776     * remove(null), contains(null) always return false
777     */
778     public void testNeverContainsNull() {
779     Collection<?> q = populatedQueue(SIZE);
780     assertFalse(q.contains(null));
781     assertFalse(q.remove(null));
782     }
783 dl 1.1 }