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.*; |
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 { |
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 { |
148 |
|
} |
149 |
|
|
150 |
|
/** |
151 |
< |
* |
151 |
> |
* Initializing from Collection of null elements throws NPE |
152 |
|
*/ |
153 |
|
public void testConstructor4() { |
154 |
|
try { |
160 |
|
} |
161 |
|
|
162 |
|
/** |
163 |
< |
* |
163 |
> |
* Initializing from Collection with some null elements throws NPE |
164 |
|
*/ |
165 |
|
public void testConstructor5() { |
166 |
|
try { |
174 |
|
} |
175 |
|
|
176 |
|
/** |
177 |
< |
* |
177 |
> |
* Queue contains all elements of collection used to initialize |
178 |
|
*/ |
179 |
|
public void testConstructor6() { |
180 |
|
try { |
189 |
|
} |
190 |
|
|
191 |
|
/** |
192 |
< |
* |
192 |
> |
* isEmpty is true before add, false after |
193 |
|
*/ |
194 |
|
public void testEmpty() { |
195 |
|
DelayQueue q = new DelayQueue(); |
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); |
222 |
|
} |
223 |
|
|
224 |
|
/** |
225 |
< |
* |
225 |
> |
* offer(null) throws NPE |
226 |
|
*/ |
227 |
|
public void testOfferNull() { |
228 |
|
try { |
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(); |
253 |
|
} |
254 |
|
|
255 |
|
/** |
256 |
< |
* |
256 |
> |
* add succeeds |
257 |
|
*/ |
258 |
|
public void testAdd() { |
259 |
|
DelayQueue q = new DelayQueue(); |
264 |
|
} |
265 |
|
|
266 |
|
/** |
267 |
< |
* |
267 |
> |
* addAll(null) throws NPE |
268 |
|
*/ |
269 |
|
public void testAddAll1() { |
270 |
|
try { |
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 { |
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 { |
317 |
|
} |
318 |
|
|
319 |
|
/** |
320 |
< |
* |
320 |
> |
* Queue contains all elements of successful addAll |
321 |
|
*/ |
322 |
|
public void testAddAll5() { |
323 |
|
try { |
335 |
|
} |
336 |
|
|
337 |
|
/** |
338 |
< |
* |
338 |
> |
* put(null) throws NPE |
339 |
|
*/ |
340 |
|
public void testPutNull() { |
341 |
|
try { |
348 |
|
} |
349 |
|
|
350 |
|
/** |
351 |
< |
* |
351 |
> |
* all elements successfully put are contained |
352 |
|
*/ |
353 |
|
public void testPut() { |
354 |
|
try { |
365 |
|
} |
366 |
|
|
367 |
|
/** |
368 |
< |
* |
368 |
> |
* put doesn't block waiting for take |
369 |
|
*/ |
370 |
|
public void testPutWithTake() { |
371 |
|
final DelayQueue q = new DelayQueue(); |
398 |
|
} |
399 |
|
|
400 |
|
/** |
401 |
< |
* |
401 |
> |
* timed offer does not time out |
402 |
|
*/ |
403 |
|
public void testTimedOffer() { |
404 |
|
final DelayQueue q = new DelayQueue(); |
424 |
|
} |
425 |
|
|
426 |
|
/** |
427 |
< |
* |
427 |
> |
* take retrieves elements in priority order |
428 |
|
*/ |
429 |
|
public void testTake() { |
430 |
|
try { |
438 |
|
} |
439 |
|
|
440 |
|
/** |
441 |
< |
* |
441 |
> |
* take blocks interruptibly when empty |
442 |
|
*/ |
443 |
|
public void testTakeFromEmpty() { |
444 |
|
final DelayQueue q = new DelayQueue(); |
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() { |
489 |
|
|
490 |
|
|
491 |
|
/** |
492 |
< |
* |
492 |
> |
* poll succeeds unless empty |
493 |
|
*/ |
494 |
|
public void testPoll() { |
495 |
|
DelayQueue q = populatedQueue(SIZE); |
500 |
|
} |
501 |
|
|
502 |
|
/** |
503 |
< |
* |
503 |
> |
* timed pool with zero timeout succeeds when non-empty, else times out |
504 |
|
*/ |
505 |
|
public void testTimedPoll0() { |
506 |
|
try { |
515 |
|
} |
516 |
|
|
517 |
|
/** |
518 |
< |
* |
518 |
> |
* timed pool with nonzero timeout succeeds when non-empty, else times out |
519 |
|
*/ |
520 |
|
public void testTimedPoll() { |
521 |
|
try { |
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() { |
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(); |
585 |
|
|
586 |
|
|
587 |
|
/** |
588 |
< |
* |
588 |
> |
* peek returns next element, or null if empty |
589 |
|
*/ |
590 |
|
public void testPeek() { |
591 |
|
DelayQueue q = populatedQueue(SIZE); |
599 |
|
} |
600 |
|
|
601 |
|
/** |
602 |
< |
* |
602 |
> |
* element returns next element, or throws NSEE if empty |
603 |
|
*/ |
604 |
|
public void testElement() { |
605 |
|
DelayQueue q = populatedQueue(SIZE); |
615 |
|
} |
616 |
|
|
617 |
|
/** |
618 |
< |
* |
618 |
> |
* remove removes next element, or throws NSEE if empty |
619 |
|
*/ |
620 |
|
public void testRemove() { |
621 |
|
DelayQueue q = populatedQueue(SIZE); |
630 |
|
} |
631 |
|
|
632 |
|
/** |
633 |
< |
* |
633 |
> |
* remove(x) removes x and returns true if present |
634 |
|
*/ |
635 |
|
public void testRemoveElement() { |
636 |
|
DelayQueue q = populatedQueue(SIZE); |
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); |
657 |
|
} |
658 |
|
|
659 |
|
/** |
660 |
< |
* |
660 |
> |
* clear removes all elements |
661 |
|
*/ |
662 |
|
public void testClear() { |
663 |
|
DelayQueue q = populatedQueue(SIZE); |
672 |
|
} |
673 |
|
|
674 |
|
/** |
675 |
< |
* |
675 |
> |
* containsAll(c) is true when c contains a subset of elements |
676 |
|
*/ |
677 |
|
public void testContainsAll() { |
678 |
|
DelayQueue q = populatedQueue(SIZE); |
686 |
|
} |
687 |
|
|
688 |
|
/** |
689 |
< |
* |
689 |
> |
* retainAll(c) retains only those elements of c and reports true if changed |
690 |
|
*/ |
691 |
|
public void testRetainAll() { |
692 |
|
DelayQueue q = populatedQueue(SIZE); |
705 |
|
} |
706 |
|
|
707 |
|
/** |
708 |
< |
* |
708 |
> |
* removeAll(c) removes only those elements of c and reports true if changed |
709 |
|
*/ |
710 |
|
public void testRemoveAll() { |
711 |
|
for (int i = 1; i < SIZE; ++i) { |
721 |
|
} |
722 |
|
|
723 |
|
/** |
724 |
< |
* |
724 |
> |
* toArray contains all elements |
725 |
|
*/ |
726 |
|
public void testToArray() { |
727 |
|
DelayQueue q = populatedQueue(SIZE); |
736 |
|
} |
737 |
|
|
738 |
|
/** |
739 |
< |
* |
739 |
> |
* toArray(a) contains all elements |
740 |
|
*/ |
741 |
|
public void testToArray2() { |
742 |
|
DelayQueue q = populatedQueue(SIZE); |
750 |
|
unexpectedException(); |
751 |
|
} |
752 |
|
} |
753 |
+ |
|
754 |
+ |
|
755 |
+ |
/** |
756 |
+ |
* toArray(null) throws NPE |
757 |
+ |
*/ |
758 |
+ |
public void testToArray_BadArg() { |
759 |
+ |
try { |
760 |
+ |
DelayQueue q = populatedQueue(SIZE); |
761 |
+ |
Object o[] = q.toArray(null); |
762 |
+ |
shouldThrow(); |
763 |
+ |
} catch(NullPointerException success){} |
764 |
+ |
} |
765 |
+ |
|
766 |
+ |
/** |
767 |
+ |
* toArray with incompatible array type throws CCE |
768 |
+ |
*/ |
769 |
+ |
public void testToArray1_BadArg() { |
770 |
+ |
try { |
771 |
+ |
DelayQueue q = populatedQueue(SIZE); |
772 |
+ |
Object o[] = q.toArray(new String[10] ); |
773 |
+ |
shouldThrow(); |
774 |
+ |
} catch(ArrayStoreException success){} |
775 |
+ |
} |
776 |
|
|
777 |
|
/** |
778 |
< |
* |
778 |
> |
* iterator iterates through all elements |
779 |
|
*/ |
780 |
|
public void testIterator() { |
781 |
|
DelayQueue q = populatedQueue(SIZE); |
789 |
|
} |
790 |
|
|
791 |
|
/** |
792 |
< |
* |
792 |
> |
* iterator.remove removes current element |
793 |
|
*/ |
794 |
|
public void testIteratorRemove () { |
742 |
– |
|
795 |
|
final DelayQueue q = new DelayQueue(); |
744 |
– |
|
796 |
|
q.add(new PDelay(2)); |
797 |
|
q.add(new PDelay(1)); |
798 |
|
q.add(new PDelay(3)); |
748 |
– |
|
799 |
|
Iterator it = q.iterator(); |
800 |
|
it.next(); |
801 |
|
it.remove(); |
752 |
– |
|
802 |
|
it = q.iterator(); |
803 |
|
assertEquals(it.next(), new PDelay(2)); |
804 |
|
assertEquals(it.next(), new PDelay(3)); |
807 |
|
|
808 |
|
|
809 |
|
/** |
810 |
< |
* |
810 |
> |
* toString contains toStrings of elements |
811 |
|
*/ |
812 |
|
public void testToString() { |
813 |
|
DelayQueue q = populatedQueue(SIZE); |
818 |
|
} |
819 |
|
|
820 |
|
/** |
821 |
< |
* |
821 |
> |
* offer transfers elements across Executor tasks |
822 |
|
*/ |
823 |
|
public void testPollInExecutor() { |
775 |
– |
|
824 |
|
final DelayQueue q = new DelayQueue(); |
777 |
– |
|
825 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
779 |
– |
|
826 |
|
executor.execute(new Runnable() { |
827 |
|
public void run() { |
828 |
|
threadAssertNull(q.poll()); |
847 |
|
} |
848 |
|
} |
849 |
|
}); |
804 |
– |
|
850 |
|
joinPool(executor); |
851 |
|
|
852 |
|
} |
853 |
|
|
854 |
|
|
855 |
|
/** |
856 |
< |
* |
856 |
> |
* Delayed actions do not occur until their delay elapses |
857 |
|
*/ |
858 |
|
public void testDelay() { |
859 |
|
DelayQueue q = new DelayQueue(); |
881 |
|
} |
882 |
|
} |
883 |
|
|
884 |
+ |
|
885 |
+ |
/** |
886 |
+ |
* drainTo(null) throws NPE |
887 |
+ |
*/ |
888 |
+ |
public void testDrainToNull() { |
889 |
+ |
DelayQueue q = populatedQueue(SIZE); |
890 |
+ |
try { |
891 |
+ |
q.drainTo(null); |
892 |
+ |
shouldThrow(); |
893 |
+ |
} catch(NullPointerException success) { |
894 |
+ |
} |
895 |
+ |
} |
896 |
+ |
|
897 |
+ |
/** |
898 |
+ |
* drainTo(this) throws IAE |
899 |
+ |
*/ |
900 |
+ |
public void testDrainToSelf() { |
901 |
+ |
DelayQueue q = populatedQueue(SIZE); |
902 |
+ |
try { |
903 |
+ |
q.drainTo(q); |
904 |
+ |
shouldThrow(); |
905 |
+ |
} catch(IllegalArgumentException success) { |
906 |
+ |
} |
907 |
+ |
} |
908 |
+ |
|
909 |
+ |
/** |
910 |
+ |
* drainTo(c) empties queue into another collection c |
911 |
+ |
*/ |
912 |
+ |
public void testDrainTo() { |
913 |
+ |
DelayQueue q = populatedQueue(SIZE); |
914 |
+ |
ArrayList l = new ArrayList(); |
915 |
+ |
q.drainTo(l); |
916 |
+ |
assertEquals(q.size(), 0); |
917 |
+ |
assertEquals(l.size(), SIZE); |
918 |
+ |
} |
919 |
+ |
|
920 |
+ |
/** |
921 |
+ |
* drainTo empties queue |
922 |
+ |
*/ |
923 |
+ |
public void testDrainToWithActivePut() { |
924 |
+ |
final DelayQueue q = populatedQueue(SIZE); |
925 |
+ |
Thread t = new Thread(new Runnable() { |
926 |
+ |
public void run() { |
927 |
+ |
q.put(new PDelay(SIZE+1)); |
928 |
+ |
} |
929 |
+ |
}); |
930 |
+ |
try { |
931 |
+ |
t.start(); |
932 |
+ |
ArrayList l = new ArrayList(); |
933 |
+ |
q.drainTo(l); |
934 |
+ |
assertTrue(l.size() >= SIZE); |
935 |
+ |
t.join(); |
936 |
+ |
assertTrue(q.size() + l.size() == SIZE+1); |
937 |
+ |
} catch(Exception e){ |
938 |
+ |
unexpectedException(); |
939 |
+ |
} |
940 |
+ |
} |
941 |
+ |
|
942 |
+ |
/** |
943 |
+ |
* drainTo(null, n) throws NPE |
944 |
+ |
*/ |
945 |
+ |
public void testDrainToNullN() { |
946 |
+ |
DelayQueue q = populatedQueue(SIZE); |
947 |
+ |
try { |
948 |
+ |
q.drainTo(null, 0); |
949 |
+ |
shouldThrow(); |
950 |
+ |
} catch(NullPointerException success) { |
951 |
+ |
} |
952 |
+ |
} |
953 |
+ |
|
954 |
+ |
/** |
955 |
+ |
* drainTo(this, n) throws IAE |
956 |
+ |
*/ |
957 |
+ |
public void testDrainToSelfN() { |
958 |
+ |
DelayQueue q = populatedQueue(SIZE); |
959 |
+ |
try { |
960 |
+ |
q.drainTo(q, 0); |
961 |
+ |
shouldThrow(); |
962 |
+ |
} catch(IllegalArgumentException success) { |
963 |
+ |
} |
964 |
+ |
} |
965 |
+ |
|
966 |
+ |
/** |
967 |
+ |
* drainTo(c, n) empties first max {n, size} elements of queue into c |
968 |
+ |
*/ |
969 |
+ |
public void testDrainToN() { |
970 |
+ |
for (int i = 0; i < SIZE + 2; ++i) { |
971 |
+ |
DelayQueue q = populatedQueue(SIZE); |
972 |
+ |
ArrayList l = new ArrayList(); |
973 |
+ |
q.drainTo(l, i); |
974 |
+ |
int k = (i < SIZE)? i : SIZE; |
975 |
+ |
assertEquals(q.size(), SIZE-k); |
976 |
+ |
assertEquals(l.size(), k); |
977 |
+ |
} |
978 |
+ |
} |
979 |
+ |
|
980 |
+ |
|
981 |
|
} |