ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/DelayQueueTest.java
Revision: 1.24
Committed: Sat Nov 21 22:00:46 2009 UTC (14 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.23: +2 -2 lines
Log Message:
reduce scope of check for IE in testTimedOffer*

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     Thread t = new ThreadShouldThrow(InterruptedException.class) {
419     public void realRun() throws InterruptedException {
420     DelayQueue q = populatedQueue(SIZE);
421     for (int i = 0; i < SIZE; ++i) {
422     threadAssertEquals(new PDelay(i), ((PDelay)q.take()));
423     }
424     q.take();
425     }};
426    
427 dl 1.1 t.start();
428 jsr166 1.20 Thread.sleep(SHORT_DELAY_MS);
429     t.interrupt();
430     t.join();
431 dl 1.1 }
432    
433    
434 dl 1.4 /**
435 dl 1.5 * poll succeeds unless empty
436 dl 1.4 */
437     public void testPoll() {
438 dl 1.3 DelayQueue q = populatedQueue(SIZE);
439     for (int i = 0; i < SIZE; ++i) {
440 dl 1.1 assertEquals(new PDelay(i), ((PDelay)q.poll()));
441     }
442 jsr166 1.18 assertNull(q.poll());
443 dl 1.1 }
444    
445 dl 1.4 /**
446 dl 1.5 * timed pool with zero timeout succeeds when non-empty, else times out
447 dl 1.4 */
448 jsr166 1.20 public void testTimedPoll0() throws InterruptedException {
449     DelayQueue q = populatedQueue(SIZE);
450     for (int i = 0; i < SIZE; ++i) {
451     assertEquals(new PDelay(i), ((PDelay)q.poll(0, MILLISECONDS)));
452 jsr166 1.18 }
453 jsr166 1.20 assertNull(q.poll(0, MILLISECONDS));
454 dl 1.1 }
455    
456 dl 1.4 /**
457 dl 1.5 * timed pool with nonzero timeout succeeds when non-empty, else times out
458 dl 1.4 */
459 jsr166 1.20 public void testTimedPoll() throws InterruptedException {
460     DelayQueue q = populatedQueue(SIZE);
461     for (int i = 0; i < SIZE; ++i) {
462     assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS)));
463 jsr166 1.18 }
464 jsr166 1.20 assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
465 dl 1.1 }
466    
467 dl 1.4 /**
468 dl 1.5 * Interrupted timed poll throws InterruptedException instead of
469     * returning timeout status
470 dl 1.4 */
471 jsr166 1.20 public void testInterruptedTimedPoll() throws InterruptedException {
472     Thread t = new Thread(new CheckedRunnable() {
473     public void realRun() throws InterruptedException {
474     DelayQueue q = populatedQueue(SIZE);
475     for (int i = 0; i < SIZE; ++i) {
476     assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS)));
477     }
478     try {
479 jsr166 1.22 q.poll(SMALL_DELAY_MS, MILLISECONDS);
480 jsr166 1.20 shouldThrow();
481     } catch (InterruptedException success) {}
482     }});
483    
484 dl 1.1 t.start();
485 jsr166 1.20 Thread.sleep(SHORT_DELAY_MS);
486     t.interrupt();
487     t.join();
488 dl 1.1 }
489    
490 dl 1.4 /**
491 dl 1.5 * timed poll before a delayed offer fails; after offer succeeds;
492     * on interruption throws
493 dl 1.4 */
494 jsr166 1.20 public void testTimedPollWithOffer() throws InterruptedException {
495 dl 1.1 final DelayQueue q = new DelayQueue();
496 jsr166 1.23 final PDelay pdelay = new PDelay(0);
497 jsr166 1.20 Thread t = new Thread(new CheckedRunnable() {
498     public void realRun() throws InterruptedException {
499     assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
500 jsr166 1.23 assertSame(pdelay, q.poll(LONG_DELAY_MS, MILLISECONDS));
501 jsr166 1.20 try {
502     q.poll(LONG_DELAY_MS, MILLISECONDS);
503     shouldThrow();
504     } catch (InterruptedException success) {}
505     }});
506    
507     t.start();
508     Thread.sleep(SMALL_DELAY_MS);
509 jsr166 1.23 assertTrue(q.offer(pdelay, SHORT_DELAY_MS, MILLISECONDS));
510 jsr166 1.20 t.interrupt();
511     t.join();
512 jsr166 1.15 }
513 dl 1.1
514    
515 dl 1.4 /**
516 dl 1.5 * peek returns next element, or null if empty
517 dl 1.4 */
518     public void testPeek() {
519 dl 1.3 DelayQueue q = populatedQueue(SIZE);
520     for (int i = 0; i < SIZE; ++i) {
521 dl 1.1 assertEquals(new PDelay(i), ((PDelay)q.peek()));
522     q.poll();
523 dl 1.12 if (q.isEmpty())
524     assertNull(q.peek());
525     else
526     assertTrue(i != ((PDelay)q.peek()).intValue());
527 dl 1.1 }
528 jsr166 1.18 assertNull(q.peek());
529 dl 1.1 }
530    
531 dl 1.4 /**
532 dl 1.5 * element returns next element, or throws NSEE if empty
533 dl 1.4 */
534     public void testElement() {
535 dl 1.3 DelayQueue q = populatedQueue(SIZE);
536     for (int i = 0; i < SIZE; ++i) {
537 dl 1.1 assertEquals(new PDelay(i), ((PDelay)q.element()));
538     q.poll();
539     }
540     try {
541     q.element();
542 dl 1.4 shouldThrow();
543 jsr166 1.20 } catch (NoSuchElementException success) {}
544 dl 1.1 }
545    
546 dl 1.4 /**
547 dl 1.5 * remove removes next element, or throws NSEE if empty
548 dl 1.4 */
549     public void testRemove() {
550 dl 1.3 DelayQueue q = populatedQueue(SIZE);
551     for (int i = 0; i < SIZE; ++i) {
552 dl 1.1 assertEquals(new PDelay(i), ((PDelay)q.remove()));
553     }
554     try {
555     q.remove();
556 dl 1.4 shouldThrow();
557 jsr166 1.20 } catch (NoSuchElementException success) {}
558 dl 1.1 }
559    
560 dl 1.4 /**
561 dl 1.5 * remove(x) removes x and returns true if present
562 dl 1.4 */
563     public void testRemoveElement() {
564 dl 1.3 DelayQueue q = populatedQueue(SIZE);
565     for (int i = 1; i < SIZE; i+=2) {
566 dl 1.1 assertTrue(q.remove(new PDelay(i)));
567     }
568 dl 1.3 for (int i = 0; i < SIZE; i+=2) {
569 dl 1.1 assertTrue(q.remove(new PDelay(i)));
570     assertFalse(q.remove(new PDelay(i+1)));
571     }
572 dl 1.2 assertTrue(q.isEmpty());
573 dl 1.1 }
574 jsr166 1.15
575 dl 1.4 /**
576 dl 1.5 * contains(x) reports true when elements added but not yet removed
577 dl 1.4 */
578     public void testContains() {
579 dl 1.3 DelayQueue q = populatedQueue(SIZE);
580     for (int i = 0; i < SIZE; ++i) {
581 dl 1.1 assertTrue(q.contains(new PDelay(i)));
582     q.poll();
583     assertFalse(q.contains(new PDelay(i)));
584     }
585     }
586    
587 dl 1.4 /**
588 dl 1.5 * clear removes all elements
589 dl 1.4 */
590     public void testClear() {
591 dl 1.3 DelayQueue q = populatedQueue(SIZE);
592 dl 1.1 q.clear();
593     assertTrue(q.isEmpty());
594     assertEquals(0, q.size());
595     assertEquals(NOCAP, q.remainingCapacity());
596 dl 1.11 PDelay x = new PDelay(1);
597     q.add(x);
598 dl 1.1 assertFalse(q.isEmpty());
599 dl 1.11 assertTrue(q.contains(x));
600 dl 1.1 q.clear();
601     assertTrue(q.isEmpty());
602     }
603    
604 dl 1.4 /**
605 dl 1.5 * containsAll(c) is true when c contains a subset of elements
606 dl 1.4 */
607     public void testContainsAll() {
608 dl 1.3 DelayQueue q = populatedQueue(SIZE);
609 dl 1.1 DelayQueue p = new DelayQueue();
610 dl 1.3 for (int i = 0; i < SIZE; ++i) {
611 dl 1.1 assertTrue(q.containsAll(p));
612     assertFalse(p.containsAll(q));
613     p.add(new PDelay(i));
614     }
615     assertTrue(p.containsAll(q));
616     }
617    
618 dl 1.4 /**
619 dl 1.5 * retainAll(c) retains only those elements of c and reports true if changed
620 dl 1.4 */
621     public void testRetainAll() {
622 dl 1.3 DelayQueue q = populatedQueue(SIZE);
623     DelayQueue p = populatedQueue(SIZE);
624     for (int i = 0; i < SIZE; ++i) {
625 dl 1.1 boolean changed = q.retainAll(p);
626     if (i == 0)
627     assertFalse(changed);
628     else
629     assertTrue(changed);
630    
631     assertTrue(q.containsAll(p));
632 dl 1.3 assertEquals(SIZE-i, q.size());
633 dl 1.1 p.remove();
634     }
635     }
636    
637 dl 1.4 /**
638 dl 1.5 * removeAll(c) removes only those elements of c and reports true if changed
639 dl 1.4 */
640     public void testRemoveAll() {
641 dl 1.3 for (int i = 1; i < SIZE; ++i) {
642     DelayQueue q = populatedQueue(SIZE);
643     DelayQueue p = populatedQueue(i);
644 dl 1.1 assertTrue(q.removeAll(p));
645 dl 1.3 assertEquals(SIZE-i, q.size());
646 dl 1.1 for (int j = 0; j < i; ++j) {
647     PDelay I = (PDelay)(p.remove());
648     assertFalse(q.contains(I));
649     }
650     }
651     }
652    
653 dl 1.4 /**
654 dl 1.5 * toArray contains all elements
655 dl 1.4 */
656 jsr166 1.20 public void testToArray() throws InterruptedException {
657 dl 1.3 DelayQueue q = populatedQueue(SIZE);
658 jsr166 1.18 Object[] o = q.toArray();
659 dl 1.1 Arrays.sort(o);
660 jsr166 1.18 for (int i = 0; i < o.length; i++)
661     assertEquals(o[i], q.take());
662 dl 1.1 }
663    
664 dl 1.4 /**
665 dl 1.5 * toArray(a) contains all elements
666 dl 1.4 */
667 jsr166 1.20 public void testToArray2() throws InterruptedException {
668 dl 1.3 DelayQueue q = populatedQueue(SIZE);
669 jsr166 1.18 PDelay[] ints = new PDelay[SIZE];
670     ints = (PDelay[])q.toArray(ints);
671 dl 1.1 Arrays.sort(ints);
672 jsr166 1.20 for (int i = 0; i < ints.length; i++)
673     assertEquals(ints[i], q.take());
674 dl 1.1 }
675 dl 1.6
676    
677     /**
678     * toArray(null) throws NPE
679     */
680     public void testToArray_BadArg() {
681 jsr166 1.18 try {
682 dl 1.6 DelayQueue q = populatedQueue(SIZE);
683 jsr166 1.18 Object o[] = q.toArray(null);
684     shouldThrow();
685     } catch (NullPointerException success) {}
686 dl 1.6 }
687    
688     /**
689 dl 1.8 * toArray with incompatible array type throws CCE
690 dl 1.6 */
691     public void testToArray1_BadArg() {
692 jsr166 1.18 try {
693 dl 1.6 DelayQueue q = populatedQueue(SIZE);
694 jsr166 1.18 Object o[] = q.toArray(new String[10] );
695     shouldThrow();
696 jsr166 1.20 } catch (ArrayStoreException success) {}
697 dl 1.6 }
698 jsr166 1.15
699 dl 1.4 /**
700 dl 1.5 * iterator iterates through all elements
701 dl 1.4 */
702     public void testIterator() {
703 dl 1.3 DelayQueue q = populatedQueue(SIZE);
704 dl 1.1 int i = 0;
705 jsr166 1.18 Iterator it = q.iterator();
706 jsr166 1.16 while (it.hasNext()) {
707 dl 1.1 assertTrue(q.contains(it.next()));
708     ++i;
709     }
710 dl 1.3 assertEquals(i, SIZE);
711 dl 1.1 }
712    
713 dl 1.4 /**
714 dl 1.5 * iterator.remove removes current element
715 dl 1.4 */
716 dl 1.1 public void testIteratorRemove () {
717     final DelayQueue q = new DelayQueue();
718     q.add(new PDelay(2));
719     q.add(new PDelay(1));
720     q.add(new PDelay(3));
721     Iterator it = q.iterator();
722     it.next();
723     it.remove();
724     it = q.iterator();
725     assertEquals(it.next(), new PDelay(2));
726     assertEquals(it.next(), new PDelay(3));
727     assertFalse(it.hasNext());
728     }
729    
730    
731 dl 1.4 /**
732 dl 1.5 * toString contains toStrings of elements
733 dl 1.4 */
734     public void testToString() {
735 dl 1.3 DelayQueue q = populatedQueue(SIZE);
736 dl 1.1 String s = q.toString();
737 dl 1.3 for (int i = 0; i < SIZE; ++i) {
738     assertTrue(s.indexOf(String.valueOf(Integer.MIN_VALUE+i)) >= 0);
739 dl 1.1 }
740 jsr166 1.15 }
741 dl 1.1
742 dl 1.4 /**
743 dl 1.5 * offer transfers elements across Executor tasks
744 dl 1.4 */
745 dl 1.1 public void testPollInExecutor() {
746     final DelayQueue q = new DelayQueue();
747     ExecutorService executor = Executors.newFixedThreadPool(2);
748 jsr166 1.20 executor.execute(new CheckedRunnable() {
749     public void realRun() throws InterruptedException {
750 dl 1.3 threadAssertNull(q.poll());
751 jsr166 1.20 threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
752     threadAssertTrue(q.isEmpty());
753     }});
754    
755     executor.execute(new CheckedRunnable() {
756     public void realRun() throws InterruptedException {
757     Thread.sleep(SHORT_DELAY_MS);
758     q.put(new PDelay(1));
759     }});
760 dl 1.1
761 dl 1.3 joinPool(executor);
762 dl 1.1
763     }
764    
765    
766 dl 1.4 /**
767 dl 1.8 * Delayed actions do not occur until their delay elapses
768 dl 1.4 */
769 jsr166 1.20 public void testDelay() throws InterruptedException {
770 dl 1.1 DelayQueue q = new DelayQueue();
771 dl 1.3 NanoDelay[] elements = new NanoDelay[SIZE];
772     for (int i = 0; i < SIZE; ++i) {
773     elements[i] = new NanoDelay(1000000000L + 1000000L * (SIZE - i));
774 dl 1.1 }
775 dl 1.3 for (int i = 0; i < SIZE; ++i) {
776 dl 1.1 q.add(elements[i]);
777     }
778    
779 jsr166 1.20 long last = 0;
780     for (int i = 0; i < SIZE; ++i) {
781     NanoDelay e = (NanoDelay)(q.take());
782     long tt = e.getTriggerTime();
783     assertTrue(tt <= System.nanoTime());
784     if (i != 0)
785     assertTrue(tt >= last);
786     last = tt;
787 dl 1.1 }
788     }
789 dl 1.6
790 dl 1.12 /**
791     * peek of a non-empty queue returns non-null even if not expired
792     */
793     public void testPeekDelayed() {
794     DelayQueue q = new DelayQueue();
795     q.add(new NanoDelay(Long.MAX_VALUE));
796     assert(q.peek() != null);
797     }
798    
799 dl 1.13
800 dl 1.12 /**
801     * poll of a non-empty queue returns null if no expired elements.
802     */
803     public void testPollDelayed() {
804     DelayQueue q = new DelayQueue();
805     q.add(new NanoDelay(Long.MAX_VALUE));
806     assertNull(q.poll());
807     }
808 dl 1.6
809     /**
810 dl 1.13 * timed poll of a non-empty queue returns null if no expired elements.
811     */
812 jsr166 1.20 public void testTimedPollDelayed() throws InterruptedException {
813 dl 1.13 DelayQueue q = new DelayQueue();
814 dl 1.14 q.add(new NanoDelay(LONG_DELAY_MS * 1000000L));
815 jsr166 1.20 assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
816 dl 1.13 }
817    
818     /**
819 dl 1.6 * drainTo(null) throws NPE
820 jsr166 1.15 */
821 dl 1.6 public void testDrainToNull() {
822     DelayQueue q = populatedQueue(SIZE);
823     try {
824     q.drainTo(null);
825     shouldThrow();
826 jsr166 1.20 } catch (NullPointerException success) {}
827 dl 1.6 }
828    
829     /**
830     * drainTo(this) throws IAE
831 jsr166 1.15 */
832 dl 1.6 public void testDrainToSelf() {
833     DelayQueue q = populatedQueue(SIZE);
834     try {
835     q.drainTo(q);
836     shouldThrow();
837 jsr166 1.20 } catch (IllegalArgumentException success) {}
838 dl 1.6 }
839    
840     /**
841     * drainTo(c) empties queue into another collection c
842 jsr166 1.15 */
843 dl 1.6 public void testDrainTo() {
844 dl 1.11 DelayQueue q = new DelayQueue();
845     PDelay[] elems = new PDelay[SIZE];
846     for (int i = 0; i < SIZE; ++i) {
847     elems[i] = new PDelay(i);
848     q.add(elems[i]);
849     }
850 dl 1.6 ArrayList l = new ArrayList();
851     q.drainTo(l);
852     assertEquals(q.size(), 0);
853 jsr166 1.15 for (int i = 0; i < SIZE; ++i)
854 dl 1.11 assertEquals(l.get(i), elems[i]);
855     q.add(elems[0]);
856     q.add(elems[1]);
857     assertFalse(q.isEmpty());
858     assertTrue(q.contains(elems[0]));
859     assertTrue(q.contains(elems[1]));
860     l.clear();
861     q.drainTo(l);
862     assertEquals(q.size(), 0);
863     assertEquals(l.size(), 2);
864 jsr166 1.15 for (int i = 0; i < 2; ++i)
865 dl 1.11 assertEquals(l.get(i), elems[i]);
866 dl 1.6 }
867    
868     /**
869     * drainTo empties queue
870 jsr166 1.15 */
871 jsr166 1.20 public void testDrainToWithActivePut() throws InterruptedException {
872 dl 1.6 final DelayQueue q = populatedQueue(SIZE);
873 jsr166 1.20 Thread t = new Thread(new CheckedRunnable() {
874     public void realRun() {
875     q.put(new PDelay(SIZE+1));
876     }});
877    
878     t.start();
879     ArrayList l = new ArrayList();
880     q.drainTo(l);
881     assertTrue(l.size() >= SIZE);
882     t.join();
883     assertTrue(q.size() + l.size() >= SIZE);
884 dl 1.6 }
885    
886     /**
887     * drainTo(null, n) throws NPE
888 jsr166 1.15 */
889 dl 1.6 public void testDrainToNullN() {
890     DelayQueue q = populatedQueue(SIZE);
891     try {
892     q.drainTo(null, 0);
893     shouldThrow();
894 jsr166 1.20 } catch (NullPointerException success) {}
895 dl 1.6 }
896    
897     /**
898     * drainTo(this, n) throws IAE
899 jsr166 1.15 */
900 dl 1.6 public void testDrainToSelfN() {
901     DelayQueue q = populatedQueue(SIZE);
902     try {
903     q.drainTo(q, 0);
904     shouldThrow();
905 jsr166 1.20 } catch (IllegalArgumentException success) {}
906 dl 1.6 }
907    
908     /**
909     * drainTo(c, n) empties first max {n, size} elements of queue into c
910 jsr166 1.15 */
911 dl 1.6 public void testDrainToN() {
912     for (int i = 0; i < SIZE + 2; ++i) {
913     DelayQueue q = populatedQueue(SIZE);
914     ArrayList l = new ArrayList();
915     q.drainTo(l, i);
916     int k = (i < SIZE)? i : SIZE;
917     assertEquals(q.size(), SIZE-k);
918     assertEquals(l.size(), k);
919     }
920     }
921    
922 dl 1.1
923     }