ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/DelayQueueTest.java
Revision: 1.3
Committed: Sun Sep 14 20:42:40 2003 UTC (20 years, 7 months ago) by dl
Branch: MAIN
Changes since 1.2: +89 -112 lines
Log Message:
New base class JSR166TestCase

File Contents

# Content
1 /*
2 * Written by members of JCP JSR-166 Expert Group and released to the
3 * public domain. Use, modify, and redistribute this code in any way
4 * without acknowledgement. Other contributors include Andrew Wright,
5 * Jeffrey Hayes, Pat Fischer, Mike Judd.
6 */
7
8 import junit.framework.*;
9 import java.util.*;
10 import java.util.concurrent.*;
11
12 public class DelayQueueTest extends JSR166TestCase {
13 public static void main(String[] args) {
14 junit.textui.TestRunner.run (suite());
15 }
16
17 public static Test suite() {
18 return new TestSuite(DelayQueueTest.class);
19 }
20
21 private static final int NOCAP = Integer.MAX_VALUE;
22
23 // Most Q/BQ tests use Pseudodelays, where delays are all elapsed
24 // (so, no blocking solely for delays) but are still ordered
25
26 static class PDelay implements Delayed {
27 int pseudodelay;
28 PDelay(int i) { pseudodelay = Integer.MIN_VALUE + i; }
29 public int compareTo(Object y) {
30 int i = pseudodelay;
31 int j = ((PDelay)y).pseudodelay;
32 if (i < j) return -1;
33 if (i > j) return 1;
34 return 0;
35 }
36
37 public int compareTo(PDelay y) {
38 int i = pseudodelay;
39 int j = ((PDelay)y).pseudodelay;
40 if (i < j) return -1;
41 if (i > j) return 1;
42 return 0;
43 }
44
45 public boolean equals(Object other) {
46 return ((PDelay)other).pseudodelay == pseudodelay;
47 }
48 public boolean equals(PDelay other) {
49 return ((PDelay)other).pseudodelay == pseudodelay;
50 }
51
52
53 public long getDelay(TimeUnit ignore) {
54 return pseudodelay;
55 }
56 public int intValue() {
57 return pseudodelay;
58 }
59
60 public String toString() {
61 return String.valueOf(pseudodelay);
62 }
63 }
64
65
66
67
68 /**
69 * Create a queue of given size containing consecutive
70 * PDelays 0 ... n.
71 */
72 private DelayQueue populatedQueue(int n) {
73 DelayQueue q = new DelayQueue();
74 assertTrue(q.isEmpty());
75 for(int i = n-1; i >= 0; i-=2)
76 assertTrue(q.offer(new PDelay(i)));
77 for(int i = (n & 1); i < n; i+=2)
78 assertTrue(q.offer(new PDelay(i)));
79 assertFalse(q.isEmpty());
80 assertEquals(NOCAP, q.remainingCapacity());
81 assertEquals(n, q.size());
82 return q;
83 }
84
85 public void testConstructor1(){
86 assertEquals(NOCAP, new DelayQueue().remainingCapacity());
87 }
88
89 public void testConstructor3(){
90
91 try {
92 DelayQueue q = new DelayQueue(null);
93 fail("Cannot make from null collection");
94 }
95 catch (NullPointerException success) {}
96 }
97
98 public void testConstructor4(){
99 try {
100 PDelay[] ints = new PDelay[SIZE];
101 DelayQueue q = new DelayQueue(Arrays.asList(ints));
102 fail("Cannot make with null elements");
103 }
104 catch (NullPointerException success) {}
105 }
106
107 public void testConstructor5(){
108 try {
109 PDelay[] ints = new PDelay[SIZE];
110 for (int i = 0; i < SIZE-1; ++i)
111 ints[i] = new PDelay(i);
112 DelayQueue q = new DelayQueue(Arrays.asList(ints));
113 fail("Cannot make with null elements");
114 }
115 catch (NullPointerException success) {}
116 }
117
118 public void testConstructor6(){
119 try {
120 PDelay[] ints = new PDelay[SIZE];
121 for (int i = 0; i < SIZE; ++i)
122 ints[i] = new PDelay(i);
123 DelayQueue q = new DelayQueue(Arrays.asList(ints));
124 for (int i = 0; i < SIZE; ++i)
125 assertEquals(ints[i], q.poll());
126 }
127 finally {}
128 }
129
130 public void testEmpty() {
131 DelayQueue q = new DelayQueue();
132 assertTrue(q.isEmpty());
133 assertEquals(NOCAP, q.remainingCapacity());
134 q.add(new PDelay(1));
135 assertFalse(q.isEmpty());
136 q.add(new PDelay(2));
137 q.remove();
138 q.remove();
139 assertTrue(q.isEmpty());
140 }
141
142 public void testRemainingCapacity(){
143 DelayQueue q = populatedQueue(SIZE);
144 for (int i = 0; i < SIZE; ++i) {
145 assertEquals(NOCAP, q.remainingCapacity());
146 assertEquals(SIZE-i, q.size());
147 q.remove();
148 }
149 for (int i = 0; i < SIZE; ++i) {
150 assertEquals(NOCAP, q.remainingCapacity());
151 assertEquals(i, q.size());
152 q.add(new PDelay(i));
153 }
154 }
155
156 public void testOfferNull(){
157 try {
158 DelayQueue q = new DelayQueue();
159 q.offer(null);
160 fail("should throw NPE");
161 } catch (NullPointerException success) { }
162 }
163
164 public void testOffer() {
165 DelayQueue q = new DelayQueue();
166 assertTrue(q.offer(new PDelay(0)));
167 assertTrue(q.offer(new PDelay(1)));
168 }
169
170 public void testAdd(){
171 DelayQueue q = new DelayQueue();
172 for (int i = 0; i < SIZE; ++i) {
173 assertEquals(i, q.size());
174 assertTrue(q.add(new PDelay(i)));
175 }
176 }
177
178 public void testAddAll1(){
179 try {
180 DelayQueue q = new DelayQueue();
181 q.addAll(null);
182 fail("Cannot add null collection");
183 }
184 catch (NullPointerException success) {}
185 }
186 public void testAddAll2(){
187 try {
188 DelayQueue q = new DelayQueue();
189 PDelay[] ints = new PDelay[SIZE];
190 q.addAll(Arrays.asList(ints));
191 fail("Cannot add null elements");
192 }
193 catch (NullPointerException success) {}
194 }
195 public void testAddAll3(){
196 try {
197 DelayQueue q = new DelayQueue();
198 PDelay[] ints = new PDelay[SIZE];
199 for (int i = 0; i < SIZE-1; ++i)
200 ints[i] = new PDelay(i);
201 q.addAll(Arrays.asList(ints));
202 fail("Cannot add null elements");
203 }
204 catch (NullPointerException success) {}
205 }
206
207 public void testAddAll5(){
208 try {
209 PDelay[] empty = new PDelay[0];
210 PDelay[] ints = new PDelay[SIZE];
211 for (int i = SIZE-1; i >= 0; --i)
212 ints[i] = new PDelay(i);
213 DelayQueue q = new DelayQueue();
214 assertFalse(q.addAll(Arrays.asList(empty)));
215 assertTrue(q.addAll(Arrays.asList(ints)));
216 for (int i = 0; i < SIZE; ++i)
217 assertEquals(ints[i], q.poll());
218 }
219 finally {}
220 }
221
222 public void testPutNull() {
223 try {
224 DelayQueue q = new DelayQueue();
225 q.put(null);
226 fail("put should throw NPE");
227 }
228 catch (NullPointerException success){
229 }
230 }
231
232 public void testPut() {
233 try {
234 DelayQueue q = new DelayQueue();
235 for (int i = 0; i < SIZE; ++i) {
236 PDelay I = new PDelay(i);
237 q.put(I);
238 assertTrue(q.contains(I));
239 }
240 assertEquals(SIZE, q.size());
241 }
242 finally {
243 }
244 }
245
246 public void testPutWithTake() {
247 final DelayQueue q = new DelayQueue();
248 Thread t = new Thread(new Runnable() {
249 public void run(){
250 int added = 0;
251 try {
252 q.put(new PDelay(0));
253 ++added;
254 q.put(new PDelay(0));
255 ++added;
256 q.put(new PDelay(0));
257 ++added;
258 q.put(new PDelay(0));
259 ++added;
260 threadAssertTrue(added == 4);
261 } finally {
262 }
263 }
264 });
265 try {
266 t.start();
267 Thread.sleep(SHORT_DELAY_MS);
268 q.take();
269 t.interrupt();
270 t.join();
271 } catch (Exception e){
272 fail("Unexpected exception");
273 }
274 }
275
276 public void testTimedOffer() {
277 final DelayQueue q = new DelayQueue();
278 Thread t = new Thread(new Runnable() {
279 public void run(){
280 try {
281 q.put(new PDelay(0));
282 q.put(new PDelay(0));
283 threadAssertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
284 threadAssertTrue(q.offer(new PDelay(0), LONG_DELAY_MS, TimeUnit.MILLISECONDS));
285 } finally { }
286 }
287 });
288
289 try {
290 t.start();
291 Thread.sleep(SMALL_DELAY_MS);
292 t.interrupt();
293 t.join();
294 } catch (Exception e){
295 fail("Unexpected exception");
296 }
297 }
298
299 public void testTake(){
300 try {
301 DelayQueue q = populatedQueue(SIZE);
302 for (int i = 0; i < SIZE; ++i) {
303 assertEquals(new PDelay(i), ((PDelay)q.take()));
304 }
305 } catch (InterruptedException e){
306 fail("Unexpected exception");
307 }
308 }
309
310 public void testTakeFromEmpty() {
311 final DelayQueue q = new DelayQueue();
312 Thread t = new Thread(new Runnable() {
313 public void run(){
314 try {
315 q.take();
316 threadFail("Should block");
317 } catch (InterruptedException success){ }
318 }
319 });
320 try {
321 t.start();
322 Thread.sleep(SHORT_DELAY_MS);
323 t.interrupt();
324 t.join();
325 } catch (Exception e){
326 fail("Unexpected exception");
327 }
328 }
329
330 public void testBlockingTake(){
331 Thread t = new Thread(new Runnable() {
332 public void run() {
333 try {
334 DelayQueue q = populatedQueue(SIZE);
335 for (int i = 0; i < SIZE; ++i) {
336 threadAssertEquals(new PDelay(i), ((PDelay)q.take()));
337 }
338 q.take();
339 threadFail("take should block");
340 } catch (InterruptedException success){
341 }
342 }});
343 t.start();
344 try {
345 Thread.sleep(SHORT_DELAY_MS);
346 t.interrupt();
347 t.join();
348 }
349 catch (InterruptedException ie) {
350 fail("Unexpected exception");
351 }
352 }
353
354
355 public void testPoll(){
356 DelayQueue q = populatedQueue(SIZE);
357 for (int i = 0; i < SIZE; ++i) {
358 assertEquals(new PDelay(i), ((PDelay)q.poll()));
359 }
360 assertNull(q.poll());
361 }
362
363 public void testTimedPoll0() {
364 try {
365 DelayQueue q = populatedQueue(SIZE);
366 for (int i = 0; i < SIZE; ++i) {
367 assertEquals(new PDelay(i), ((PDelay)q.poll(0, TimeUnit.MILLISECONDS)));
368 }
369 assertNull(q.poll(0, TimeUnit.MILLISECONDS));
370 } catch (InterruptedException e){
371 fail("Unexpected exception");
372 }
373 }
374
375 public void testTimedPoll() {
376 try {
377 DelayQueue q = populatedQueue(SIZE);
378 for (int i = 0; i < SIZE; ++i) {
379 assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)));
380 }
381 assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
382 } catch (InterruptedException e){
383 fail("Unexpected exception");
384 }
385 }
386
387 public void testInterruptedTimedPoll(){
388 Thread t = new Thread(new Runnable() {
389 public void run() {
390 try {
391 DelayQueue q = populatedQueue(SIZE);
392 for (int i = 0; i < SIZE; ++i) {
393 threadAssertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)));
394 }
395 threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
396 } catch (InterruptedException success){
397 }
398 }});
399 t.start();
400 try {
401 Thread.sleep(SHORT_DELAY_MS);
402 t.interrupt();
403 t.join();
404 }
405 catch (InterruptedException ie) {
406 fail("Unexpected exception");
407 }
408 }
409
410 public void testTimedPollWithOffer(){
411 final DelayQueue q = new DelayQueue();
412 Thread t = new Thread(new Runnable() {
413 public void run(){
414 try {
415 threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
416 q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
417 q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
418 threadFail("Should block");
419 } catch (InterruptedException success) { }
420 }
421 });
422 try {
423 t.start();
424 Thread.sleep(SMALL_DELAY_MS);
425 assertTrue(q.offer(new PDelay(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
426 t.interrupt();
427 t.join();
428 } catch (Exception e){
429 fail("Unexpected exception");
430 }
431 }
432
433
434 public void testPeek(){
435 DelayQueue q = populatedQueue(SIZE);
436 for (int i = 0; i < SIZE; ++i) {
437 assertEquals(new PDelay(i), ((PDelay)q.peek()));
438 q.poll();
439 assertTrue(q.peek() == null ||
440 i != ((PDelay)q.peek()).intValue());
441 }
442 assertNull(q.peek());
443 }
444
445 public void testElement(){
446 DelayQueue q = populatedQueue(SIZE);
447 for (int i = 0; i < SIZE; ++i) {
448 assertEquals(new PDelay(i), ((PDelay)q.element()));
449 q.poll();
450 }
451 try {
452 q.element();
453 fail("no such element");
454 }
455 catch (NoSuchElementException success) {}
456 }
457
458 public void testRemove(){
459 DelayQueue q = populatedQueue(SIZE);
460 for (int i = 0; i < SIZE; ++i) {
461 assertEquals(new PDelay(i), ((PDelay)q.remove()));
462 }
463 try {
464 q.remove();
465 fail("remove should throw");
466 } catch (NoSuchElementException success){
467 }
468 }
469
470 public void testRemoveElement(){
471 DelayQueue q = populatedQueue(SIZE);
472 for (int i = 1; i < SIZE; i+=2) {
473 assertTrue(q.remove(new PDelay(i)));
474 }
475 for (int i = 0; i < SIZE; i+=2) {
476 assertTrue(q.remove(new PDelay(i)));
477 assertFalse(q.remove(new PDelay(i+1)));
478 }
479 assertTrue(q.isEmpty());
480 }
481
482 public void testContains(){
483 DelayQueue q = populatedQueue(SIZE);
484 for (int i = 0; i < SIZE; ++i) {
485 assertTrue(q.contains(new PDelay(i)));
486 q.poll();
487 assertFalse(q.contains(new PDelay(i)));
488 }
489 }
490
491 public void testClear(){
492 DelayQueue q = populatedQueue(SIZE);
493 q.clear();
494 assertTrue(q.isEmpty());
495 assertEquals(0, q.size());
496 assertEquals(NOCAP, q.remainingCapacity());
497 q.add(new PDelay(1));
498 assertFalse(q.isEmpty());
499 q.clear();
500 assertTrue(q.isEmpty());
501 }
502
503 public void testContainsAll(){
504 DelayQueue q = populatedQueue(SIZE);
505 DelayQueue p = new DelayQueue();
506 for (int i = 0; i < SIZE; ++i) {
507 assertTrue(q.containsAll(p));
508 assertFalse(p.containsAll(q));
509 p.add(new PDelay(i));
510 }
511 assertTrue(p.containsAll(q));
512 }
513
514 public void testRetainAll(){
515 DelayQueue q = populatedQueue(SIZE);
516 DelayQueue p = populatedQueue(SIZE);
517 for (int i = 0; i < SIZE; ++i) {
518 boolean changed = q.retainAll(p);
519 if (i == 0)
520 assertFalse(changed);
521 else
522 assertTrue(changed);
523
524 assertTrue(q.containsAll(p));
525 assertEquals(SIZE-i, q.size());
526 p.remove();
527 }
528 }
529
530 public void testRemoveAll(){
531 for (int i = 1; i < SIZE; ++i) {
532 DelayQueue q = populatedQueue(SIZE);
533 DelayQueue p = populatedQueue(i);
534 assertTrue(q.removeAll(p));
535 assertEquals(SIZE-i, q.size());
536 for (int j = 0; j < i; ++j) {
537 PDelay I = (PDelay)(p.remove());
538 assertFalse(q.contains(I));
539 }
540 }
541 }
542
543 public void testToArray(){
544 DelayQueue q = populatedQueue(SIZE);
545 Object[] o = q.toArray();
546 Arrays.sort(o);
547 try {
548 for(int i = 0; i < o.length; i++)
549 assertEquals(o[i], q.take());
550 } catch (InterruptedException e){
551 fail("Unexpected exception");
552 }
553 }
554
555 public void testToArray2(){
556 DelayQueue q = populatedQueue(SIZE);
557 PDelay[] ints = new PDelay[SIZE];
558 ints = (PDelay[])q.toArray(ints);
559 Arrays.sort(ints);
560 try {
561 for(int i = 0; i < ints.length; i++)
562 assertEquals(ints[i], q.take());
563 } catch (InterruptedException e){
564 fail("Unexpected exception");
565 }
566 }
567
568 public void testIterator(){
569 DelayQueue q = populatedQueue(SIZE);
570 int i = 0;
571 Iterator it = q.iterator();
572 while(it.hasNext()) {
573 assertTrue(q.contains(it.next()));
574 ++i;
575 }
576 assertEquals(i, SIZE);
577 }
578
579 public void testIteratorRemove () {
580
581 final DelayQueue q = new DelayQueue();
582
583 q.add(new PDelay(2));
584 q.add(new PDelay(1));
585 q.add(new PDelay(3));
586
587 Iterator it = q.iterator();
588 it.next();
589 it.remove();
590
591 it = q.iterator();
592 assertEquals(it.next(), new PDelay(2));
593 assertEquals(it.next(), new PDelay(3));
594 assertFalse(it.hasNext());
595 }
596
597
598 public void testToString(){
599 DelayQueue q = populatedQueue(SIZE);
600 String s = q.toString();
601 for (int i = 0; i < SIZE; ++i) {
602 assertTrue(s.indexOf(String.valueOf(Integer.MIN_VALUE+i)) >= 0);
603 }
604 }
605
606 public void testPollInExecutor() {
607
608 final DelayQueue q = new DelayQueue();
609
610 ExecutorService executor = Executors.newFixedThreadPool(2);
611
612 executor.execute(new Runnable() {
613 public void run() {
614 threadAssertNull(q.poll());
615 try {
616 threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
617 threadAssertTrue(q.isEmpty());
618 }
619 catch (InterruptedException e) {
620 threadFail("should not be interrupted");
621 }
622 }
623 });
624
625 executor.execute(new Runnable() {
626 public void run() {
627 try {
628 Thread.sleep(SHORT_DELAY_MS);
629 q.put(new PDelay(1));
630 }
631 catch (InterruptedException e) {
632 threadFail("should not be interrupted");
633 }
634 }
635 });
636
637 joinPool(executor);
638
639 }
640
641 static class NanoDelay implements Delayed {
642 long trigger;
643 NanoDelay(long i) {
644 trigger = System.nanoTime() + i;
645 }
646 public int compareTo(Object y) {
647 long i = trigger;
648 long j = ((NanoDelay)y).trigger;
649 if (i < j) return -1;
650 if (i > j) return 1;
651 return 0;
652 }
653
654 public int compareTo(NanoDelay y) {
655 long i = trigger;
656 long j = ((NanoDelay)y).trigger;
657 if (i < j) return -1;
658 if (i > j) return 1;
659 return 0;
660 }
661
662 public boolean equals(Object other) {
663 return ((NanoDelay)other).trigger == trigger;
664 }
665 public boolean equals(NanoDelay other) {
666 return ((NanoDelay)other).trigger == trigger;
667 }
668
669 public long getDelay(TimeUnit unit) {
670 long n = trigger - System.nanoTime();
671 return unit.convert(n, TimeUnit.NANOSECONDS);
672 }
673
674 public long getTriggerTime() {
675 return trigger;
676 }
677
678 public String toString() {
679 return String.valueOf(trigger);
680 }
681 }
682
683 public void testDelay() {
684 DelayQueue q = new DelayQueue();
685 NanoDelay[] elements = new NanoDelay[SIZE];
686 for (int i = 0; i < SIZE; ++i) {
687 elements[i] = new NanoDelay(1000000000L + 1000000L * (SIZE - i));
688 }
689 for (int i = 0; i < SIZE; ++i) {
690 q.add(elements[i]);
691 }
692
693 try {
694 long last = 0;
695 for (int i = 0; i < SIZE; ++i) {
696 NanoDelay e = (NanoDelay)(q.take());
697 long tt = e.getTriggerTime();
698 assertTrue(tt <= System.nanoTime());
699 if (i != 0)
700 assertTrue(tt >= last);
701 last = tt;
702 }
703 }
704 catch(InterruptedException ie) {
705 fail("Unexpected Exception");
706 }
707 }
708
709 }