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.4 by dl, Sat Sep 20 18:20:07 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 Q/BQ 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 >     *
133 >     */
134 >    public void testConstructor1() {
135          assertEquals(NOCAP, new DelayQueue().remainingCapacity());
136      }
137  
138 <    public void testConstructor3(){
139 <
138 >    /**
139 >     *
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 >     *
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 >     *
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 >     *
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 +     *
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 >     *
207 >     */
208 >    public void testRemainingCapacity() {
209          DelayQueue q = populatedQueue(SIZE);
210          for (int i = 0; i < SIZE; ++i) {
211              assertEquals(NOCAP, q.remainingCapacity());
# Line 153 | Line 219 | public class DelayQueueTest extends JSR1
219          }
220      }
221  
222 <    public void testOfferNull(){
222 >    /**
223 >     *
224 >     */
225 >    public void testOfferNull() {
226          try {
227              DelayQueue q = new DelayQueue();
228              q.offer(null);
229 <            fail("should throw NPE");
229 >            shouldThrow();
230          } catch (NullPointerException success) { }  
231      }
232  
233 +    /**
234 +     *
235 +     */
236      public void testOffer() {
237          DelayQueue q = new DelayQueue();
238          assertTrue(q.offer(new PDelay(0)));
239          assertTrue(q.offer(new PDelay(1)));
240      }
241  
242 <    public void testAdd(){
242 >    /**
243 >     *
244 >     */
245 >    public void testAdd() {
246          DelayQueue q = new DelayQueue();
247          for (int i = 0; i < SIZE; ++i) {
248              assertEquals(i, q.size());
# Line 175 | Line 250 | public class DelayQueueTest extends JSR1
250          }
251      }
252  
253 <    public void testAddAll1(){
253 >    /**
254 >     *
255 >     */
256 >    public void testAddAll1() {
257          try {
258              DelayQueue q = new DelayQueue();
259              q.addAll(null);
260 <            fail("Cannot add null collection");
260 >            shouldThrow();
261          }
262          catch (NullPointerException success) {}
263      }
264 <    public void testAddAll2(){
264 >    /**
265 >     *
266 >     */
267 >    public void testAddAll2() {
268          try {
269              DelayQueue q = new DelayQueue();
270              PDelay[] ints = new PDelay[SIZE];
271              q.addAll(Arrays.asList(ints));
272 <            fail("Cannot add null elements");
272 >            shouldThrow();
273          }
274          catch (NullPointerException success) {}
275      }
276 <    public void testAddAll3(){
276 >    /**
277 >     *
278 >     */
279 >    public void testAddAll3() {
280          try {
281              DelayQueue q = new DelayQueue();
282              PDelay[] ints = new PDelay[SIZE];
283              for (int i = 0; i < SIZE-1; ++i)
284                  ints[i] = new PDelay(i);
285              q.addAll(Arrays.asList(ints));
286 <            fail("Cannot add null elements");
286 >            shouldThrow();
287          }
288          catch (NullPointerException success) {}
289      }
290  
291 <    public void testAddAll5(){
291 >    /**
292 >     *
293 >     */
294 >    public void testAddAll5() {
295          try {
296              PDelay[] empty = new PDelay[0];
297              PDelay[] ints = new PDelay[SIZE];
# Line 219 | Line 306 | public class DelayQueueTest extends JSR1
306          finally {}
307      }
308  
309 +    /**
310 +     *
311 +     */
312       public void testPutNull() {
313          try {
314              DelayQueue q = new DelayQueue();
315              q.put(null);
316 <            fail("put should throw NPE");
316 >            shouldThrow();
317          }
318          catch (NullPointerException success){
319          }  
320       }
321  
322 +    /**
323 +     *
324 +     */
325       public void testPut() {
326           try {
327               DelayQueue q = new DelayQueue();
# Line 243 | Line 336 | public class DelayQueueTest extends JSR1
336          }
337      }
338  
339 +    /**
340 +     *
341 +     */
342      public void testPutWithTake() {
343          final DelayQueue q = new DelayQueue();
344          Thread t = new Thread(new Runnable() {
345 <                public void run(){
345 >                public void run() {
346                      int added = 0;
347                      try {
348                          q.put(new PDelay(0));
# Line 269 | Line 365 | public class DelayQueueTest extends JSR1
365              t.interrupt();
366              t.join();
367          } catch (Exception e){
368 <            fail("Unexpected exception");
368 >            unexpectedException();
369          }
370      }
371  
372 +    /**
373 +     *
374 +     */
375      public void testTimedOffer() {
376          final DelayQueue q = new DelayQueue();
377          Thread t = new Thread(new Runnable() {
378 <                public void run(){
378 >                public void run() {
379                      try {
380                          q.put(new PDelay(0));
381                          q.put(new PDelay(0));
# Line 292 | Line 391 | public class DelayQueueTest extends JSR1
391              t.interrupt();
392              t.join();
393          } catch (Exception e){
394 <            fail("Unexpected exception");
394 >            unexpectedException();
395          }
396      }
397  
398 <    public void testTake(){
398 >    /**
399 >     *
400 >     */
401 >    public void testTake() {
402          try {
403              DelayQueue q = populatedQueue(SIZE);
404              for (int i = 0; i < SIZE; ++i) {
405                  assertEquals(new PDelay(i), ((PDelay)q.take()));
406              }
407          } catch (InterruptedException e){
408 <            fail("Unexpected exception");
408 >            unexpectedException();
409          }  
410      }
411  
412 +    /**
413 +     *
414 +     */
415      public void testTakeFromEmpty() {
416          final DelayQueue q = new DelayQueue();
417          Thread t = new Thread(new Runnable() {
418 <                public void run(){
418 >                public void run() {
419                      try {
420                          q.take();
421 <                        threadFail("Should block");
421 >                        threadShouldThrow();
422                      } catch (InterruptedException success){ }                
423                  }
424              });
# Line 323 | Line 428 | public class DelayQueueTest extends JSR1
428              t.interrupt();
429              t.join();
430          } catch (Exception e){
431 <            fail("Unexpected exception");
431 >            unexpectedException();
432          }
433      }
434  
435 <    public void testBlockingTake(){
435 >    /**
436 >     *
437 >     */
438 >    public void testBlockingTake() {
439          Thread t = new Thread(new Runnable() {
440                  public void run() {
441                      try {
# Line 336 | Line 444 | public class DelayQueueTest extends JSR1
444                              threadAssertEquals(new PDelay(i), ((PDelay)q.take()));
445                          }
446                          q.take();
447 <                        threadFail("take should block");
447 >                        threadShouldThrow();
448                      } catch (InterruptedException success){
449                      }  
450                  }});
# Line 347 | Line 455 | public class DelayQueueTest extends JSR1
455             t.join();
456          }
457          catch (InterruptedException ie) {
458 <            fail("Unexpected exception");
458 >            unexpectedException();
459          }
460      }
461  
462  
463 <    public void testPoll(){
463 >    /**
464 >     *
465 >     */
466 >    public void testPoll() {
467          DelayQueue q = populatedQueue(SIZE);
468          for (int i = 0; i < SIZE; ++i) {
469              assertEquals(new PDelay(i), ((PDelay)q.poll()));
# Line 360 | Line 471 | public class DelayQueueTest extends JSR1
471          assertNull(q.poll());
472      }
473  
474 +    /**
475 +     *
476 +     */
477      public void testTimedPoll0() {
478          try {
479              DelayQueue q = populatedQueue(SIZE);
# Line 368 | Line 482 | public class DelayQueueTest extends JSR1
482              }
483              assertNull(q.poll(0, TimeUnit.MILLISECONDS));
484          } catch (InterruptedException e){
485 <            fail("Unexpected exception");
485 >            unexpectedException();
486          }  
487      }
488  
489 +    /**
490 +     *
491 +     */
492      public void testTimedPoll() {
493          try {
494              DelayQueue q = populatedQueue(SIZE);
# Line 380 | Line 497 | public class DelayQueueTest extends JSR1
497              }
498              assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
499          } catch (InterruptedException e){
500 <            fail("Unexpected exception");
500 >            unexpectedException();
501          }  
502      }
503  
504 <    public void testInterruptedTimedPoll(){
504 >    /**
505 >     *
506 >     */
507 >    public void testInterruptedTimedPoll() {
508          Thread t = new Thread(new Runnable() {
509                  public void run() {
510                      try {
# Line 403 | Line 523 | public class DelayQueueTest extends JSR1
523             t.join();
524          }
525          catch (InterruptedException ie) {
526 <            fail("Unexpected exception");
526 >            unexpectedException();
527          }
528      }
529  
530 <    public void testTimedPollWithOffer(){
530 >    /**
531 >     *
532 >     */
533 >    public void testTimedPollWithOffer() {
534          final DelayQueue q = new DelayQueue();
535          Thread t = new Thread(new Runnable() {
536 <                public void run(){
536 >                public void run() {
537                      try {
538                          threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
539                          q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
# Line 426 | Line 549 | public class DelayQueueTest extends JSR1
549              t.interrupt();
550              t.join();
551          } catch (Exception e){
552 <            fail("Unexpected exception");
552 >            unexpectedException();
553          }
554      }  
555  
556  
557 <    public void testPeek(){
557 >    /**
558 >     *
559 >     */
560 >    public void testPeek() {
561          DelayQueue q = populatedQueue(SIZE);
562          for (int i = 0; i < SIZE; ++i) {
563              assertEquals(new PDelay(i), ((PDelay)q.peek()));
# Line 442 | Line 568 | public class DelayQueueTest extends JSR1
568          assertNull(q.peek());
569      }
570  
571 <    public void testElement(){
571 >    /**
572 >     *
573 >     */
574 >    public void testElement() {
575          DelayQueue q = populatedQueue(SIZE);
576          for (int i = 0; i < SIZE; ++i) {
577              assertEquals(new PDelay(i), ((PDelay)q.element()));
# Line 450 | Line 579 | public class DelayQueueTest extends JSR1
579          }
580          try {
581              q.element();
582 <            fail("no such element");
582 >            shouldThrow();
583          }
584          catch (NoSuchElementException success) {}
585      }
586  
587 <    public void testRemove(){
587 >    /**
588 >     *
589 >     */
590 >    public void testRemove() {
591          DelayQueue q = populatedQueue(SIZE);
592          for (int i = 0; i < SIZE; ++i) {
593              assertEquals(new PDelay(i), ((PDelay)q.remove()));
594          }
595          try {
596              q.remove();
597 <            fail("remove should throw");
597 >            shouldThrow();
598          } catch (NoSuchElementException success){
599          }  
600      }
601  
602 <    public void testRemoveElement(){
602 >    /**
603 >     *
604 >     */
605 >    public void testRemoveElement() {
606          DelayQueue q = populatedQueue(SIZE);
607          for (int i = 1; i < SIZE; i+=2) {
608              assertTrue(q.remove(new PDelay(i)));
# Line 479 | Line 614 | public class DelayQueueTest extends JSR1
614          assertTrue(q.isEmpty());
615      }
616          
617 <    public void testContains(){
617 >    /**
618 >     *
619 >     */
620 >    public void testContains() {
621          DelayQueue q = populatedQueue(SIZE);
622          for (int i = 0; i < SIZE; ++i) {
623              assertTrue(q.contains(new PDelay(i)));
# Line 488 | Line 626 | public class DelayQueueTest extends JSR1
626          }
627      }
628  
629 <    public void testClear(){
629 >    /**
630 >     *
631 >     */
632 >    public void testClear() {
633          DelayQueue q = populatedQueue(SIZE);
634          q.clear();
635          assertTrue(q.isEmpty());
# Line 500 | Line 641 | public class DelayQueueTest extends JSR1
641          assertTrue(q.isEmpty());
642      }
643  
644 <    public void testContainsAll(){
644 >    /**
645 >     *
646 >     */
647 >    public void testContainsAll() {
648          DelayQueue q = populatedQueue(SIZE);
649          DelayQueue p = new DelayQueue();
650          for (int i = 0; i < SIZE; ++i) {
# Line 511 | Line 655 | public class DelayQueueTest extends JSR1
655          assertTrue(p.containsAll(q));
656      }
657  
658 <    public void testRetainAll(){
658 >    /**
659 >     *
660 >     */
661 >    public void testRetainAll() {
662          DelayQueue q = populatedQueue(SIZE);
663          DelayQueue p = populatedQueue(SIZE);
664          for (int i = 0; i < SIZE; ++i) {
# Line 527 | Line 674 | public class DelayQueueTest extends JSR1
674          }
675      }
676  
677 <    public void testRemoveAll(){
677 >    /**
678 >     *
679 >     */
680 >    public void testRemoveAll() {
681          for (int i = 1; i < SIZE; ++i) {
682              DelayQueue q = populatedQueue(SIZE);
683              DelayQueue p = populatedQueue(i);
# Line 540 | Line 690 | public class DelayQueueTest extends JSR1
690          }
691      }
692  
693 <    public void testToArray(){
693 >    /**
694 >     *
695 >     */
696 >    public void testToArray() {
697          DelayQueue q = populatedQueue(SIZE);
698          Object[] o = q.toArray();
699          Arrays.sort(o);
# Line 548 | Line 701 | public class DelayQueueTest extends JSR1
701          for(int i = 0; i < o.length; i++)
702              assertEquals(o[i], q.take());
703          } catch (InterruptedException e){
704 <            fail("Unexpected exception");
704 >            unexpectedException();
705          }    
706      }
707  
708 <    public void testToArray2(){
708 >    /**
709 >     *
710 >     */
711 >    public void testToArray2() {
712          DelayQueue q = populatedQueue(SIZE);
713          PDelay[] ints = new PDelay[SIZE];
714          ints = (PDelay[])q.toArray(ints);
# Line 561 | Line 717 | public class DelayQueueTest extends JSR1
717              for(int i = 0; i < ints.length; i++)
718                  assertEquals(ints[i], q.take());
719          } catch (InterruptedException e){
720 <            fail("Unexpected exception");
720 >            unexpectedException();
721          }    
722      }
723      
724 <    public void testIterator(){
724 >    /**
725 >     *
726 >     */
727 >    public void testIterator() {
728          DelayQueue q = populatedQueue(SIZE);
729          int i = 0;
730          Iterator it = q.iterator();
# Line 576 | Line 735 | public class DelayQueueTest extends JSR1
735          assertEquals(i, SIZE);
736      }
737  
738 +    /**
739 +     *
740 +     */
741      public void testIteratorRemove () {
742  
743          final DelayQueue q = new DelayQueue();
# Line 595 | Line 757 | public class DelayQueueTest extends JSR1
757      }
758  
759  
760 <    public void testToString(){
760 >    /**
761 >     *
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 +     *
773 +     */
774      public void testPollInExecutor() {
775  
776          final DelayQueue q = new DelayQueue();
# Line 617 | Line 785 | public class DelayQueueTest extends JSR1
785                      threadAssertTrue(q.isEmpty());
786                  }
787                  catch (InterruptedException e) {
788 <                    threadFail("should not be interrupted");
788 >                    threadUnexpectedException();
789                  }
790              }
791          });
# Line 629 | Line 797 | public class DelayQueueTest extends JSR1
797                      q.put(new PDelay(1));
798                  }
799                  catch (InterruptedException e) {
800 <                    threadFail("should not be interrupted");
800 >                    threadUnexpectedException();
801                  }
802              }
803          });
# Line 638 | Line 806 | public class DelayQueueTest extends JSR1
806  
807      }
808  
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    }
809  
810 +    /**
811 +     *
812 +     */
813      public void testDelay() {
814          DelayQueue q = new DelayQueue();
815          NanoDelay[] elements = new NanoDelay[SIZE];
# Line 702 | Line 832 | public class DelayQueueTest extends JSR1
832              }
833          }
834          catch(InterruptedException ie) {
835 <            fail("Unexpected Exception");
835 >            unexpectedException();
836          }
837      }
838  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines