ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/DelayQueueTest.java
(Generate patch)

Comparing jsr166/src/test/tck/DelayQueueTest.java (file contents):
Revision 1.3 by dl, Sun Sep 14 20:42:40 2003 UTC vs.
Revision 1.5 by dl, Thu Sep 25 11:02:41 2003 UTC

# Line 20 | Line 20 | public class DelayQueueTest extends JSR1
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 <
23 >    /**
24 >     * A delayed implmentation for testing.
25 >     * Most  tests use Pseudodelays, where delays are all elapsed
26 >     * (so, no blocking solely for delays) but are still ordered
27 >     */
28      static class PDelay implements Delayed {
29          int pseudodelay;
30          PDelay(int i) { pseudodelay = Integer.MIN_VALUE + i; }
# Line 63 | Line 65 | public class DelayQueueTest extends JSR1
65      }
66  
67  
68 +    /**
69 +     * Delayed implementation that actually delays
70 +     */
71 +    static class NanoDelay implements Delayed {
72 +        long trigger;
73 +        NanoDelay(long i) {
74 +            trigger = System.nanoTime() + i;
75 +        }
76 +        public int compareTo(Object y) {
77 +            long i = trigger;
78 +            long j = ((NanoDelay)y).trigger;
79 +            if (i < j) return -1;
80 +            if (i > j) return 1;
81 +            return 0;
82 +        }
83 +
84 +        public int compareTo(NanoDelay y) {
85 +            long i = trigger;
86 +            long j = ((NanoDelay)y).trigger;
87 +            if (i < j) return -1;
88 +            if (i > j) return 1;
89 +            return 0;
90 +        }
91 +
92 +        public boolean equals(Object other) {
93 +            return ((NanoDelay)other).trigger == trigger;
94 +        }
95 +        public boolean equals(NanoDelay other) {
96 +            return ((NanoDelay)other).trigger == trigger;
97 +        }
98 +
99 +        public long getDelay(TimeUnit unit) {
100 +            long n = trigger - System.nanoTime();
101 +            return unit.convert(n, TimeUnit.NANOSECONDS);
102 +        }
103 +
104 +        public long getTriggerTime() {
105 +            return trigger;
106 +        }
107 +
108 +        public String toString() {
109 +            return String.valueOf(trigger);
110 +        }
111 +    }
112  
113  
114      /**
# Line 82 | Line 128 | public class DelayQueueTest extends JSR1
128          return q;
129      }
130  
131 <    public void testConstructor1(){
131 >    /**
132 >     * A new queue has unbounded capacity
133 >     */
134 >    public void testConstructor1() {
135          assertEquals(NOCAP, new DelayQueue().remainingCapacity());
136      }
137  
138 <    public void testConstructor3(){
139 <
138 >    /**
139 >     * Initializing from null Collection throws NPE
140 >     */
141 >    public void testConstructor3() {
142          try {
143              DelayQueue q = new DelayQueue(null);
144 <            fail("Cannot make from null collection");
144 >            shouldThrow();
145          }
146          catch (NullPointerException success) {}
147      }
148  
149 <    public void testConstructor4(){
149 >    /**
150 >     * Initializing from Collection of null elements throws NPE
151 >     */
152 >    public void testConstructor4() {
153          try {
154              PDelay[] ints = new PDelay[SIZE];
155              DelayQueue q = new DelayQueue(Arrays.asList(ints));
156 <            fail("Cannot make with null elements");
156 >            shouldThrow();
157          }
158          catch (NullPointerException success) {}
159      }
160  
161 <    public void testConstructor5(){
161 >    /**
162 >     * Initializing from Collection with some null elements throws NPE
163 >     */
164 >    public void testConstructor5() {
165          try {
166              PDelay[] ints = new PDelay[SIZE];
167              for (int i = 0; i < SIZE-1; ++i)
168                  ints[i] = new PDelay(i);
169              DelayQueue q = new DelayQueue(Arrays.asList(ints));
170 <            fail("Cannot make with null elements");
170 >            shouldThrow();
171          }
172          catch (NullPointerException success) {}
173      }
174  
175 <    public void testConstructor6(){
175 >    /**
176 >     * Queue contains all elements of collection used to initialize
177 >     */
178 >    public void testConstructor6() {
179          try {
180              PDelay[] ints = new PDelay[SIZE];
181              for (int i = 0; i < SIZE; ++i)
# Line 127 | Line 187 | public class DelayQueueTest extends JSR1
187          finally {}
188      }
189  
190 +    /**
191 +     * isEmpty is true before add, false after
192 +     */
193      public void testEmpty() {
194          DelayQueue q = new DelayQueue();
195          assertTrue(q.isEmpty());
# Line 139 | Line 202 | public class DelayQueueTest extends JSR1
202          assertTrue(q.isEmpty());
203      }
204  
205 <    public void testRemainingCapacity(){
205 >    /**
206 >     * remainingCapacity does not change when elementa added or removed,
207 >     * but size does
208 >     */
209 >    public void testRemainingCapacity() {
210          DelayQueue q = populatedQueue(SIZE);
211          for (int i = 0; i < SIZE; ++i) {
212              assertEquals(NOCAP, q.remainingCapacity());
# Line 153 | Line 220 | public class DelayQueueTest extends JSR1
220          }
221      }
222  
223 <    public void testOfferNull(){
223 >    /**
224 >     * offer(null) throws NPE
225 >     */
226 >    public void testOfferNull() {
227          try {
228              DelayQueue q = new DelayQueue();
229              q.offer(null);
230 <            fail("should throw NPE");
230 >            shouldThrow();
231          } catch (NullPointerException success) { }  
232      }
233  
234 +    /**
235 +     * offer non-null succeeds
236 +     */
237      public void testOffer() {
238          DelayQueue q = new DelayQueue();
239          assertTrue(q.offer(new PDelay(0)));
240          assertTrue(q.offer(new PDelay(1)));
241      }
242  
243 <    public void testAdd(){
243 >    /**
244 >     * add succeeds
245 >     */
246 >    public void testAdd() {
247          DelayQueue q = new DelayQueue();
248          for (int i = 0; i < SIZE; ++i) {
249              assertEquals(i, q.size());
# Line 175 | Line 251 | public class DelayQueueTest extends JSR1
251          }
252      }
253  
254 <    public void testAddAll1(){
254 >    /**
255 >     * addAll(null) throws NPE
256 >     */
257 >    public void testAddAll1() {
258          try {
259              DelayQueue q = new DelayQueue();
260              q.addAll(null);
261 <            fail("Cannot add null collection");
261 >            shouldThrow();
262          }
263          catch (NullPointerException success) {}
264      }
265 <    public void testAddAll2(){
265 >    /**
266 >     * addAll of a collection with null elements throws NPE
267 >     */
268 >    public void testAddAll2() {
269          try {
270              DelayQueue q = new DelayQueue();
271              PDelay[] ints = new PDelay[SIZE];
272              q.addAll(Arrays.asList(ints));
273 <            fail("Cannot add null elements");
273 >            shouldThrow();
274          }
275          catch (NullPointerException success) {}
276      }
277 <    public void testAddAll3(){
277 >    /**
278 >     * addAll of a collection with any null elements throws NPE after
279 >     * possibly adding some elements
280 >     */
281 >    public void testAddAll3() {
282          try {
283              DelayQueue q = new DelayQueue();
284              PDelay[] ints = new PDelay[SIZE];
285              for (int i = 0; i < SIZE-1; ++i)
286                  ints[i] = new PDelay(i);
287              q.addAll(Arrays.asList(ints));
288 <            fail("Cannot add null elements");
288 >            shouldThrow();
289          }
290          catch (NullPointerException success) {}
291      }
292  
293 <    public void testAddAll5(){
293 >    /**
294 >     * Queue contains all elements of successful addAll
295 >     */
296 >    public void testAddAll5() {
297          try {
298              PDelay[] empty = new PDelay[0];
299              PDelay[] ints = new PDelay[SIZE];
# Line 219 | Line 308 | public class DelayQueueTest extends JSR1
308          finally {}
309      }
310  
311 +    /**
312 +     * put(null) throws NPE
313 +     */
314       public void testPutNull() {
315          try {
316              DelayQueue q = new DelayQueue();
317              q.put(null);
318 <            fail("put should throw NPE");
318 >            shouldThrow();
319          }
320          catch (NullPointerException success){
321          }  
322       }
323  
324 +    /**
325 +     * all elements successfully put are contained
326 +     */
327       public void testPut() {
328           try {
329               DelayQueue q = new DelayQueue();
# Line 243 | Line 338 | public class DelayQueueTest extends JSR1
338          }
339      }
340  
341 +    /**
342 +     * put doesn't block waiting for take
343 +     */
344      public void testPutWithTake() {
345          final DelayQueue q = new DelayQueue();
346          Thread t = new Thread(new Runnable() {
347 <                public void run(){
347 >                public void run() {
348                      int added = 0;
349                      try {
350                          q.put(new PDelay(0));
# Line 269 | Line 367 | public class DelayQueueTest extends JSR1
367              t.interrupt();
368              t.join();
369          } catch (Exception e){
370 <            fail("Unexpected exception");
370 >            unexpectedException();
371          }
372      }
373  
374 +    /**
375 +     * timed offer does not time out
376 +     */
377      public void testTimedOffer() {
378          final DelayQueue q = new DelayQueue();
379          Thread t = new Thread(new Runnable() {
380 <                public void run(){
380 >                public void run() {
381                      try {
382                          q.put(new PDelay(0));
383                          q.put(new PDelay(0));
# Line 292 | Line 393 | public class DelayQueueTest extends JSR1
393              t.interrupt();
394              t.join();
395          } catch (Exception e){
396 <            fail("Unexpected exception");
396 >            unexpectedException();
397          }
398      }
399  
400 <    public void testTake(){
400 >    /**
401 >     * take retrieves elements in priority order
402 >     */
403 >    public void testTake() {
404          try {
405              DelayQueue q = populatedQueue(SIZE);
406              for (int i = 0; i < SIZE; ++i) {
407                  assertEquals(new PDelay(i), ((PDelay)q.take()));
408              }
409          } catch (InterruptedException e){
410 <            fail("Unexpected exception");
410 >            unexpectedException();
411          }  
412      }
413  
414 +    /**
415 +     * take blocks interruptibly when empty
416 +     */
417      public void testTakeFromEmpty() {
418          final DelayQueue q = new DelayQueue();
419          Thread t = new Thread(new Runnable() {
420 <                public void run(){
420 >                public void run() {
421                      try {
422                          q.take();
423 <                        threadFail("Should block");
423 >                        threadShouldThrow();
424                      } catch (InterruptedException success){ }                
425                  }
426              });
# Line 323 | Line 430 | public class DelayQueueTest extends JSR1
430              t.interrupt();
431              t.join();
432          } catch (Exception e){
433 <            fail("Unexpected exception");
433 >            unexpectedException();
434          }
435      }
436  
437 <    public void testBlockingTake(){
437 >    /**
438 >     * Take removes existing elements until empty, then blocks interruptibly
439 >     */
440 >    public void testBlockingTake() {
441          Thread t = new Thread(new Runnable() {
442                  public void run() {
443                      try {
# Line 336 | Line 446 | public class DelayQueueTest extends JSR1
446                              threadAssertEquals(new PDelay(i), ((PDelay)q.take()));
447                          }
448                          q.take();
449 <                        threadFail("take should block");
449 >                        threadShouldThrow();
450                      } catch (InterruptedException success){
451                      }  
452                  }});
# Line 347 | Line 457 | public class DelayQueueTest extends JSR1
457             t.join();
458          }
459          catch (InterruptedException ie) {
460 <            fail("Unexpected exception");
460 >            unexpectedException();
461          }
462      }
463  
464  
465 <    public void testPoll(){
465 >    /**
466 >     * poll succeeds unless empty
467 >     */
468 >    public void testPoll() {
469          DelayQueue q = populatedQueue(SIZE);
470          for (int i = 0; i < SIZE; ++i) {
471              assertEquals(new PDelay(i), ((PDelay)q.poll()));
# Line 360 | Line 473 | public class DelayQueueTest extends JSR1
473          assertNull(q.poll());
474      }
475  
476 +    /**
477 +     * timed pool with zero timeout succeeds when non-empty, else times out
478 +     */
479      public void testTimedPoll0() {
480          try {
481              DelayQueue q = populatedQueue(SIZE);
# Line 368 | Line 484 | public class DelayQueueTest extends JSR1
484              }
485              assertNull(q.poll(0, TimeUnit.MILLISECONDS));
486          } catch (InterruptedException e){
487 <            fail("Unexpected exception");
487 >            unexpectedException();
488          }  
489      }
490  
491 +    /**
492 +     * timed pool with nonzero timeout succeeds when non-empty, else times out
493 +     */
494      public void testTimedPoll() {
495          try {
496              DelayQueue q = populatedQueue(SIZE);
# Line 380 | Line 499 | public class DelayQueueTest extends JSR1
499              }
500              assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
501          } catch (InterruptedException e){
502 <            fail("Unexpected exception");
502 >            unexpectedException();
503          }  
504      }
505  
506 <    public void testInterruptedTimedPoll(){
506 >    /**
507 >     * Interrupted timed poll throws InterruptedException instead of
508 >     * returning timeout status
509 >     */
510 >    public void testInterruptedTimedPoll() {
511          Thread t = new Thread(new Runnable() {
512                  public void run() {
513                      try {
# Line 403 | Line 526 | public class DelayQueueTest extends JSR1
526             t.join();
527          }
528          catch (InterruptedException ie) {
529 <            fail("Unexpected exception");
529 >            unexpectedException();
530          }
531      }
532  
533 <    public void testTimedPollWithOffer(){
533 >    /**
534 >     *  timed poll before a delayed offer fails; after offer succeeds;
535 >     *  on interruption throws
536 >     */
537 >    public void testTimedPollWithOffer() {
538          final DelayQueue q = new DelayQueue();
539          Thread t = new Thread(new Runnable() {
540 <                public void run(){
540 >                public void run() {
541                      try {
542                          threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
543                          q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
# Line 426 | Line 553 | public class DelayQueueTest extends JSR1
553              t.interrupt();
554              t.join();
555          } catch (Exception e){
556 <            fail("Unexpected exception");
556 >            unexpectedException();
557          }
558      }  
559  
560  
561 <    public void testPeek(){
561 >    /**
562 >     * peek returns next element, or null if empty
563 >     */
564 >    public void testPeek() {
565          DelayQueue q = populatedQueue(SIZE);
566          for (int i = 0; i < SIZE; ++i) {
567              assertEquals(new PDelay(i), ((PDelay)q.peek()));
# Line 442 | Line 572 | public class DelayQueueTest extends JSR1
572          assertNull(q.peek());
573      }
574  
575 <    public void testElement(){
575 >    /**
576 >     * element returns next element, or throws NSEE if empty
577 >     */
578 >    public void testElement() {
579          DelayQueue q = populatedQueue(SIZE);
580          for (int i = 0; i < SIZE; ++i) {
581              assertEquals(new PDelay(i), ((PDelay)q.element()));
# Line 450 | Line 583 | public class DelayQueueTest extends JSR1
583          }
584          try {
585              q.element();
586 <            fail("no such element");
586 >            shouldThrow();
587          }
588          catch (NoSuchElementException success) {}
589      }
590  
591 <    public void testRemove(){
591 >    /**
592 >     * remove removes next element, or throws NSEE if empty
593 >     */
594 >    public void testRemove() {
595          DelayQueue q = populatedQueue(SIZE);
596          for (int i = 0; i < SIZE; ++i) {
597              assertEquals(new PDelay(i), ((PDelay)q.remove()));
598          }
599          try {
600              q.remove();
601 <            fail("remove should throw");
601 >            shouldThrow();
602          } catch (NoSuchElementException success){
603          }  
604      }
605  
606 <    public void testRemoveElement(){
606 >    /**
607 >     * remove(x) removes x and returns true if present
608 >     */
609 >    public void testRemoveElement() {
610          DelayQueue q = populatedQueue(SIZE);
611          for (int i = 1; i < SIZE; i+=2) {
612              assertTrue(q.remove(new PDelay(i)));
# Line 479 | Line 618 | public class DelayQueueTest extends JSR1
618          assertTrue(q.isEmpty());
619      }
620          
621 <    public void testContains(){
621 >    /**
622 >     * contains(x) reports true when elements added but not yet removed
623 >     */
624 >    public void testContains() {
625          DelayQueue q = populatedQueue(SIZE);
626          for (int i = 0; i < SIZE; ++i) {
627              assertTrue(q.contains(new PDelay(i)));
# Line 488 | Line 630 | public class DelayQueueTest extends JSR1
630          }
631      }
632  
633 <    public void testClear(){
633 >    /**
634 >     * clear removes all elements
635 >     */
636 >    public void testClear() {
637          DelayQueue q = populatedQueue(SIZE);
638          q.clear();
639          assertTrue(q.isEmpty());
# Line 500 | Line 645 | public class DelayQueueTest extends JSR1
645          assertTrue(q.isEmpty());
646      }
647  
648 <    public void testContainsAll(){
648 >    /**
649 >     * containsAll(c) is true when c contains a subset of elements
650 >     */
651 >    public void testContainsAll() {
652          DelayQueue q = populatedQueue(SIZE);
653          DelayQueue p = new DelayQueue();
654          for (int i = 0; i < SIZE; ++i) {
# Line 511 | Line 659 | public class DelayQueueTest extends JSR1
659          assertTrue(p.containsAll(q));
660      }
661  
662 <    public void testRetainAll(){
662 >    /**
663 >     * retainAll(c) retains only those elements of c and reports true if changed
664 >     */
665 >    public void testRetainAll() {
666          DelayQueue q = populatedQueue(SIZE);
667          DelayQueue p = populatedQueue(SIZE);
668          for (int i = 0; i < SIZE; ++i) {
# Line 527 | Line 678 | public class DelayQueueTest extends JSR1
678          }
679      }
680  
681 <    public void testRemoveAll(){
681 >    /**
682 >     * removeAll(c) removes only those elements of c and reports true if changed
683 >     */
684 >    public void testRemoveAll() {
685          for (int i = 1; i < SIZE; ++i) {
686              DelayQueue q = populatedQueue(SIZE);
687              DelayQueue p = populatedQueue(i);
# Line 540 | Line 694 | public class DelayQueueTest extends JSR1
694          }
695      }
696  
697 <    public void testToArray(){
697 >    /**
698 >     * toArray contains all elements
699 >     */
700 >    public void testToArray() {
701          DelayQueue q = populatedQueue(SIZE);
702          Object[] o = q.toArray();
703          Arrays.sort(o);
# Line 548 | Line 705 | public class DelayQueueTest extends JSR1
705          for(int i = 0; i < o.length; i++)
706              assertEquals(o[i], q.take());
707          } catch (InterruptedException e){
708 <            fail("Unexpected exception");
708 >            unexpectedException();
709          }    
710      }
711  
712 <    public void testToArray2(){
712 >    /**
713 >     * toArray(a) contains all elements
714 >     */
715 >    public void testToArray2() {
716          DelayQueue q = populatedQueue(SIZE);
717          PDelay[] ints = new PDelay[SIZE];
718          ints = (PDelay[])q.toArray(ints);
# Line 561 | Line 721 | public class DelayQueueTest extends JSR1
721              for(int i = 0; i < ints.length; i++)
722                  assertEquals(ints[i], q.take());
723          } catch (InterruptedException e){
724 <            fail("Unexpected exception");
724 >            unexpectedException();
725          }    
726      }
727      
728 <    public void testIterator(){
728 >    /**
729 >     * iterator iterates through all elements
730 >     */
731 >    public void testIterator() {
732          DelayQueue q = populatedQueue(SIZE);
733          int i = 0;
734          Iterator it = q.iterator();
# Line 576 | Line 739 | public class DelayQueueTest extends JSR1
739          assertEquals(i, SIZE);
740      }
741  
742 +    /**
743 +     * iterator.remove removes current element
744 +     */
745      public void testIteratorRemove () {
580
746          final DelayQueue q = new DelayQueue();
582
747          q.add(new PDelay(2));
748          q.add(new PDelay(1));
749          q.add(new PDelay(3));
586
750          Iterator it = q.iterator();
751          it.next();
752          it.remove();
590
753          it = q.iterator();
754          assertEquals(it.next(), new PDelay(2));
755          assertEquals(it.next(), new PDelay(3));
# Line 595 | Line 757 | public class DelayQueueTest extends JSR1
757      }
758  
759  
760 <    public void testToString(){
760 >    /**
761 >     * toString contains toStrings of elements
762 >     */
763 >    public void testToString() {
764          DelayQueue q = populatedQueue(SIZE);
765          String s = q.toString();
766          for (int i = 0; i < SIZE; ++i) {
# Line 603 | Line 768 | public class DelayQueueTest extends JSR1
768          }
769      }        
770  
771 +    /**
772 +     * offer transfers elements across Executor tasks
773 +     */
774      public void testPollInExecutor() {
607
775          final DelayQueue q = new DelayQueue();
609
776          ExecutorService executor = Executors.newFixedThreadPool(2);
611
777          executor.execute(new Runnable() {
778              public void run() {
779                  threadAssertNull(q.poll());
# Line 617 | Line 782 | public class DelayQueueTest extends JSR1
782                      threadAssertTrue(q.isEmpty());
783                  }
784                  catch (InterruptedException e) {
785 <                    threadFail("should not be interrupted");
785 >                    threadUnexpectedException();
786                  }
787              }
788          });
# Line 629 | Line 794 | public class DelayQueueTest extends JSR1
794                      q.put(new PDelay(1));
795                  }
796                  catch (InterruptedException e) {
797 <                    threadFail("should not be interrupted");
797 >                    threadUnexpectedException();
798                  }
799              }
800          });
636        
801          joinPool(executor);
802  
803      }
804  
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    }
805  
806 +    /**
807 +     * Dekayed actions do not occur until their delay elapses
808 +     */
809      public void testDelay() {
810          DelayQueue q = new DelayQueue();
811          NanoDelay[] elements = new NanoDelay[SIZE];
# Line 702 | Line 828 | public class DelayQueueTest extends JSR1
828              }
829          }
830          catch(InterruptedException ie) {
831 <            fail("Unexpected Exception");
831 >            unexpectedException();
832          }
833      }
834  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines