37 |
|
} |
38 |
|
|
39 |
|
/** |
40 |
< |
* |
40 |
> |
* A new queue has the indicated capacity, or Integer.MAX_VALUE if |
41 |
> |
* none given |
42 |
|
*/ |
43 |
|
public void testConstructor1() { |
44 |
|
assertEquals(SIZE, new LinkedBlockingQueue(SIZE).remainingCapacity()); |
45 |
+ |
assertEquals(Integer.MAX_VALUE, new LinkedBlockingQueue().remainingCapacity()); |
46 |
|
} |
47 |
|
|
48 |
|
/** |
49 |
< |
* |
49 |
> |
* Constructor throws IAE if capacity argument nonpositive |
50 |
|
*/ |
51 |
|
public void testConstructor2() { |
52 |
|
try { |
57 |
|
} |
58 |
|
|
59 |
|
/** |
60 |
< |
* |
60 |
> |
* Initializing from null Collection throws NPE |
61 |
|
*/ |
62 |
|
public void testConstructor3() { |
61 |
– |
|
63 |
|
try { |
64 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(null); |
65 |
|
shouldThrow(); |
68 |
|
} |
69 |
|
|
70 |
|
/** |
71 |
< |
* |
71 |
> |
* Initializing from Collection of null elements throws NPE |
72 |
|
*/ |
73 |
|
public void testConstructor4() { |
74 |
|
try { |
80 |
|
} |
81 |
|
|
82 |
|
/** |
83 |
< |
* |
83 |
> |
* Initializing from Collection with some null elements throws NPE |
84 |
|
*/ |
85 |
|
public void testConstructor5() { |
86 |
|
try { |
94 |
|
} |
95 |
|
|
96 |
|
/** |
97 |
< |
* |
97 |
> |
* Queue contains all elements of collection used to initialize |
98 |
|
*/ |
99 |
|
public void testConstructor6() { |
100 |
|
try { |
109 |
|
} |
110 |
|
|
111 |
|
/** |
112 |
< |
* |
112 |
> |
* Queue transitions from empty to full when elements added |
113 |
|
*/ |
114 |
|
public void testEmptyFull() { |
115 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
124 |
|
} |
125 |
|
|
126 |
|
/** |
127 |
< |
* |
127 |
> |
* remainingCapacity decreases on add, increases on remove |
128 |
|
*/ |
129 |
|
public void testRemainingCapacity() { |
130 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
141 |
|
} |
142 |
|
|
143 |
|
/** |
144 |
< |
* |
144 |
> |
* offer(null) throws NPE |
145 |
|
*/ |
146 |
|
public void testOfferNull() { |
147 |
|
try { |
152 |
|
} |
153 |
|
|
154 |
|
/** |
155 |
< |
* |
155 |
> |
* add(null) throws NPE |
156 |
> |
*/ |
157 |
> |
public void testAddNull() { |
158 |
> |
try { |
159 |
> |
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
160 |
> |
q.add(null); |
161 |
> |
shouldThrow(); |
162 |
> |
} catch (NullPointerException success) { } |
163 |
> |
} |
164 |
> |
|
165 |
> |
/** |
166 |
> |
* Offer succeeds if not full; fails if full |
167 |
|
*/ |
168 |
|
public void testOffer() { |
169 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
172 |
|
} |
173 |
|
|
174 |
|
/** |
175 |
< |
* |
175 |
> |
* add succeeds if not full; throws ISE if full |
176 |
|
*/ |
177 |
|
public void testAdd() { |
178 |
|
try { |
187 |
|
} |
188 |
|
|
189 |
|
/** |
190 |
< |
* |
190 |
> |
* addAll(null) throws NPE |
191 |
|
*/ |
192 |
|
public void testAddAll1() { |
193 |
|
try { |
197 |
|
} |
198 |
|
catch (NullPointerException success) {} |
199 |
|
} |
200 |
+ |
|
201 |
|
/** |
202 |
< |
* |
202 |
> |
* addAll(this) throws IAE |
203 |
> |
*/ |
204 |
> |
public void testAddAllSelf() { |
205 |
> |
try { |
206 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
207 |
> |
q.addAll(q); |
208 |
> |
shouldThrow(); |
209 |
> |
} |
210 |
> |
catch (IllegalArgumentException success) {} |
211 |
> |
} |
212 |
> |
|
213 |
> |
/** |
214 |
> |
* addAll of a collection with null elements throws NPE |
215 |
|
*/ |
216 |
|
public void testAddAll2() { |
217 |
|
try { |
223 |
|
catch (NullPointerException success) {} |
224 |
|
} |
225 |
|
/** |
226 |
< |
* |
226 |
> |
* addAll of a collection with any null elements throws NPE after |
227 |
> |
* possibly adding some elements |
228 |
|
*/ |
229 |
|
public void testAddAll3() { |
230 |
|
try { |
238 |
|
catch (NullPointerException success) {} |
239 |
|
} |
240 |
|
/** |
241 |
< |
* |
241 |
> |
* addAll throws ISE if not enough room |
242 |
|
*/ |
243 |
|
public void testAddAll4() { |
244 |
|
try { |
252 |
|
catch (IllegalStateException success) {} |
253 |
|
} |
254 |
|
/** |
255 |
< |
* |
255 |
> |
* Queue contains all elements, in traversal order, of successful addAll |
256 |
|
*/ |
257 |
|
public void testAddAll5() { |
258 |
|
try { |
270 |
|
} |
271 |
|
|
272 |
|
/** |
273 |
< |
* |
273 |
> |
* put(null) throws NPE |
274 |
|
*/ |
275 |
|
public void testPutNull() { |
276 |
|
try { |
286 |
|
} |
287 |
|
|
288 |
|
/** |
289 |
< |
* |
289 |
> |
* all elements successfully put are contained |
290 |
|
*/ |
291 |
|
public void testPut() { |
292 |
|
try { |
304 |
|
} |
305 |
|
|
306 |
|
/** |
307 |
< |
* |
307 |
> |
* put blocks interruptibly if full |
308 |
|
*/ |
309 |
|
public void testBlockingPut() { |
310 |
|
Thread t = new Thread(new Runnable() { |
334 |
|
} |
335 |
|
|
336 |
|
/** |
337 |
< |
* |
337 |
> |
* put blocks waiting for take when full |
338 |
|
*/ |
339 |
|
public void testPutWithTake() { |
340 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
368 |
|
} |
369 |
|
|
370 |
|
/** |
371 |
< |
* |
371 |
> |
* timed offer times out if full and elements not taken |
372 |
|
*/ |
373 |
|
public void testTimedOffer() { |
374 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
395 |
|
} |
396 |
|
|
397 |
|
/** |
398 |
< |
* |
398 |
> |
* take retrieves elements in FIFO order |
399 |
|
*/ |
400 |
|
public void testTake() { |
401 |
|
try { |
409 |
|
} |
410 |
|
|
411 |
|
/** |
412 |
< |
* |
412 |
> |
* take blocks interruptibly when empty |
413 |
|
*/ |
414 |
|
public void testTakeFromEmpty() { |
415 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
432 |
|
} |
433 |
|
|
434 |
|
/** |
435 |
< |
* |
435 |
> |
* Take removes existing elements until empty, then blocks interruptibly |
436 |
|
*/ |
437 |
|
public void testBlockingTake() { |
438 |
|
Thread t = new Thread(new Runnable() { |
460 |
|
|
461 |
|
|
462 |
|
/** |
463 |
< |
* |
463 |
> |
* poll succeeds unless empty |
464 |
|
*/ |
465 |
|
public void testPoll() { |
466 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
471 |
|
} |
472 |
|
|
473 |
|
/** |
474 |
< |
* |
474 |
> |
* timed pool with zero timeout succeeds when non-empty, else times out |
475 |
|
*/ |
476 |
|
public void testTimedPoll0() { |
477 |
|
try { |
486 |
|
} |
487 |
|
|
488 |
|
/** |
489 |
< |
* |
489 |
> |
* timed pool with nonzero timeout succeeds when non-empty, else times out |
490 |
|
*/ |
491 |
|
public void testTimedPoll() { |
492 |
|
try { |
501 |
|
} |
502 |
|
|
503 |
|
/** |
504 |
< |
* |
504 |
> |
* Interrupted timed poll throws InterruptedException instead of |
505 |
> |
* returning timeout status |
506 |
|
*/ |
507 |
|
public void testInterruptedTimedPoll() { |
508 |
|
Thread t = new Thread(new Runnable() { |
528 |
|
} |
529 |
|
|
530 |
|
/** |
531 |
< |
* |
531 |
> |
* timed poll before a delayed offer fails; after offer succeeds; |
532 |
> |
* on interruption throws |
533 |
|
*/ |
534 |
|
public void testTimedPollWithOffer() { |
535 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
554 |
|
} |
555 |
|
} |
556 |
|
|
529 |
– |
|
557 |
|
/** |
558 |
< |
* |
558 |
> |
* peek returns next element, or null if empty |
559 |
|
*/ |
560 |
|
public void testPeek() { |
561 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
569 |
|
} |
570 |
|
|
571 |
|
/** |
572 |
< |
* |
572 |
> |
* element returns next element, or throws NSEE if empty |
573 |
|
*/ |
574 |
|
public void testElement() { |
575 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
585 |
|
} |
586 |
|
|
587 |
|
/** |
588 |
< |
* |
588 |
> |
* remove removes next element, or throws NSEE if empty |
589 |
|
*/ |
590 |
|
public void testRemove() { |
591 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
600 |
|
} |
601 |
|
|
602 |
|
/** |
603 |
< |
* |
603 |
> |
* remove(x) removes x and returns true if present |
604 |
|
*/ |
605 |
|
public void testRemoveElement() { |
606 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
615 |
|
} |
616 |
|
|
617 |
|
/** |
618 |
< |
* |
618 |
> |
* contains(x) reports true when elements added but not yet removed |
619 |
|
*/ |
620 |
|
public void testContains() { |
621 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
627 |
|
} |
628 |
|
|
629 |
|
/** |
630 |
< |
* |
630 |
> |
* clear removes all elements |
631 |
|
*/ |
632 |
|
public void testClear() { |
633 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
642 |
|
} |
643 |
|
|
644 |
|
/** |
645 |
< |
* |
645 |
> |
* containsAll(c) is true when c contains a subset of elements |
646 |
|
*/ |
647 |
|
public void testContainsAll() { |
648 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
656 |
|
} |
657 |
|
|
658 |
|
/** |
659 |
< |
* |
659 |
> |
* retainAll(c) retains only those elements of c and reports true if changed |
660 |
|
*/ |
661 |
|
public void testRetainAll() { |
662 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
675 |
|
} |
676 |
|
|
677 |
|
/** |
678 |
< |
* |
678 |
> |
* removeAll(c) removes only those elements of c and reports true if changed |
679 |
|
*/ |
680 |
|
public void testRemoveAll() { |
681 |
|
for (int i = 1; i < SIZE; ++i) { |
690 |
|
} |
691 |
|
} |
692 |
|
|
666 |
– |
|
693 |
|
/** |
694 |
< |
* |
694 |
> |
* toArray contains all elements |
695 |
|
*/ |
696 |
|
public void testToArray() { |
697 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
705 |
|
} |
706 |
|
|
707 |
|
/** |
708 |
< |
* |
708 |
> |
* toArray(a) contains all elements |
709 |
|
*/ |
710 |
|
public void testToArray2() { |
711 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
718 |
|
unexpectedException(); |
719 |
|
} |
720 |
|
} |
721 |
+ |
|
722 |
+ |
/** |
723 |
+ |
* toArray(null) throws NPE |
724 |
+ |
*/ |
725 |
+ |
public void testToArray_BadArg() { |
726 |
+ |
try { |
727 |
+ |
LinkedBlockingQueue q = populatedQueue(SIZE); |
728 |
+ |
Object o[] = q.toArray(null); |
729 |
+ |
shouldThrow(); |
730 |
+ |
} catch(NullPointerException success){} |
731 |
+ |
} |
732 |
+ |
|
733 |
+ |
/** |
734 |
+ |
* toArray with incompatable array type throws CCE |
735 |
+ |
*/ |
736 |
+ |
public void testToArray1_BadArg() { |
737 |
+ |
try { |
738 |
+ |
LinkedBlockingQueue q = populatedQueue(SIZE); |
739 |
+ |
Object o[] = q.toArray(new String[10] ); |
740 |
+ |
shouldThrow(); |
741 |
+ |
} catch(ArrayStoreException success){} |
742 |
+ |
} |
743 |
+ |
|
744 |
|
|
745 |
|
/** |
746 |
< |
* |
746 |
> |
* iterator iterates through all elements |
747 |
|
*/ |
748 |
|
public void testIterator() { |
749 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
758 |
|
} |
759 |
|
|
760 |
|
/** |
761 |
< |
* |
761 |
> |
* iterator.remove removes current element |
762 |
|
*/ |
763 |
< |
public void testIteratorOrdering() { |
715 |
< |
|
763 |
> |
public void testIteratorRemove () { |
764 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(3); |
765 |
+ |
q.add(two); |
766 |
+ |
q.add(one); |
767 |
+ |
q.add(three); |
768 |
+ |
|
769 |
+ |
Iterator it = q.iterator(); |
770 |
+ |
it.next(); |
771 |
+ |
it.remove(); |
772 |
+ |
|
773 |
+ |
it = q.iterator(); |
774 |
+ |
assertEquals(it.next(), one); |
775 |
+ |
assertEquals(it.next(), three); |
776 |
+ |
assertFalse(it.hasNext()); |
777 |
+ |
} |
778 |
+ |
|
779 |
|
|
780 |
+ |
/** |
781 |
+ |
* iterator ordering is FIFO |
782 |
+ |
*/ |
783 |
+ |
public void testIteratorOrdering() { |
784 |
+ |
final LinkedBlockingQueue q = new LinkedBlockingQueue(3); |
785 |
|
q.add(one); |
786 |
|
q.add(two); |
787 |
|
q.add(three); |
721 |
– |
|
788 |
|
assertEquals(0, q.remainingCapacity()); |
723 |
– |
|
789 |
|
int k = 0; |
790 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
791 |
|
int i = ((Integer)(it.next())).intValue(); |
792 |
|
assertEquals(++k, i); |
793 |
|
} |
729 |
– |
|
794 |
|
assertEquals(3, k); |
795 |
|
} |
796 |
|
|
797 |
|
/** |
798 |
< |
* |
798 |
> |
* Modifications do not cause iterators to fail |
799 |
|
*/ |
800 |
|
public void testWeaklyConsistentIteration () { |
737 |
– |
|
801 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(3); |
739 |
– |
|
802 |
|
q.add(one); |
803 |
|
q.add(two); |
804 |
|
q.add(three); |
743 |
– |
|
805 |
|
try { |
806 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
807 |
|
q.remove(); |
811 |
|
catch (ConcurrentModificationException e) { |
812 |
|
unexpectedException(); |
813 |
|
} |
753 |
– |
|
814 |
|
assertEquals(0, q.size()); |
815 |
|
} |
816 |
|
|
817 |
|
|
818 |
|
/** |
819 |
< |
* |
819 |
> |
* toString contains toStrings of elements |
820 |
|
*/ |
821 |
|
public void testToString() { |
822 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
828 |
|
|
829 |
|
|
830 |
|
/** |
831 |
< |
* |
831 |
> |
* offer transfers elements across Executor tasks |
832 |
|
*/ |
833 |
|
public void testOfferInExecutor() { |
774 |
– |
|
834 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
776 |
– |
|
835 |
|
q.add(one); |
836 |
|
q.add(two); |
779 |
– |
|
837 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
781 |
– |
|
838 |
|
executor.execute(new Runnable() { |
839 |
|
public void run() { |
840 |
|
threadAssertFalse(q.offer(three)); |
861 |
|
}); |
862 |
|
|
863 |
|
joinPool(executor); |
808 |
– |
|
864 |
|
} |
865 |
|
|
866 |
|
/** |
867 |
< |
* |
867 |
> |
* poll retrieves elements across Executor threads |
868 |
|
*/ |
869 |
|
public void testPollInExecutor() { |
815 |
– |
|
870 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
817 |
– |
|
871 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
819 |
– |
|
872 |
|
executor.execute(new Runnable() { |
873 |
|
public void run() { |
874 |
|
threadAssertNull(q.poll()); |
898 |
|
} |
899 |
|
|
900 |
|
/** |
901 |
< |
* |
901 |
> |
* A deserialized serialized queue has same elements in same order |
902 |
|
*/ |
903 |
|
public void testSerialization() { |
904 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
920 |
|
} |
921 |
|
} |
922 |
|
|
923 |
+ |
/** |
924 |
+ |
* drainTo(null) throws NPE |
925 |
+ |
*/ |
926 |
+ |
public void testDrainToNull() { |
927 |
+ |
LinkedBlockingQueue q = populatedQueue(SIZE); |
928 |
+ |
try { |
929 |
+ |
q.drainTo(null); |
930 |
+ |
shouldThrow(); |
931 |
+ |
} catch(NullPointerException success) { |
932 |
+ |
} |
933 |
+ |
} |
934 |
+ |
|
935 |
+ |
/** |
936 |
+ |
* drainTo(this) throws IAE |
937 |
+ |
*/ |
938 |
+ |
public void testDrainToSelf() { |
939 |
+ |
LinkedBlockingQueue q = populatedQueue(SIZE); |
940 |
+ |
try { |
941 |
+ |
q.drainTo(q); |
942 |
+ |
shouldThrow(); |
943 |
+ |
} catch(IllegalArgumentException success) { |
944 |
+ |
} |
945 |
+ |
} |
946 |
+ |
|
947 |
+ |
/** |
948 |
+ |
* drainTo(c) empties queue into another collection c |
949 |
+ |
*/ |
950 |
+ |
public void testDrainTo() { |
951 |
+ |
LinkedBlockingQueue q = populatedQueue(SIZE); |
952 |
+ |
ArrayList l = new ArrayList(); |
953 |
+ |
q.drainTo(l); |
954 |
+ |
assertEquals(q.size(), 0); |
955 |
+ |
assertEquals(l.size(), SIZE); |
956 |
+ |
for (int i = 0; i < SIZE; ++i) |
957 |
+ |
assertEquals(l.get(i), new Integer(i)); |
958 |
+ |
} |
959 |
+ |
|
960 |
+ |
/** |
961 |
+ |
* drainTo empties full queue, unblocking a waiting put. |
962 |
+ |
*/ |
963 |
+ |
public void testDrainToWithActivePut() { |
964 |
+ |
final LinkedBlockingQueue q = populatedQueue(SIZE); |
965 |
+ |
Thread t = new Thread(new Runnable() { |
966 |
+ |
public void run() { |
967 |
+ |
try { |
968 |
+ |
q.put(new Integer(SIZE+1)); |
969 |
+ |
} catch (InterruptedException ie){ |
970 |
+ |
threadUnexpectedException(); |
971 |
+ |
} |
972 |
+ |
} |
973 |
+ |
}); |
974 |
+ |
try { |
975 |
+ |
t.start(); |
976 |
+ |
ArrayList l = new ArrayList(); |
977 |
+ |
q.drainTo(l); |
978 |
+ |
assertTrue(l.size() >= SIZE); |
979 |
+ |
for (int i = 0; i < SIZE; ++i) |
980 |
+ |
assertEquals(l.get(i), new Integer(i)); |
981 |
+ |
t.join(); |
982 |
+ |
assertTrue(q.size() + l.size() == SIZE+1); |
983 |
+ |
} catch(Exception e){ |
984 |
+ |
unexpectedException(); |
985 |
+ |
} |
986 |
+ |
} |
987 |
+ |
|
988 |
+ |
/** |
989 |
+ |
* drainTo(null, n) throws NPE |
990 |
+ |
*/ |
991 |
+ |
public void testDrainToNullN() { |
992 |
+ |
LinkedBlockingQueue q = populatedQueue(SIZE); |
993 |
+ |
try { |
994 |
+ |
q.drainTo(null, 0); |
995 |
+ |
shouldThrow(); |
996 |
+ |
} catch(NullPointerException success) { |
997 |
+ |
} |
998 |
+ |
} |
999 |
+ |
|
1000 |
+ |
/** |
1001 |
+ |
* drainTo(this, n) throws IAE |
1002 |
+ |
*/ |
1003 |
+ |
public void testDrainToSelfN() { |
1004 |
+ |
LinkedBlockingQueue q = populatedQueue(SIZE); |
1005 |
+ |
try { |
1006 |
+ |
q.drainTo(q, 0); |
1007 |
+ |
shouldThrow(); |
1008 |
+ |
} catch(IllegalArgumentException success) { |
1009 |
+ |
} |
1010 |
+ |
} |
1011 |
+ |
|
1012 |
+ |
/** |
1013 |
+ |
* drainTo(c, n) empties first max {n, size} elements of queue into c |
1014 |
+ |
*/ |
1015 |
+ |
public void testDrainToN() { |
1016 |
+ |
for (int i = 0; i < SIZE + 2; ++i) { |
1017 |
+ |
LinkedBlockingQueue q = populatedQueue(SIZE); |
1018 |
+ |
ArrayList l = new ArrayList(); |
1019 |
+ |
q.drainTo(l, i); |
1020 |
+ |
int k = (i < SIZE)? i : SIZE; |
1021 |
+ |
assertEquals(q.size(), SIZE-k); |
1022 |
+ |
assertEquals(l.size(), k); |
1023 |
+ |
for (int j = 0; j < k; ++j) |
1024 |
+ |
assertEquals(l.get(j), new Integer(j)); |
1025 |
+ |
} |
1026 |
+ |
} |
1027 |
+ |
|
1028 |
|
} |