22 |
|
|
23 |
|
/** |
24 |
|
* A delayed implmentation for testing. |
25 |
< |
* Most Q/BQ tests use Pseudodelays, where delays are all elapsed |
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 { |
129 |
|
} |
130 |
|
|
131 |
|
/** |
132 |
< |
* |
132 |
> |
* A new queue has unbounded capacity |
133 |
|
*/ |
134 |
|
public void testConstructor1() { |
135 |
|
assertEquals(NOCAP, new DelayQueue().remainingCapacity()); |
136 |
|
} |
137 |
|
|
138 |
|
/** |
139 |
< |
* |
139 |
> |
* Initializing from null Collection throws NPE |
140 |
|
*/ |
141 |
|
public void testConstructor3() { |
142 |
|
try { |
147 |
|
} |
148 |
|
|
149 |
|
/** |
150 |
< |
* |
150 |
> |
* Initializing from Collection of null elements throws NPE |
151 |
|
*/ |
152 |
|
public void testConstructor4() { |
153 |
|
try { |
159 |
|
} |
160 |
|
|
161 |
|
/** |
162 |
< |
* |
162 |
> |
* Initializing from Collection with some null elements throws NPE |
163 |
|
*/ |
164 |
|
public void testConstructor5() { |
165 |
|
try { |
173 |
|
} |
174 |
|
|
175 |
|
/** |
176 |
< |
* |
176 |
> |
* Queue contains all elements of collection used to initialize |
177 |
|
*/ |
178 |
|
public void testConstructor6() { |
179 |
|
try { |
188 |
|
} |
189 |
|
|
190 |
|
/** |
191 |
< |
* |
191 |
> |
* isEmpty is true before add, false after |
192 |
|
*/ |
193 |
|
public void testEmpty() { |
194 |
|
DelayQueue q = new DelayQueue(); |
203 |
|
} |
204 |
|
|
205 |
|
/** |
206 |
< |
* |
206 |
> |
* remainingCapacity does not change when elementa added or removed, |
207 |
> |
* but size does |
208 |
|
*/ |
209 |
|
public void testRemainingCapacity() { |
210 |
|
DelayQueue q = populatedQueue(SIZE); |
221 |
|
} |
222 |
|
|
223 |
|
/** |
224 |
< |
* |
224 |
> |
* offer(null) throws NPE |
225 |
|
*/ |
226 |
|
public void testOfferNull() { |
227 |
|
try { |
232 |
|
} |
233 |
|
|
234 |
|
/** |
235 |
< |
* |
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(); |
252 |
|
} |
253 |
|
|
254 |
|
/** |
255 |
< |
* |
255 |
> |
* add succeeds |
256 |
|
*/ |
257 |
|
public void testAdd() { |
258 |
|
DelayQueue q = new DelayQueue(); |
263 |
|
} |
264 |
|
|
265 |
|
/** |
266 |
< |
* |
266 |
> |
* addAll(null) throws NPE |
267 |
|
*/ |
268 |
|
public void testAddAll1() { |
269 |
|
try { |
273 |
|
} |
274 |
|
catch (NullPointerException success) {} |
275 |
|
} |
276 |
+ |
|
277 |
+ |
|
278 |
|
/** |
279 |
< |
* |
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 { |
300 |
|
catch (NullPointerException success) {} |
301 |
|
} |
302 |
|
/** |
303 |
< |
* |
303 |
> |
* addAll of a collection with any null elements throws NPE after |
304 |
> |
* possibly adding some elements |
305 |
|
*/ |
306 |
|
public void testAddAll3() { |
307 |
|
try { |
316 |
|
} |
317 |
|
|
318 |
|
/** |
319 |
< |
* |
319 |
> |
* Queue contains all elements of successful addAll |
320 |
|
*/ |
321 |
|
public void testAddAll5() { |
322 |
|
try { |
334 |
|
} |
335 |
|
|
336 |
|
/** |
337 |
< |
* |
337 |
> |
* put(null) throws NPE |
338 |
|
*/ |
339 |
|
public void testPutNull() { |
340 |
|
try { |
347 |
|
} |
348 |
|
|
349 |
|
/** |
350 |
< |
* |
350 |
> |
* all elements successfully put are contained |
351 |
|
*/ |
352 |
|
public void testPut() { |
353 |
|
try { |
364 |
|
} |
365 |
|
|
366 |
|
/** |
367 |
< |
* |
367 |
> |
* put doesn't block waiting for take |
368 |
|
*/ |
369 |
|
public void testPutWithTake() { |
370 |
|
final DelayQueue q = new DelayQueue(); |
397 |
|
} |
398 |
|
|
399 |
|
/** |
400 |
< |
* |
400 |
> |
* timed offer does not time out |
401 |
|
*/ |
402 |
|
public void testTimedOffer() { |
403 |
|
final DelayQueue q = new DelayQueue(); |
423 |
|
} |
424 |
|
|
425 |
|
/** |
426 |
< |
* |
426 |
> |
* take retrieves elements in priority order |
427 |
|
*/ |
428 |
|
public void testTake() { |
429 |
|
try { |
437 |
|
} |
438 |
|
|
439 |
|
/** |
440 |
< |
* |
440 |
> |
* take blocks interruptibly when empty |
441 |
|
*/ |
442 |
|
public void testTakeFromEmpty() { |
443 |
|
final DelayQueue q = new DelayQueue(); |
460 |
|
} |
461 |
|
|
462 |
|
/** |
463 |
< |
* |
463 |
> |
* Take removes existing elements until empty, then blocks interruptibly |
464 |
|
*/ |
465 |
|
public void testBlockingTake() { |
466 |
|
Thread t = new Thread(new Runnable() { |
488 |
|
|
489 |
|
|
490 |
|
/** |
491 |
< |
* |
491 |
> |
* poll succeeds unless empty |
492 |
|
*/ |
493 |
|
public void testPoll() { |
494 |
|
DelayQueue q = populatedQueue(SIZE); |
499 |
|
} |
500 |
|
|
501 |
|
/** |
502 |
< |
* |
502 |
> |
* timed pool with zero timeout succeeds when non-empty, else times out |
503 |
|
*/ |
504 |
|
public void testTimedPoll0() { |
505 |
|
try { |
514 |
|
} |
515 |
|
|
516 |
|
/** |
517 |
< |
* |
517 |
> |
* timed pool with nonzero timeout succeeds when non-empty, else times out |
518 |
|
*/ |
519 |
|
public void testTimedPoll() { |
520 |
|
try { |
529 |
|
} |
530 |
|
|
531 |
|
/** |
532 |
< |
* |
532 |
> |
* Interrupted timed poll throws InterruptedException instead of |
533 |
> |
* returning timeout status |
534 |
|
*/ |
535 |
|
public void testInterruptedTimedPoll() { |
536 |
|
Thread t = new Thread(new Runnable() { |
556 |
|
} |
557 |
|
|
558 |
|
/** |
559 |
< |
* |
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(); |
584 |
|
|
585 |
|
|
586 |
|
/** |
587 |
< |
* |
587 |
> |
* peek returns next element, or null if empty |
588 |
|
*/ |
589 |
|
public void testPeek() { |
590 |
|
DelayQueue q = populatedQueue(SIZE); |
598 |
|
} |
599 |
|
|
600 |
|
/** |
601 |
< |
* |
601 |
> |
* element returns next element, or throws NSEE if empty |
602 |
|
*/ |
603 |
|
public void testElement() { |
604 |
|
DelayQueue q = populatedQueue(SIZE); |
614 |
|
} |
615 |
|
|
616 |
|
/** |
617 |
< |
* |
617 |
> |
* remove removes next element, or throws NSEE if empty |
618 |
|
*/ |
619 |
|
public void testRemove() { |
620 |
|
DelayQueue q = populatedQueue(SIZE); |
629 |
|
} |
630 |
|
|
631 |
|
/** |
632 |
< |
* |
632 |
> |
* remove(x) removes x and returns true if present |
633 |
|
*/ |
634 |
|
public void testRemoveElement() { |
635 |
|
DelayQueue q = populatedQueue(SIZE); |
644 |
|
} |
645 |
|
|
646 |
|
/** |
647 |
< |
* |
647 |
> |
* contains(x) reports true when elements added but not yet removed |
648 |
|
*/ |
649 |
|
public void testContains() { |
650 |
|
DelayQueue q = populatedQueue(SIZE); |
656 |
|
} |
657 |
|
|
658 |
|
/** |
659 |
< |
* |
659 |
> |
* clear removes all elements |
660 |
|
*/ |
661 |
|
public void testClear() { |
662 |
|
DelayQueue q = populatedQueue(SIZE); |
671 |
|
} |
672 |
|
|
673 |
|
/** |
674 |
< |
* |
674 |
> |
* containsAll(c) is true when c contains a subset of elements |
675 |
|
*/ |
676 |
|
public void testContainsAll() { |
677 |
|
DelayQueue q = populatedQueue(SIZE); |
685 |
|
} |
686 |
|
|
687 |
|
/** |
688 |
< |
* |
688 |
> |
* retainAll(c) retains only those elements of c and reports true if changed |
689 |
|
*/ |
690 |
|
public void testRetainAll() { |
691 |
|
DelayQueue q = populatedQueue(SIZE); |
704 |
|
} |
705 |
|
|
706 |
|
/** |
707 |
< |
* |
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) { |
720 |
|
} |
721 |
|
|
722 |
|
/** |
723 |
< |
* |
723 |
> |
* toArray contains all elements |
724 |
|
*/ |
725 |
|
public void testToArray() { |
726 |
|
DelayQueue q = populatedQueue(SIZE); |
735 |
|
} |
736 |
|
|
737 |
|
/** |
738 |
< |
* |
738 |
> |
* toArray(a) contains all elements |
739 |
|
*/ |
740 |
|
public void testToArray2() { |
741 |
|
DelayQueue q = populatedQueue(SIZE); |
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 |
|
/** |
777 |
< |
* |
777 |
> |
* iterator iterates through all elements |
778 |
|
*/ |
779 |
|
public void testIterator() { |
780 |
|
DelayQueue q = populatedQueue(SIZE); |
788 |
|
} |
789 |
|
|
790 |
|
/** |
791 |
< |
* |
791 |
> |
* iterator.remove removes current element |
792 |
|
*/ |
793 |
|
public void testIteratorRemove () { |
742 |
– |
|
794 |
|
final DelayQueue q = new DelayQueue(); |
744 |
– |
|
795 |
|
q.add(new PDelay(2)); |
796 |
|
q.add(new PDelay(1)); |
797 |
|
q.add(new PDelay(3)); |
748 |
– |
|
798 |
|
Iterator it = q.iterator(); |
799 |
|
it.next(); |
800 |
|
it.remove(); |
752 |
– |
|
801 |
|
it = q.iterator(); |
802 |
|
assertEquals(it.next(), new PDelay(2)); |
803 |
|
assertEquals(it.next(), new PDelay(3)); |
806 |
|
|
807 |
|
|
808 |
|
/** |
809 |
< |
* |
809 |
> |
* toString contains toStrings of elements |
810 |
|
*/ |
811 |
|
public void testToString() { |
812 |
|
DelayQueue q = populatedQueue(SIZE); |
817 |
|
} |
818 |
|
|
819 |
|
/** |
820 |
< |
* |
820 |
> |
* offer transfers elements across Executor tasks |
821 |
|
*/ |
822 |
|
public void testPollInExecutor() { |
775 |
– |
|
823 |
|
final DelayQueue q = new DelayQueue(); |
777 |
– |
|
824 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
779 |
– |
|
825 |
|
executor.execute(new Runnable() { |
826 |
|
public void run() { |
827 |
|
threadAssertNull(q.poll()); |
846 |
|
} |
847 |
|
} |
848 |
|
}); |
804 |
– |
|
849 |
|
joinPool(executor); |
850 |
|
|
851 |
|
} |
852 |
|
|
853 |
|
|
854 |
|
/** |
855 |
< |
* |
855 |
> |
* Dekayed actions do not occur until their delay elapses |
856 |
|
*/ |
857 |
|
public void testDelay() { |
858 |
|
DelayQueue q = new DelayQueue(); |
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 |
|
} |