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.6 by dl, Sun Oct 5 23:00:40 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 +     * add(null) throws NPE
236 +     */
237 +    public void testAddNull() {
238 +        try {
239 +            DelayQueue q = new DelayQueue();
240 +            q.add(null);
241 +            shouldThrow();
242 +        } catch (NullPointerException success) { }  
243 +    }
244 +
245 +    /**
246 +     * offer non-null succeeds
247 +     */
248      public void testOffer() {
249          DelayQueue q = new DelayQueue();
250          assertTrue(q.offer(new PDelay(0)));
251          assertTrue(q.offer(new PDelay(1)));
252      }
253  
254 <    public void testAdd(){
254 >    /**
255 >     * add succeeds
256 >     */
257 >    public void testAdd() {
258          DelayQueue q = new DelayQueue();
259          for (int i = 0; i < SIZE; ++i) {
260              assertEquals(i, q.size());
# Line 175 | Line 262 | public class DelayQueueTest extends JSR1
262          }
263      }
264  
265 <    public void testAddAll1(){
265 >    /**
266 >     * addAll(null) throws NPE
267 >     */
268 >    public void testAddAll1() {
269          try {
270              DelayQueue q = new DelayQueue();
271              q.addAll(null);
272 <            fail("Cannot add null collection");
272 >            shouldThrow();
273          }
274          catch (NullPointerException success) {}
275      }
276 <    public void testAddAll2(){
276 >
277 >
278 >    /**
279 >     * addAll(this) throws IAE
280 >     */
281 >    public void testAddAllSelf() {
282 >        try {
283 >            DelayQueue q = populatedQueue(SIZE);
284 >            q.addAll(q);
285 >            shouldThrow();
286 >        }
287 >        catch (IllegalArgumentException success) {}
288 >    }
289 >
290 >    /**
291 >     * addAll of a collection with null elements throws NPE
292 >     */
293 >    public void testAddAll2() {
294          try {
295              DelayQueue q = new DelayQueue();
296              PDelay[] ints = new PDelay[SIZE];
297              q.addAll(Arrays.asList(ints));
298 <            fail("Cannot add null elements");
298 >            shouldThrow();
299          }
300          catch (NullPointerException success) {}
301      }
302 <    public void testAddAll3(){
302 >    /**
303 >     * addAll of a collection with any null elements throws NPE after
304 >     * possibly adding some elements
305 >     */
306 >    public void testAddAll3() {
307          try {
308              DelayQueue q = new DelayQueue();
309              PDelay[] ints = new PDelay[SIZE];
310              for (int i = 0; i < SIZE-1; ++i)
311                  ints[i] = new PDelay(i);
312              q.addAll(Arrays.asList(ints));
313 <            fail("Cannot add null elements");
313 >            shouldThrow();
314          }
315          catch (NullPointerException success) {}
316      }
317  
318 <    public void testAddAll5(){
318 >    /**
319 >     * Queue contains all elements of successful addAll
320 >     */
321 >    public void testAddAll5() {
322          try {
323              PDelay[] empty = new PDelay[0];
324              PDelay[] ints = new PDelay[SIZE];
# Line 219 | Line 333 | public class DelayQueueTest extends JSR1
333          finally {}
334      }
335  
336 +    /**
337 +     * put(null) throws NPE
338 +     */
339       public void testPutNull() {
340          try {
341              DelayQueue q = new DelayQueue();
342              q.put(null);
343 <            fail("put should throw NPE");
343 >            shouldThrow();
344          }
345          catch (NullPointerException success){
346          }  
347       }
348  
349 +    /**
350 +     * all elements successfully put are contained
351 +     */
352       public void testPut() {
353           try {
354               DelayQueue q = new DelayQueue();
# Line 243 | Line 363 | public class DelayQueueTest extends JSR1
363          }
364      }
365  
366 +    /**
367 +     * put doesn't block waiting for take
368 +     */
369      public void testPutWithTake() {
370          final DelayQueue q = new DelayQueue();
371          Thread t = new Thread(new Runnable() {
372 <                public void run(){
372 >                public void run() {
373                      int added = 0;
374                      try {
375                          q.put(new PDelay(0));
# Line 269 | Line 392 | public class DelayQueueTest extends JSR1
392              t.interrupt();
393              t.join();
394          } catch (Exception e){
395 <            fail("Unexpected exception");
395 >            unexpectedException();
396          }
397      }
398  
399 +    /**
400 +     * timed offer does not time out
401 +     */
402      public void testTimedOffer() {
403          final DelayQueue q = new DelayQueue();
404          Thread t = new Thread(new Runnable() {
405 <                public void run(){
405 >                public void run() {
406                      try {
407                          q.put(new PDelay(0));
408                          q.put(new PDelay(0));
# Line 292 | Line 418 | public class DelayQueueTest extends JSR1
418              t.interrupt();
419              t.join();
420          } catch (Exception e){
421 <            fail("Unexpected exception");
421 >            unexpectedException();
422          }
423      }
424  
425 <    public void testTake(){
425 >    /**
426 >     * take retrieves elements in priority order
427 >     */
428 >    public void testTake() {
429          try {
430              DelayQueue q = populatedQueue(SIZE);
431              for (int i = 0; i < SIZE; ++i) {
432                  assertEquals(new PDelay(i), ((PDelay)q.take()));
433              }
434          } catch (InterruptedException e){
435 <            fail("Unexpected exception");
435 >            unexpectedException();
436          }  
437      }
438  
439 +    /**
440 +     * take blocks interruptibly when empty
441 +     */
442      public void testTakeFromEmpty() {
443          final DelayQueue q = new DelayQueue();
444          Thread t = new Thread(new Runnable() {
445 <                public void run(){
445 >                public void run() {
446                      try {
447                          q.take();
448 <                        threadFail("Should block");
448 >                        threadShouldThrow();
449                      } catch (InterruptedException success){ }                
450                  }
451              });
# Line 323 | Line 455 | public class DelayQueueTest extends JSR1
455              t.interrupt();
456              t.join();
457          } catch (Exception e){
458 <            fail("Unexpected exception");
458 >            unexpectedException();
459          }
460      }
461  
462 <    public void testBlockingTake(){
462 >    /**
463 >     * Take removes existing elements until empty, then blocks interruptibly
464 >     */
465 >    public void testBlockingTake() {
466          Thread t = new Thread(new Runnable() {
467                  public void run() {
468                      try {
# Line 336 | Line 471 | public class DelayQueueTest extends JSR1
471                              threadAssertEquals(new PDelay(i), ((PDelay)q.take()));
472                          }
473                          q.take();
474 <                        threadFail("take should block");
474 >                        threadShouldThrow();
475                      } catch (InterruptedException success){
476                      }  
477                  }});
# Line 347 | Line 482 | public class DelayQueueTest extends JSR1
482             t.join();
483          }
484          catch (InterruptedException ie) {
485 <            fail("Unexpected exception");
485 >            unexpectedException();
486          }
487      }
488  
489  
490 <    public void testPoll(){
490 >    /**
491 >     * poll succeeds unless empty
492 >     */
493 >    public void testPoll() {
494          DelayQueue q = populatedQueue(SIZE);
495          for (int i = 0; i < SIZE; ++i) {
496              assertEquals(new PDelay(i), ((PDelay)q.poll()));
# Line 360 | Line 498 | public class DelayQueueTest extends JSR1
498          assertNull(q.poll());
499      }
500  
501 +    /**
502 +     * timed pool with zero timeout succeeds when non-empty, else times out
503 +     */
504      public void testTimedPoll0() {
505          try {
506              DelayQueue q = populatedQueue(SIZE);
# Line 368 | Line 509 | public class DelayQueueTest extends JSR1
509              }
510              assertNull(q.poll(0, TimeUnit.MILLISECONDS));
511          } catch (InterruptedException e){
512 <            fail("Unexpected exception");
512 >            unexpectedException();
513          }  
514      }
515  
516 +    /**
517 +     * timed pool with nonzero timeout succeeds when non-empty, else times out
518 +     */
519      public void testTimedPoll() {
520          try {
521              DelayQueue q = populatedQueue(SIZE);
# Line 380 | Line 524 | public class DelayQueueTest extends JSR1
524              }
525              assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
526          } catch (InterruptedException e){
527 <            fail("Unexpected exception");
527 >            unexpectedException();
528          }  
529      }
530  
531 <    public void testInterruptedTimedPoll(){
531 >    /**
532 >     * Interrupted timed poll throws InterruptedException instead of
533 >     * returning timeout status
534 >     */
535 >    public void testInterruptedTimedPoll() {
536          Thread t = new Thread(new Runnable() {
537                  public void run() {
538                      try {
# Line 403 | Line 551 | public class DelayQueueTest extends JSR1
551             t.join();
552          }
553          catch (InterruptedException ie) {
554 <            fail("Unexpected exception");
554 >            unexpectedException();
555          }
556      }
557  
558 <    public void testTimedPollWithOffer(){
558 >    /**
559 >     *  timed poll before a delayed offer fails; after offer succeeds;
560 >     *  on interruption throws
561 >     */
562 >    public void testTimedPollWithOffer() {
563          final DelayQueue q = new DelayQueue();
564          Thread t = new Thread(new Runnable() {
565 <                public void run(){
565 >                public void run() {
566                      try {
567                          threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
568                          q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
# Line 426 | Line 578 | public class DelayQueueTest extends JSR1
578              t.interrupt();
579              t.join();
580          } catch (Exception e){
581 <            fail("Unexpected exception");
581 >            unexpectedException();
582          }
583      }  
584  
585  
586 <    public void testPeek(){
586 >    /**
587 >     * peek returns next element, or null if empty
588 >     */
589 >    public void testPeek() {
590          DelayQueue q = populatedQueue(SIZE);
591          for (int i = 0; i < SIZE; ++i) {
592              assertEquals(new PDelay(i), ((PDelay)q.peek()));
# Line 442 | Line 597 | public class DelayQueueTest extends JSR1
597          assertNull(q.peek());
598      }
599  
600 <    public void testElement(){
600 >    /**
601 >     * element returns next element, or throws NSEE if empty
602 >     */
603 >    public void testElement() {
604          DelayQueue q = populatedQueue(SIZE);
605          for (int i = 0; i < SIZE; ++i) {
606              assertEquals(new PDelay(i), ((PDelay)q.element()));
# Line 450 | Line 608 | public class DelayQueueTest extends JSR1
608          }
609          try {
610              q.element();
611 <            fail("no such element");
611 >            shouldThrow();
612          }
613          catch (NoSuchElementException success) {}
614      }
615  
616 <    public void testRemove(){
616 >    /**
617 >     * remove removes next element, or throws NSEE if empty
618 >     */
619 >    public void testRemove() {
620          DelayQueue q = populatedQueue(SIZE);
621          for (int i = 0; i < SIZE; ++i) {
622              assertEquals(new PDelay(i), ((PDelay)q.remove()));
623          }
624          try {
625              q.remove();
626 <            fail("remove should throw");
626 >            shouldThrow();
627          } catch (NoSuchElementException success){
628          }  
629      }
630  
631 <    public void testRemoveElement(){
631 >    /**
632 >     * remove(x) removes x and returns true if present
633 >     */
634 >    public void testRemoveElement() {
635          DelayQueue q = populatedQueue(SIZE);
636          for (int i = 1; i < SIZE; i+=2) {
637              assertTrue(q.remove(new PDelay(i)));
# Line 479 | Line 643 | public class DelayQueueTest extends JSR1
643          assertTrue(q.isEmpty());
644      }
645          
646 <    public void testContains(){
646 >    /**
647 >     * contains(x) reports true when elements added but not yet removed
648 >     */
649 >    public void testContains() {
650          DelayQueue q = populatedQueue(SIZE);
651          for (int i = 0; i < SIZE; ++i) {
652              assertTrue(q.contains(new PDelay(i)));
# Line 488 | Line 655 | public class DelayQueueTest extends JSR1
655          }
656      }
657  
658 <    public void testClear(){
658 >    /**
659 >     * clear removes all elements
660 >     */
661 >    public void testClear() {
662          DelayQueue q = populatedQueue(SIZE);
663          q.clear();
664          assertTrue(q.isEmpty());
# Line 500 | Line 670 | public class DelayQueueTest extends JSR1
670          assertTrue(q.isEmpty());
671      }
672  
673 <    public void testContainsAll(){
673 >    /**
674 >     * containsAll(c) is true when c contains a subset of elements
675 >     */
676 >    public void testContainsAll() {
677          DelayQueue q = populatedQueue(SIZE);
678          DelayQueue p = new DelayQueue();
679          for (int i = 0; i < SIZE; ++i) {
# Line 511 | Line 684 | public class DelayQueueTest extends JSR1
684          assertTrue(p.containsAll(q));
685      }
686  
687 <    public void testRetainAll(){
687 >    /**
688 >     * retainAll(c) retains only those elements of c and reports true if changed
689 >     */
690 >    public void testRetainAll() {
691          DelayQueue q = populatedQueue(SIZE);
692          DelayQueue p = populatedQueue(SIZE);
693          for (int i = 0; i < SIZE; ++i) {
# Line 527 | Line 703 | public class DelayQueueTest extends JSR1
703          }
704      }
705  
706 <    public void testRemoveAll(){
706 >    /**
707 >     * removeAll(c) removes only those elements of c and reports true if changed
708 >     */
709 >    public void testRemoveAll() {
710          for (int i = 1; i < SIZE; ++i) {
711              DelayQueue q = populatedQueue(SIZE);
712              DelayQueue p = populatedQueue(i);
# Line 540 | Line 719 | public class DelayQueueTest extends JSR1
719          }
720      }
721  
722 <    public void testToArray(){
722 >    /**
723 >     * toArray contains all elements
724 >     */
725 >    public void testToArray() {
726          DelayQueue q = populatedQueue(SIZE);
727          Object[] o = q.toArray();
728          Arrays.sort(o);
# Line 548 | Line 730 | public class DelayQueueTest extends JSR1
730          for(int i = 0; i < o.length; i++)
731              assertEquals(o[i], q.take());
732          } catch (InterruptedException e){
733 <            fail("Unexpected exception");
733 >            unexpectedException();
734          }    
735      }
736  
737 <    public void testToArray2(){
737 >    /**
738 >     * toArray(a) contains all elements
739 >     */
740 >    public void testToArray2() {
741          DelayQueue q = populatedQueue(SIZE);
742          PDelay[] ints = new PDelay[SIZE];
743          ints = (PDelay[])q.toArray(ints);
# Line 561 | Line 746 | public class DelayQueueTest extends JSR1
746              for(int i = 0; i < ints.length; i++)
747                  assertEquals(ints[i], q.take());
748          } catch (InterruptedException e){
749 <            fail("Unexpected exception");
749 >            unexpectedException();
750          }    
751      }
752 +
753 +
754 +    /**
755 +     * toArray(null) throws NPE
756 +     */
757 +    public void testToArray_BadArg() {
758 +        try {
759 +            DelayQueue q = populatedQueue(SIZE);
760 +            Object o[] = q.toArray(null);
761 +            shouldThrow();
762 +        } catch(NullPointerException success){}
763 +    }
764 +
765 +    /**
766 +     * toArray with incompatable array type throws CCE
767 +     */
768 +    public void testToArray1_BadArg() {
769 +        try {
770 +            DelayQueue q = populatedQueue(SIZE);
771 +            Object o[] = q.toArray(new String[10] );
772 +            shouldThrow();
773 +        } catch(ArrayStoreException  success){}
774 +    }
775      
776 <    public void testIterator(){
776 >    /**
777 >     * iterator iterates through all elements
778 >     */
779 >    public void testIterator() {
780          DelayQueue q = populatedQueue(SIZE);
781          int i = 0;
782          Iterator it = q.iterator();
# Line 576 | Line 787 | public class DelayQueueTest extends JSR1
787          assertEquals(i, SIZE);
788      }
789  
790 +    /**
791 +     * iterator.remove removes current element
792 +     */
793      public void testIteratorRemove () {
580
794          final DelayQueue q = new DelayQueue();
582
795          q.add(new PDelay(2));
796          q.add(new PDelay(1));
797          q.add(new PDelay(3));
586
798          Iterator it = q.iterator();
799          it.next();
800          it.remove();
590
801          it = q.iterator();
802          assertEquals(it.next(), new PDelay(2));
803          assertEquals(it.next(), new PDelay(3));
# Line 595 | Line 805 | public class DelayQueueTest extends JSR1
805      }
806  
807  
808 <    public void testToString(){
808 >    /**
809 >     * toString contains toStrings of elements
810 >     */
811 >    public void testToString() {
812          DelayQueue q = populatedQueue(SIZE);
813          String s = q.toString();
814          for (int i = 0; i < SIZE; ++i) {
# Line 603 | Line 816 | public class DelayQueueTest extends JSR1
816          }
817      }        
818  
819 +    /**
820 +     * offer transfers elements across Executor tasks
821 +     */
822      public void testPollInExecutor() {
607
823          final DelayQueue q = new DelayQueue();
609
824          ExecutorService executor = Executors.newFixedThreadPool(2);
611
825          executor.execute(new Runnable() {
826              public void run() {
827                  threadAssertNull(q.poll());
# Line 617 | Line 830 | public class DelayQueueTest extends JSR1
830                      threadAssertTrue(q.isEmpty());
831                  }
832                  catch (InterruptedException e) {
833 <                    threadFail("should not be interrupted");
833 >                    threadUnexpectedException();
834                  }
835              }
836          });
# Line 629 | Line 842 | public class DelayQueueTest extends JSR1
842                      q.put(new PDelay(1));
843                  }
844                  catch (InterruptedException e) {
845 <                    threadFail("should not be interrupted");
845 >                    threadUnexpectedException();
846                  }
847              }
848          });
636        
849          joinPool(executor);
850  
851      }
852  
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    }
853  
854 +    /**
855 +     * Dekayed actions do not occur until their delay elapses
856 +     */
857      public void testDelay() {
858          DelayQueue q = new DelayQueue();
859          NanoDelay[] elements = new NanoDelay[SIZE];
# Line 702 | Line 876 | public class DelayQueueTest extends JSR1
876              }
877          }
878          catch(InterruptedException ie) {
879 <            fail("Unexpected Exception");
879 >            unexpectedException();
880 >        }
881 >    }
882 >
883 >
884 >    /**
885 >     * drainTo(null) throws NPE
886 >     */
887 >    public void testDrainToNull() {
888 >        DelayQueue q = populatedQueue(SIZE);
889 >        try {
890 >            q.drainTo(null);
891 >            shouldThrow();
892 >        } catch(NullPointerException success) {
893 >        }
894 >    }
895 >
896 >    /**
897 >     * drainTo(this) throws IAE
898 >     */
899 >    public void testDrainToSelf() {
900 >        DelayQueue q = populatedQueue(SIZE);
901 >        try {
902 >            q.drainTo(q);
903 >            shouldThrow();
904 >        } catch(IllegalArgumentException success) {
905          }
906      }
907  
908 +    /**
909 +     * drainTo(c) empties queue into another collection c
910 +     */
911 +    public void testDrainTo() {
912 +        DelayQueue q = populatedQueue(SIZE);
913 +        ArrayList l = new ArrayList();
914 +        q.drainTo(l);
915 +        assertEquals(q.size(), 0);
916 +        assertEquals(l.size(), SIZE);
917 +    }
918 +
919 +    /**
920 +     * drainTo empties queue
921 +     */
922 +    public void testDrainToWithActivePut() {
923 +        final DelayQueue q = populatedQueue(SIZE);
924 +        Thread t = new Thread(new Runnable() {
925 +                public void run() {
926 +                    q.put(new PDelay(SIZE+1));
927 +                }
928 +            });
929 +        try {
930 +            t.start();
931 +            ArrayList l = new ArrayList();
932 +            q.drainTo(l);
933 +            assertTrue(l.size() >= SIZE);
934 +            t.join();
935 +            assertTrue(q.size() + l.size() == SIZE+1);
936 +        } catch(Exception e){
937 +            unexpectedException();
938 +        }
939 +    }
940 +
941 +    /**
942 +     * drainTo(null, n) throws NPE
943 +     */
944 +    public void testDrainToNullN() {
945 +        DelayQueue q = populatedQueue(SIZE);
946 +        try {
947 +            q.drainTo(null, 0);
948 +            shouldThrow();
949 +        } catch(NullPointerException success) {
950 +        }
951 +    }
952 +
953 +    /**
954 +     * drainTo(this, n) throws IAE
955 +     */
956 +    public void testDrainToSelfN() {
957 +        DelayQueue q = populatedQueue(SIZE);
958 +        try {
959 +            q.drainTo(q, 0);
960 +            shouldThrow();
961 +        } catch(IllegalArgumentException success) {
962 +        }
963 +    }
964 +
965 +    /**
966 +     * drainTo(c, n) empties first max {n, size} elements of queue into c
967 +     */
968 +    public void testDrainToN() {
969 +        for (int i = 0; i < SIZE + 2; ++i) {
970 +            DelayQueue q = populatedQueue(SIZE);
971 +            ArrayList l = new ArrayList();
972 +            q.drainTo(l, i);
973 +            int k = (i < SIZE)? i : SIZE;
974 +            assertEquals(q.size(), SIZE-k);
975 +            assertEquals(l.size(), k);
976 +        }
977 +    }
978 +
979 +
980   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines