ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/DelayQueueTest.java
Revision: 1.65
Committed: Wed Dec 31 20:09:08 2014 UTC (9 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.64: +2 -2 lines
Log Message:
whitespace

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