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.4 by dl, Sat Sep 20 18:20:07 2003 UTC vs.
Revision 1.11 by dl, Sun Oct 31 14:55:14 2004 UTC

# Line 1 | Line 1
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.
2 > * Written by Doug Lea with assistance from members of JCP JSR-166
3 > * Expert Group and released to the public domain, as explained at
4 > * http://creativecommons.org/licenses/publicdomain
5 > * Other contributors include Andrew Wright, Jeffrey Hayes,
6 > * Pat Fisher, Mike Judd.
7   */
8  
9   import junit.framework.*;
# Line 21 | Line 22 | public class DelayQueueTest extends JSR1
22      private static final int NOCAP = Integer.MAX_VALUE;
23  
24      /**
25 <     * A delayed implmentation for testing.
26 <     * Most Q/BQ tests use Pseudodelays, where delays are all elapsed
25 >     * A delayed implementation for testing.
26 >     * Most  tests use Pseudodelays, where delays are all elapsed
27       * (so, no blocking solely for delays) but are still ordered
28       */
29      static class PDelay implements Delayed {
30          int pseudodelay;
31          PDelay(int i) { pseudodelay = Integer.MIN_VALUE + i; }
32 <        public int compareTo(Object y) {
32 >        public int compareTo(PDelay y) {
33              int i = pseudodelay;
34              int j = ((PDelay)y).pseudodelay;
35              if (i < j) return -1;
# Line 36 | Line 37 | public class DelayQueueTest extends JSR1
37              return 0;
38          }
39  
40 <        public int compareTo(PDelay y) {
40 >        public int compareTo(Delayed y) {
41              int i = pseudodelay;
42              int j = ((PDelay)y).pseudodelay;
43              if (i < j) return -1;
# Line 73 | Line 74 | public class DelayQueueTest extends JSR1
74          NanoDelay(long i) {
75              trigger = System.nanoTime() + i;
76          }
77 <        public int compareTo(Object y) {
77 >        public int compareTo(NanoDelay y) {
78              long i = trigger;
79              long j = ((NanoDelay)y).trigger;
80              if (i < j) return -1;
# Line 81 | Line 82 | public class DelayQueueTest extends JSR1
82              return 0;
83          }
84  
85 <        public int compareTo(NanoDelay y) {
85 >        public int compareTo(Delayed y) {
86              long i = trigger;
87              long j = ((NanoDelay)y).trigger;
88              if (i < j) return -1;
# Line 129 | Line 130 | public class DelayQueueTest extends JSR1
130      }
131  
132      /**
133 <     *
133 >     * A new queue has unbounded capacity
134       */
135      public void testConstructor1() {
136          assertEquals(NOCAP, new DelayQueue().remainingCapacity());
137      }
138  
139      /**
140 <     *
140 >     * Initializing from null Collection throws NPE
141       */
142      public void testConstructor3() {
143          try {
# Line 147 | Line 148 | public class DelayQueueTest extends JSR1
148      }
149  
150      /**
151 <     *
151 >     * Initializing from Collection of null elements throws NPE
152       */
153      public void testConstructor4() {
154          try {
# Line 159 | Line 160 | public class DelayQueueTest extends JSR1
160      }
161  
162      /**
163 <     *
163 >     * Initializing from Collection with some null elements throws NPE
164       */
165      public void testConstructor5() {
166          try {
# Line 173 | Line 174 | public class DelayQueueTest extends JSR1
174      }
175  
176      /**
177 <     *
177 >     * Queue contains all elements of collection used to initialize
178       */
179      public void testConstructor6() {
180          try {
# Line 188 | Line 189 | public class DelayQueueTest extends JSR1
189      }
190  
191      /**
192 <     *
192 >     * isEmpty is true before add, false after
193       */
194      public void testEmpty() {
195          DelayQueue q = new DelayQueue();
# Line 203 | Line 204 | public class DelayQueueTest extends JSR1
204      }
205  
206      /**
207 <     *
207 >     * remainingCapacity does not change when elementa added or removed,
208 >     * but size does
209       */
210      public void testRemainingCapacity() {
211          DelayQueue q = populatedQueue(SIZE);
# Line 220 | Line 222 | public class DelayQueueTest extends JSR1
222      }
223  
224      /**
225 <     *
225 >     * offer(null) throws NPE
226       */
227      public void testOfferNull() {
228          try {
# Line 231 | Line 233 | public class DelayQueueTest extends JSR1
233      }
234  
235      /**
236 <     *
236 >     * add(null) throws NPE
237 >     */
238 >    public void testAddNull() {
239 >        try {
240 >            DelayQueue q = new DelayQueue();
241 >            q.add(null);
242 >            shouldThrow();
243 >        } catch (NullPointerException success) { }  
244 >    }
245 >
246 >    /**
247 >     * offer non-null succeeds
248       */
249      public void testOffer() {
250          DelayQueue q = new DelayQueue();
# Line 240 | Line 253 | public class DelayQueueTest extends JSR1
253      }
254  
255      /**
256 <     *
256 >     * add succeeds
257       */
258      public void testAdd() {
259          DelayQueue q = new DelayQueue();
# Line 251 | Line 264 | public class DelayQueueTest extends JSR1
264      }
265  
266      /**
267 <     *
267 >     * addAll(null) throws NPE
268       */
269      public void testAddAll1() {
270          try {
# Line 261 | Line 274 | public class DelayQueueTest extends JSR1
274          }
275          catch (NullPointerException success) {}
276      }
277 +
278 +
279      /**
280 <     *
280 >     * addAll(this) throws IAE
281 >     */
282 >    public void testAddAllSelf() {
283 >        try {
284 >            DelayQueue q = populatedQueue(SIZE);
285 >            q.addAll(q);
286 >            shouldThrow();
287 >        }
288 >        catch (IllegalArgumentException success) {}
289 >    }
290 >
291 >    /**
292 >     * addAll of a collection with null elements throws NPE
293       */
294      public void testAddAll2() {
295          try {
# Line 274 | Line 301 | public class DelayQueueTest extends JSR1
301          catch (NullPointerException success) {}
302      }
303      /**
304 <     *
304 >     * addAll of a collection with any null elements throws NPE after
305 >     * possibly adding some elements
306       */
307      public void testAddAll3() {
308          try {
# Line 289 | Line 317 | public class DelayQueueTest extends JSR1
317      }
318  
319      /**
320 <     *
320 >     * Queue contains all elements of successful addAll
321       */
322      public void testAddAll5() {
323          try {
# Line 307 | Line 335 | public class DelayQueueTest extends JSR1
335      }
336  
337      /**
338 <     *
338 >     * put(null) throws NPE
339       */
340       public void testPutNull() {
341          try {
# Line 320 | Line 348 | public class DelayQueueTest extends JSR1
348       }
349  
350      /**
351 <     *
351 >     * all elements successfully put are contained
352       */
353       public void testPut() {
354           try {
# Line 337 | Line 365 | public class DelayQueueTest extends JSR1
365      }
366  
367      /**
368 <     *
368 >     * put doesn't block waiting for take
369       */
370      public void testPutWithTake() {
371          final DelayQueue q = new DelayQueue();
# Line 370 | Line 398 | public class DelayQueueTest extends JSR1
398      }
399  
400      /**
401 <     *
401 >     * timed offer does not time out
402       */
403      public void testTimedOffer() {
404          final DelayQueue q = new DelayQueue();
# Line 396 | Line 424 | public class DelayQueueTest extends JSR1
424      }
425  
426      /**
427 <     *
427 >     * take retrieves elements in priority order
428       */
429      public void testTake() {
430          try {
# Line 410 | Line 438 | public class DelayQueueTest extends JSR1
438      }
439  
440      /**
441 <     *
441 >     * take blocks interruptibly when empty
442       */
443      public void testTakeFromEmpty() {
444          final DelayQueue q = new DelayQueue();
# Line 433 | Line 461 | public class DelayQueueTest extends JSR1
461      }
462  
463      /**
464 <     *
464 >     * Take removes existing elements until empty, then blocks interruptibly
465       */
466      public void testBlockingTake() {
467          Thread t = new Thread(new Runnable() {
# Line 461 | Line 489 | public class DelayQueueTest extends JSR1
489  
490  
491      /**
492 <     *
492 >     * poll succeeds unless empty
493       */
494      public void testPoll() {
495          DelayQueue q = populatedQueue(SIZE);
# Line 472 | Line 500 | public class DelayQueueTest extends JSR1
500      }
501  
502      /**
503 <     *
503 >     * timed pool with zero timeout succeeds when non-empty, else times out
504       */
505      public void testTimedPoll0() {
506          try {
# Line 487 | Line 515 | public class DelayQueueTest extends JSR1
515      }
516  
517      /**
518 <     *
518 >     * timed pool with nonzero timeout succeeds when non-empty, else times out
519       */
520      public void testTimedPoll() {
521          try {
# Line 502 | Line 530 | public class DelayQueueTest extends JSR1
530      }
531  
532      /**
533 <     *
533 >     * Interrupted timed poll throws InterruptedException instead of
534 >     * returning timeout status
535       */
536      public void testInterruptedTimedPoll() {
537          Thread t = new Thread(new Runnable() {
# Line 528 | Line 557 | public class DelayQueueTest extends JSR1
557      }
558  
559      /**
560 <     *
560 >     *  timed poll before a delayed offer fails; after offer succeeds;
561 >     *  on interruption throws
562       */
563      public void testTimedPollWithOffer() {
564          final DelayQueue q = new DelayQueue();
# Line 555 | Line 585 | public class DelayQueueTest extends JSR1
585  
586  
587      /**
588 <     *
588 >     * peek returns next element, or null if empty
589       */
590      public void testPeek() {
591          DelayQueue q = populatedQueue(SIZE);
# Line 569 | Line 599 | public class DelayQueueTest extends JSR1
599      }
600  
601      /**
602 <     *
602 >     * element returns next element, or throws NSEE if empty
603       */
604      public void testElement() {
605          DelayQueue q = populatedQueue(SIZE);
# Line 585 | Line 615 | public class DelayQueueTest extends JSR1
615      }
616  
617      /**
618 <     *
618 >     * remove removes next element, or throws NSEE if empty
619       */
620      public void testRemove() {
621          DelayQueue q = populatedQueue(SIZE);
# Line 600 | Line 630 | public class DelayQueueTest extends JSR1
630      }
631  
632      /**
633 <     *
633 >     * remove(x) removes x and returns true if present
634       */
635      public void testRemoveElement() {
636          DelayQueue q = populatedQueue(SIZE);
# Line 615 | Line 645 | public class DelayQueueTest extends JSR1
645      }
646          
647      /**
648 <     *
648 >     * contains(x) reports true when elements added but not yet removed
649       */
650      public void testContains() {
651          DelayQueue q = populatedQueue(SIZE);
# Line 627 | Line 657 | public class DelayQueueTest extends JSR1
657      }
658  
659      /**
660 <     *
660 >     * clear removes all elements
661       */
662      public void testClear() {
663          DelayQueue q = populatedQueue(SIZE);
# Line 635 | Line 665 | public class DelayQueueTest extends JSR1
665          assertTrue(q.isEmpty());
666          assertEquals(0, q.size());
667          assertEquals(NOCAP, q.remainingCapacity());
668 <        q.add(new PDelay(1));
668 >        PDelay x = new PDelay(1);
669 >        q.add(x);
670          assertFalse(q.isEmpty());
671 +        assertTrue(q.contains(x));
672          q.clear();
673          assertTrue(q.isEmpty());
674      }
675  
676      /**
677 <     *
677 >     * containsAll(c) is true when c contains a subset of elements
678       */
679      public void testContainsAll() {
680          DelayQueue q = populatedQueue(SIZE);
# Line 656 | Line 688 | public class DelayQueueTest extends JSR1
688      }
689  
690      /**
691 <     *
691 >     * retainAll(c) retains only those elements of c and reports true if changed
692       */
693      public void testRetainAll() {
694          DelayQueue q = populatedQueue(SIZE);
# Line 675 | Line 707 | public class DelayQueueTest extends JSR1
707      }
708  
709      /**
710 <     *
710 >     * removeAll(c) removes only those elements of c and reports true if changed
711       */
712      public void testRemoveAll() {
713          for (int i = 1; i < SIZE; ++i) {
# Line 691 | Line 723 | public class DelayQueueTest extends JSR1
723      }
724  
725      /**
726 <     *
726 >     * toArray contains all elements
727       */
728      public void testToArray() {
729          DelayQueue q = populatedQueue(SIZE);
# Line 706 | Line 738 | public class DelayQueueTest extends JSR1
738      }
739  
740      /**
741 <     *
741 >     * toArray(a) contains all elements
742       */
743      public void testToArray2() {
744          DelayQueue q = populatedQueue(SIZE);
# Line 720 | Line 752 | public class DelayQueueTest extends JSR1
752              unexpectedException();
753          }    
754      }
755 +
756 +
757 +    /**
758 +     * toArray(null) throws NPE
759 +     */
760 +    public void testToArray_BadArg() {
761 +        try {
762 +            DelayQueue q = populatedQueue(SIZE);
763 +            Object o[] = q.toArray(null);
764 +            shouldThrow();
765 +        } catch(NullPointerException success){}
766 +    }
767 +
768 +    /**
769 +     * toArray with incompatible array type throws CCE
770 +     */
771 +    public void testToArray1_BadArg() {
772 +        try {
773 +            DelayQueue q = populatedQueue(SIZE);
774 +            Object o[] = q.toArray(new String[10] );
775 +            shouldThrow();
776 +        } catch(ArrayStoreException  success){}
777 +    }
778      
779      /**
780 <     *
780 >     * iterator iterates through all elements
781       */
782      public void testIterator() {
783          DelayQueue q = populatedQueue(SIZE);
# Line 736 | Line 791 | public class DelayQueueTest extends JSR1
791      }
792  
793      /**
794 <     *
794 >     * iterator.remove removes current element
795       */
796      public void testIteratorRemove () {
742
797          final DelayQueue q = new DelayQueue();
744
798          q.add(new PDelay(2));
799          q.add(new PDelay(1));
800          q.add(new PDelay(3));
748
801          Iterator it = q.iterator();
802          it.next();
803          it.remove();
752
804          it = q.iterator();
805          assertEquals(it.next(), new PDelay(2));
806          assertEquals(it.next(), new PDelay(3));
# Line 758 | Line 809 | public class DelayQueueTest extends JSR1
809  
810  
811      /**
812 <     *
812 >     * toString contains toStrings of elements
813       */
814      public void testToString() {
815          DelayQueue q = populatedQueue(SIZE);
# Line 769 | Line 820 | public class DelayQueueTest extends JSR1
820      }        
821  
822      /**
823 <     *
823 >     * offer transfers elements across Executor tasks
824       */
825      public void testPollInExecutor() {
775
826          final DelayQueue q = new DelayQueue();
777
827          ExecutorService executor = Executors.newFixedThreadPool(2);
779
828          executor.execute(new Runnable() {
829              public void run() {
830                  threadAssertNull(q.poll());
# Line 801 | Line 849 | public class DelayQueueTest extends JSR1
849                  }
850              }
851          });
804        
852          joinPool(executor);
853  
854      }
855  
856  
857      /**
858 <     *
858 >     * Delayed actions do not occur until their delay elapses
859       */
860      public void testDelay() {
861          DelayQueue q = new DelayQueue();
# Line 836 | Line 883 | public class DelayQueueTest extends JSR1
883          }
884      }
885  
886 +
887 +    /**
888 +     * drainTo(null) throws NPE
889 +     */
890 +    public void testDrainToNull() {
891 +        DelayQueue q = populatedQueue(SIZE);
892 +        try {
893 +            q.drainTo(null);
894 +            shouldThrow();
895 +        } catch(NullPointerException success) {
896 +        }
897 +    }
898 +
899 +    /**
900 +     * drainTo(this) throws IAE
901 +     */
902 +    public void testDrainToSelf() {
903 +        DelayQueue q = populatedQueue(SIZE);
904 +        try {
905 +            q.drainTo(q);
906 +            shouldThrow();
907 +        } catch(IllegalArgumentException success) {
908 +        }
909 +    }
910 +
911 +    /**
912 +     * drainTo(c) empties queue into another collection c
913 +     */
914 +    public void testDrainTo() {
915 +        DelayQueue q = new DelayQueue();
916 +        PDelay[] elems = new PDelay[SIZE];
917 +        for (int i = 0; i < SIZE; ++i) {
918 +            elems[i] = new PDelay(i);
919 +            q.add(elems[i]);
920 +        }
921 +        ArrayList l = new ArrayList();
922 +        q.drainTo(l);
923 +        assertEquals(q.size(), 0);
924 +        for (int i = 0; i < SIZE; ++i)
925 +            assertEquals(l.get(i), elems[i]);
926 +        q.add(elems[0]);
927 +        q.add(elems[1]);
928 +        assertFalse(q.isEmpty());
929 +        assertTrue(q.contains(elems[0]));
930 +        assertTrue(q.contains(elems[1]));
931 +        l.clear();
932 +        q.drainTo(l);
933 +        assertEquals(q.size(), 0);
934 +        assertEquals(l.size(), 2);
935 +        for (int i = 0; i < 2; ++i)
936 +            assertEquals(l.get(i), elems[i]);
937 +    }
938 +
939 +    /**
940 +     * drainTo empties queue
941 +     */
942 +    public void testDrainToWithActivePut() {
943 +        final DelayQueue q = populatedQueue(SIZE);
944 +        Thread t = new Thread(new Runnable() {
945 +                public void run() {
946 +                    q.put(new PDelay(SIZE+1));
947 +                }
948 +            });
949 +        try {
950 +            t.start();
951 +            ArrayList l = new ArrayList();
952 +            q.drainTo(l);
953 +            assertTrue(l.size() >= SIZE);
954 +            t.join();
955 +            assertTrue(q.size() + l.size() >= SIZE);
956 +        } catch(Exception e){
957 +            unexpectedException();
958 +        }
959 +    }
960 +
961 +    /**
962 +     * drainTo(null, n) throws NPE
963 +     */
964 +    public void testDrainToNullN() {
965 +        DelayQueue q = populatedQueue(SIZE);
966 +        try {
967 +            q.drainTo(null, 0);
968 +            shouldThrow();
969 +        } catch(NullPointerException success) {
970 +        }
971 +    }
972 +
973 +    /**
974 +     * drainTo(this, n) throws IAE
975 +     */
976 +    public void testDrainToSelfN() {
977 +        DelayQueue q = populatedQueue(SIZE);
978 +        try {
979 +            q.drainTo(q, 0);
980 +            shouldThrow();
981 +        } catch(IllegalArgumentException success) {
982 +        }
983 +    }
984 +
985 +    /**
986 +     * drainTo(c, n) empties first max {n, size} elements of queue into c
987 +     */
988 +    public void testDrainToN() {
989 +        for (int i = 0; i < SIZE + 2; ++i) {
990 +            DelayQueue q = populatedQueue(SIZE);
991 +            ArrayList l = new ArrayList();
992 +            q.drainTo(l, i);
993 +            int k = (i < SIZE)? i : SIZE;
994 +            assertEquals(q.size(), SIZE-k);
995 +            assertEquals(l.size(), k);
996 +        }
997 +    }
998 +
999 +
1000   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines