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 |
> |
* Offer succeeds if not full; fails if full |
156 |
|
*/ |
157 |
|
public void testOffer() { |
158 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
161 |
|
} |
162 |
|
|
163 |
|
/** |
164 |
< |
* |
164 |
> |
* add succeeds if not full; throws ISE if full |
165 |
|
*/ |
166 |
|
public void testAdd() { |
167 |
|
try { |
176 |
|
} |
177 |
|
|
178 |
|
/** |
179 |
< |
* |
179 |
> |
* addAll(null) throws NPE |
180 |
|
*/ |
181 |
|
public void testAddAll1() { |
182 |
|
try { |
187 |
|
catch (NullPointerException success) {} |
188 |
|
} |
189 |
|
/** |
190 |
< |
* |
190 |
> |
* addAll of a collection with null elements throws NPE |
191 |
|
*/ |
192 |
|
public void testAddAll2() { |
193 |
|
try { |
199 |
|
catch (NullPointerException success) {} |
200 |
|
} |
201 |
|
/** |
202 |
< |
* |
202 |
> |
* addAll of a collection with any null elements throws NPE after |
203 |
> |
* possibly adding some elements |
204 |
|
*/ |
205 |
|
public void testAddAll3() { |
206 |
|
try { |
214 |
|
catch (NullPointerException success) {} |
215 |
|
} |
216 |
|
/** |
217 |
< |
* |
217 |
> |
* addAll throws ISE if not enough room |
218 |
|
*/ |
219 |
|
public void testAddAll4() { |
220 |
|
try { |
228 |
|
catch (IllegalStateException success) {} |
229 |
|
} |
230 |
|
/** |
231 |
< |
* |
231 |
> |
* Queue contains all elements, in traversal order, of successful addAll |
232 |
|
*/ |
233 |
|
public void testAddAll5() { |
234 |
|
try { |
246 |
|
} |
247 |
|
|
248 |
|
/** |
249 |
< |
* |
249 |
> |
* put(null) throws NPE |
250 |
|
*/ |
251 |
|
public void testPutNull() { |
252 |
|
try { |
262 |
|
} |
263 |
|
|
264 |
|
/** |
265 |
< |
* |
265 |
> |
* all elements successfully put are contained |
266 |
|
*/ |
267 |
|
public void testPut() { |
268 |
|
try { |
280 |
|
} |
281 |
|
|
282 |
|
/** |
283 |
< |
* |
283 |
> |
* put blocks interruptibly if full |
284 |
|
*/ |
285 |
|
public void testBlockingPut() { |
286 |
|
Thread t = new Thread(new Runnable() { |
310 |
|
} |
311 |
|
|
312 |
|
/** |
313 |
< |
* |
313 |
> |
* put blocks waiting for take when full |
314 |
|
*/ |
315 |
|
public void testPutWithTake() { |
316 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
344 |
|
} |
345 |
|
|
346 |
|
/** |
347 |
< |
* |
347 |
> |
* timed offer times out if full and elements not taken |
348 |
|
*/ |
349 |
|
public void testTimedOffer() { |
350 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
371 |
|
} |
372 |
|
|
373 |
|
/** |
374 |
< |
* |
374 |
> |
* take retrieves elements in FIFO order |
375 |
|
*/ |
376 |
|
public void testTake() { |
377 |
|
try { |
385 |
|
} |
386 |
|
|
387 |
|
/** |
388 |
< |
* |
388 |
> |
* take blocks interruptibly when empty |
389 |
|
*/ |
390 |
|
public void testTakeFromEmpty() { |
391 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
408 |
|
} |
409 |
|
|
410 |
|
/** |
411 |
< |
* |
411 |
> |
* Take removes existing elements until empty, then blocks interruptibly |
412 |
|
*/ |
413 |
|
public void testBlockingTake() { |
414 |
|
Thread t = new Thread(new Runnable() { |
436 |
|
|
437 |
|
|
438 |
|
/** |
439 |
< |
* |
439 |
> |
* poll succeeds unless empty |
440 |
|
*/ |
441 |
|
public void testPoll() { |
442 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
447 |
|
} |
448 |
|
|
449 |
|
/** |
450 |
< |
* |
450 |
> |
* timed pool with zero timeout succeeds when non-empty, else times out |
451 |
|
*/ |
452 |
|
public void testTimedPoll0() { |
453 |
|
try { |
462 |
|
} |
463 |
|
|
464 |
|
/** |
465 |
< |
* |
465 |
> |
* timed pool with nonzero timeout succeeds when non-empty, else times out |
466 |
|
*/ |
467 |
|
public void testTimedPoll() { |
468 |
|
try { |
477 |
|
} |
478 |
|
|
479 |
|
/** |
480 |
< |
* |
480 |
> |
* Interrupted timed poll throws InterruptedException instead of |
481 |
> |
* returning timeout status |
482 |
|
*/ |
483 |
|
public void testInterruptedTimedPoll() { |
484 |
|
Thread t = new Thread(new Runnable() { |
504 |
|
} |
505 |
|
|
506 |
|
/** |
507 |
< |
* |
507 |
> |
* timed poll before a delayed offer fails; after offer succeeds; |
508 |
> |
* on interruption throws |
509 |
|
*/ |
510 |
|
public void testTimedPollWithOffer() { |
511 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
530 |
|
} |
531 |
|
} |
532 |
|
|
529 |
– |
|
533 |
|
/** |
534 |
< |
* |
534 |
> |
* peek returns next element, or null if empty |
535 |
|
*/ |
536 |
|
public void testPeek() { |
537 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
545 |
|
} |
546 |
|
|
547 |
|
/** |
548 |
< |
* |
548 |
> |
* element returns next element, or throws NSEE if empty |
549 |
|
*/ |
550 |
|
public void testElement() { |
551 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
561 |
|
} |
562 |
|
|
563 |
|
/** |
564 |
< |
* |
564 |
> |
* remove removes next element, or throws NSEE if empty |
565 |
|
*/ |
566 |
|
public void testRemove() { |
567 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
576 |
|
} |
577 |
|
|
578 |
|
/** |
579 |
< |
* |
579 |
> |
* remove(x) removes x and returns true if present |
580 |
|
*/ |
581 |
|
public void testRemoveElement() { |
582 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
591 |
|
} |
592 |
|
|
593 |
|
/** |
594 |
< |
* |
594 |
> |
* contains(x) reports true when elements added but not yet removed |
595 |
|
*/ |
596 |
|
public void testContains() { |
597 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
603 |
|
} |
604 |
|
|
605 |
|
/** |
606 |
< |
* |
606 |
> |
* clear removes all elements |
607 |
|
*/ |
608 |
|
public void testClear() { |
609 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
618 |
|
} |
619 |
|
|
620 |
|
/** |
621 |
< |
* |
621 |
> |
* containsAll(c) is true when c contains a subset of elements |
622 |
|
*/ |
623 |
|
public void testContainsAll() { |
624 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
632 |
|
} |
633 |
|
|
634 |
|
/** |
635 |
< |
* |
635 |
> |
* retainAll(c) retains only those elements of c and reports true if changed |
636 |
|
*/ |
637 |
|
public void testRetainAll() { |
638 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
651 |
|
} |
652 |
|
|
653 |
|
/** |
654 |
< |
* |
654 |
> |
* removeAll(c) removes only those elements of c and reports true if changed |
655 |
|
*/ |
656 |
|
public void testRemoveAll() { |
657 |
|
for (int i = 1; i < SIZE; ++i) { |
666 |
|
} |
667 |
|
} |
668 |
|
|
666 |
– |
|
669 |
|
/** |
670 |
< |
* |
670 |
> |
* toArray contains all elements |
671 |
|
*/ |
672 |
|
public void testToArray() { |
673 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
681 |
|
} |
682 |
|
|
683 |
|
/** |
684 |
< |
* |
684 |
> |
* toArray(a) contains all elements |
685 |
|
*/ |
686 |
|
public void testToArray2() { |
687 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
696 |
|
} |
697 |
|
|
698 |
|
/** |
699 |
< |
* |
699 |
> |
* iterator iterates through all elements |
700 |
|
*/ |
701 |
|
public void testIterator() { |
702 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
711 |
|
} |
712 |
|
|
713 |
|
/** |
714 |
< |
* |
714 |
> |
* iterator.remove removes current element |
715 |
|
*/ |
716 |
< |
public void testIteratorOrdering() { |
715 |
< |
|
716 |
> |
public void testIteratorRemove () { |
717 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(3); |
718 |
+ |
q.add(two); |
719 |
+ |
q.add(one); |
720 |
+ |
q.add(three); |
721 |
|
|
722 |
+ |
Iterator it = q.iterator(); |
723 |
+ |
it.next(); |
724 |
+ |
it.remove(); |
725 |
+ |
|
726 |
+ |
it = q.iterator(); |
727 |
+ |
assertEquals(it.next(), one); |
728 |
+ |
assertEquals(it.next(), three); |
729 |
+ |
assertFalse(it.hasNext()); |
730 |
+ |
} |
731 |
+ |
|
732 |
+ |
|
733 |
+ |
/** |
734 |
+ |
* iterator ordering is FIFO |
735 |
+ |
*/ |
736 |
+ |
public void testIteratorOrdering() { |
737 |
+ |
final LinkedBlockingQueue q = new LinkedBlockingQueue(3); |
738 |
|
q.add(one); |
739 |
|
q.add(two); |
740 |
|
q.add(three); |
721 |
– |
|
741 |
|
assertEquals(0, q.remainingCapacity()); |
723 |
– |
|
742 |
|
int k = 0; |
743 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
744 |
|
int i = ((Integer)(it.next())).intValue(); |
745 |
|
assertEquals(++k, i); |
746 |
|
} |
729 |
– |
|
747 |
|
assertEquals(3, k); |
748 |
|
} |
749 |
|
|
750 |
|
/** |
751 |
< |
* |
751 |
> |
* Modifications do not cause iterators to fail |
752 |
|
*/ |
753 |
|
public void testWeaklyConsistentIteration () { |
737 |
– |
|
754 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(3); |
739 |
– |
|
755 |
|
q.add(one); |
756 |
|
q.add(two); |
757 |
|
q.add(three); |
743 |
– |
|
758 |
|
try { |
759 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
760 |
|
q.remove(); |
764 |
|
catch (ConcurrentModificationException e) { |
765 |
|
unexpectedException(); |
766 |
|
} |
753 |
– |
|
767 |
|
assertEquals(0, q.size()); |
768 |
|
} |
769 |
|
|
770 |
|
|
771 |
|
/** |
772 |
< |
* |
772 |
> |
* toString contains toStrings of elements |
773 |
|
*/ |
774 |
|
public void testToString() { |
775 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
781 |
|
|
782 |
|
|
783 |
|
/** |
784 |
< |
* |
784 |
> |
* offer transfers elements across Executor tasks |
785 |
|
*/ |
786 |
|
public void testOfferInExecutor() { |
774 |
– |
|
787 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
776 |
– |
|
788 |
|
q.add(one); |
789 |
|
q.add(two); |
779 |
– |
|
790 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
781 |
– |
|
791 |
|
executor.execute(new Runnable() { |
792 |
|
public void run() { |
793 |
|
threadAssertFalse(q.offer(three)); |
814 |
|
}); |
815 |
|
|
816 |
|
joinPool(executor); |
808 |
– |
|
817 |
|
} |
818 |
|
|
819 |
|
/** |
820 |
< |
* |
820 |
> |
* poll retrieves elements across Executor threads |
821 |
|
*/ |
822 |
|
public void testPollInExecutor() { |
815 |
– |
|
823 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
817 |
– |
|
824 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
819 |
– |
|
825 |
|
executor.execute(new Runnable() { |
826 |
|
public void run() { |
827 |
|
threadAssertNull(q.poll()); |
851 |
|
} |
852 |
|
|
853 |
|
/** |
854 |
< |
* |
854 |
> |
* A deserialized serialized queue has same elements in same order |
855 |
|
*/ |
856 |
|
public void testSerialization() { |
857 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |