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.*; |
14 |
|
public class LinkedBlockingQueueTest extends JSR166TestCase { |
15 |
|
|
16 |
|
public static void main(String[] args) { |
17 |
< |
junit.textui.TestRunner.run (suite()); |
17 |
> |
junit.textui.TestRunner.run (suite()); |
18 |
|
} |
19 |
|
|
20 |
|
public static Test suite() { |
36 |
|
assertEquals(n, q.size()); |
37 |
|
return q; |
38 |
|
} |
39 |
< |
|
39 |
> |
|
40 |
|
/** |
41 |
< |
* |
41 |
> |
* A new queue has the indicated capacity, or Integer.MAX_VALUE if |
42 |
> |
* none given |
43 |
|
*/ |
44 |
|
public void testConstructor1() { |
45 |
|
assertEquals(SIZE, new LinkedBlockingQueue(SIZE).remainingCapacity()); |
46 |
+ |
assertEquals(Integer.MAX_VALUE, new LinkedBlockingQueue().remainingCapacity()); |
47 |
|
} |
48 |
|
|
49 |
|
/** |
50 |
< |
* |
50 |
> |
* Constructor throws IAE if capacity argument nonpositive |
51 |
|
*/ |
52 |
|
public void testConstructor2() { |
53 |
|
try { |
58 |
|
} |
59 |
|
|
60 |
|
/** |
61 |
< |
* |
61 |
> |
* Initializing from null Collection throws NPE |
62 |
|
*/ |
63 |
|
public void testConstructor3() { |
61 |
– |
|
64 |
|
try { |
65 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(null); |
66 |
|
shouldThrow(); |
69 |
|
} |
70 |
|
|
71 |
|
/** |
72 |
< |
* |
72 |
> |
* Initializing from Collection of null elements throws NPE |
73 |
|
*/ |
74 |
|
public void testConstructor4() { |
75 |
|
try { |
81 |
|
} |
82 |
|
|
83 |
|
/** |
84 |
< |
* |
84 |
> |
* Initializing from Collection with some null elements throws NPE |
85 |
|
*/ |
86 |
|
public void testConstructor5() { |
87 |
|
try { |
95 |
|
} |
96 |
|
|
97 |
|
/** |
98 |
< |
* |
98 |
> |
* Queue contains all elements of collection used to initialize |
99 |
|
*/ |
100 |
|
public void testConstructor6() { |
101 |
|
try { |
110 |
|
} |
111 |
|
|
112 |
|
/** |
113 |
< |
* |
113 |
> |
* Queue transitions from empty to full when elements added |
114 |
|
*/ |
115 |
|
public void testEmptyFull() { |
116 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
125 |
|
} |
126 |
|
|
127 |
|
/** |
128 |
< |
* |
128 |
> |
* remainingCapacity decreases on add, increases on remove |
129 |
|
*/ |
130 |
|
public void testRemainingCapacity() { |
131 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
142 |
|
} |
143 |
|
|
144 |
|
/** |
145 |
< |
* |
145 |
> |
* offer(null) throws NPE |
146 |
|
*/ |
147 |
|
public void testOfferNull() { |
148 |
|
try { |
149 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
150 |
|
q.offer(null); |
151 |
|
shouldThrow(); |
152 |
< |
} catch (NullPointerException success) { } |
152 |
> |
} catch (NullPointerException success) { } |
153 |
> |
} |
154 |
> |
|
155 |
> |
/** |
156 |
> |
* add(null) throws NPE |
157 |
> |
*/ |
158 |
> |
public void testAddNull() { |
159 |
> |
try { |
160 |
> |
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
161 |
> |
q.add(null); |
162 |
> |
shouldThrow(); |
163 |
> |
} catch (NullPointerException success) { } |
164 |
|
} |
165 |
|
|
166 |
|
/** |
167 |
< |
* |
167 |
> |
* Offer succeeds if not full; fails if full |
168 |
|
*/ |
169 |
|
public void testOffer() { |
170 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
173 |
|
} |
174 |
|
|
175 |
|
/** |
176 |
< |
* |
176 |
> |
* add succeeds if not full; throws ISE if full |
177 |
|
*/ |
178 |
|
public void testAdd() { |
179 |
|
try { |
184 |
|
assertEquals(0, q.remainingCapacity()); |
185 |
|
q.add(new Integer(SIZE)); |
186 |
|
} catch (IllegalStateException success){ |
187 |
< |
} |
187 |
> |
} |
188 |
|
} |
189 |
|
|
190 |
|
/** |
191 |
< |
* |
191 |
> |
* addAll(null) throws NPE |
192 |
|
*/ |
193 |
|
public void testAddAll1() { |
194 |
|
try { |
198 |
|
} |
199 |
|
catch (NullPointerException success) {} |
200 |
|
} |
201 |
+ |
|
202 |
+ |
/** |
203 |
+ |
* addAll(this) throws IAE |
204 |
+ |
*/ |
205 |
+ |
public void testAddAllSelf() { |
206 |
+ |
try { |
207 |
+ |
LinkedBlockingQueue q = populatedQueue(SIZE); |
208 |
+ |
q.addAll(q); |
209 |
+ |
shouldThrow(); |
210 |
+ |
} |
211 |
+ |
catch (IllegalArgumentException success) {} |
212 |
+ |
} |
213 |
+ |
|
214 |
|
/** |
215 |
< |
* |
215 |
> |
* addAll of a collection with null elements throws NPE |
216 |
|
*/ |
217 |
|
public void testAddAll2() { |
218 |
|
try { |
224 |
|
catch (NullPointerException success) {} |
225 |
|
} |
226 |
|
/** |
227 |
< |
* |
227 |
> |
* addAll of a collection with any null elements throws NPE after |
228 |
> |
* possibly adding some elements |
229 |
|
*/ |
230 |
|
public void testAddAll3() { |
231 |
|
try { |
239 |
|
catch (NullPointerException success) {} |
240 |
|
} |
241 |
|
/** |
242 |
< |
* |
242 |
> |
* addAll throws ISE if not enough room |
243 |
|
*/ |
244 |
|
public void testAddAll4() { |
245 |
|
try { |
253 |
|
catch (IllegalStateException success) {} |
254 |
|
} |
255 |
|
/** |
256 |
< |
* |
256 |
> |
* Queue contains all elements, in traversal order, of successful addAll |
257 |
|
*/ |
258 |
|
public void testAddAll5() { |
259 |
|
try { |
271 |
|
} |
272 |
|
|
273 |
|
/** |
274 |
< |
* |
274 |
> |
* put(null) throws NPE |
275 |
|
*/ |
276 |
|
public void testPutNull() { |
277 |
|
try { |
278 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
279 |
|
q.put(null); |
280 |
|
shouldThrow(); |
281 |
< |
} |
281 |
> |
} |
282 |
|
catch (NullPointerException success){ |
283 |
< |
} |
283 |
> |
} |
284 |
|
catch (InterruptedException ie) { |
285 |
|
unexpectedException(); |
286 |
|
} |
287 |
|
} |
288 |
|
|
289 |
|
/** |
290 |
< |
* |
290 |
> |
* all elements successfully put are contained |
291 |
|
*/ |
292 |
|
public void testPut() { |
293 |
|
try { |
305 |
|
} |
306 |
|
|
307 |
|
/** |
308 |
< |
* |
308 |
> |
* put blocks interruptibly if full |
309 |
|
*/ |
310 |
|
public void testBlockingPut() { |
311 |
|
Thread t = new Thread(new Runnable() { |
321 |
|
threadShouldThrow(); |
322 |
|
} catch (InterruptedException ie){ |
323 |
|
threadAssertEquals(added, SIZE); |
324 |
< |
} |
324 |
> |
} |
325 |
|
}}); |
326 |
|
t.start(); |
327 |
< |
try { |
328 |
< |
Thread.sleep(SHORT_DELAY_MS); |
327 |
> |
try { |
328 |
> |
Thread.sleep(SHORT_DELAY_MS); |
329 |
|
t.interrupt(); |
330 |
|
t.join(); |
331 |
|
} |
335 |
|
} |
336 |
|
|
337 |
|
/** |
338 |
< |
* |
338 |
> |
* put blocks waiting for take when full |
339 |
|
*/ |
340 |
|
public void testPutWithTake() { |
341 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
369 |
|
} |
370 |
|
|
371 |
|
/** |
372 |
< |
* |
372 |
> |
* timed offer times out if full and elements not taken |
373 |
|
*/ |
374 |
|
public void testTimedOffer() { |
375 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
384 |
|
} catch (InterruptedException success){} |
385 |
|
} |
386 |
|
}); |
387 |
< |
|
387 |
> |
|
388 |
|
try { |
389 |
|
t.start(); |
390 |
|
Thread.sleep(SMALL_DELAY_MS); |
396 |
|
} |
397 |
|
|
398 |
|
/** |
399 |
< |
* |
399 |
> |
* take retrieves elements in FIFO order |
400 |
|
*/ |
401 |
|
public void testTake() { |
402 |
|
try { |
406 |
|
} |
407 |
|
} catch (InterruptedException e){ |
408 |
|
unexpectedException(); |
409 |
< |
} |
409 |
> |
} |
410 |
|
} |
411 |
|
|
412 |
|
/** |
413 |
< |
* |
413 |
> |
* take blocks interruptibly when empty |
414 |
|
*/ |
415 |
|
public void testTakeFromEmpty() { |
416 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
419 |
|
try { |
420 |
|
q.take(); |
421 |
|
threadShouldThrow(); |
422 |
< |
} catch (InterruptedException success){ } |
422 |
> |
} catch (InterruptedException success){ } |
423 |
|
} |
424 |
|
}); |
425 |
|
try { |
433 |
|
} |
434 |
|
|
435 |
|
/** |
436 |
< |
* |
436 |
> |
* Take removes existing elements until empty, then blocks interruptibly |
437 |
|
*/ |
438 |
|
public void testBlockingTake() { |
439 |
|
Thread t = new Thread(new Runnable() { |
446 |
|
q.take(); |
447 |
|
threadShouldThrow(); |
448 |
|
} catch (InterruptedException success){ |
449 |
< |
} |
449 |
> |
} |
450 |
|
}}); |
451 |
|
t.start(); |
452 |
< |
try { |
453 |
< |
Thread.sleep(SHORT_DELAY_MS); |
452 |
> |
try { |
453 |
> |
Thread.sleep(SHORT_DELAY_MS); |
454 |
|
t.interrupt(); |
455 |
|
t.join(); |
456 |
|
} |
461 |
|
|
462 |
|
|
463 |
|
/** |
464 |
< |
* |
464 |
> |
* poll succeeds unless empty |
465 |
|
*/ |
466 |
|
public void testPoll() { |
467 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
472 |
|
} |
473 |
|
|
474 |
|
/** |
475 |
< |
* |
475 |
> |
* timed pool with zero timeout succeeds when non-empty, else times out |
476 |
|
*/ |
477 |
|
public void testTimedPoll0() { |
478 |
|
try { |
483 |
|
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
484 |
|
} catch (InterruptedException e){ |
485 |
|
unexpectedException(); |
486 |
< |
} |
486 |
> |
} |
487 |
|
} |
488 |
|
|
489 |
|
/** |
490 |
< |
* |
490 |
> |
* timed pool with nonzero timeout succeeds when non-empty, else times out |
491 |
|
*/ |
492 |
|
public void testTimedPoll() { |
493 |
|
try { |
498 |
|
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
499 |
|
} catch (InterruptedException e){ |
500 |
|
unexpectedException(); |
501 |
< |
} |
501 |
> |
} |
502 |
|
} |
503 |
|
|
504 |
|
/** |
505 |
< |
* |
505 |
> |
* Interrupted timed poll throws InterruptedException instead of |
506 |
> |
* returning timeout status |
507 |
|
*/ |
508 |
|
public void testInterruptedTimedPoll() { |
509 |
|
Thread t = new Thread(new Runnable() { |
515 |
|
} |
516 |
|
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
517 |
|
} catch (InterruptedException success){ |
518 |
< |
} |
518 |
> |
} |
519 |
|
}}); |
520 |
|
t.start(); |
521 |
< |
try { |
522 |
< |
Thread.sleep(SHORT_DELAY_MS); |
521 |
> |
try { |
522 |
> |
Thread.sleep(SHORT_DELAY_MS); |
523 |
|
t.interrupt(); |
524 |
|
t.join(); |
525 |
|
} |
529 |
|
} |
530 |
|
|
531 |
|
/** |
532 |
< |
* |
532 |
> |
* timed poll before a delayed offer fails; after offer succeeds; |
533 |
> |
* on interruption throws |
534 |
|
*/ |
535 |
|
public void testTimedPollWithOffer() { |
536 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
541 |
|
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
542 |
|
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
543 |
|
threadShouldThrow(); |
544 |
< |
} catch (InterruptedException success) { } |
544 |
> |
} catch (InterruptedException success) { } |
545 |
|
} |
546 |
|
}); |
547 |
|
try { |
553 |
|
} catch (Exception e){ |
554 |
|
unexpectedException(); |
555 |
|
} |
556 |
< |
} |
528 |
< |
|
556 |
> |
} |
557 |
|
|
558 |
|
/** |
559 |
< |
* |
559 |
> |
* peek returns next element, or null if empty |
560 |
|
*/ |
561 |
|
public void testPeek() { |
562 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
570 |
|
} |
571 |
|
|
572 |
|
/** |
573 |
< |
* |
573 |
> |
* element returns next element, or throws NSEE if empty |
574 |
|
*/ |
575 |
|
public void testElement() { |
576 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
586 |
|
} |
587 |
|
|
588 |
|
/** |
589 |
< |
* |
589 |
> |
* remove removes next element, or throws NSEE if empty |
590 |
|
*/ |
591 |
|
public void testRemove() { |
592 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
597 |
|
q.remove(); |
598 |
|
shouldThrow(); |
599 |
|
} catch (NoSuchElementException success){ |
600 |
< |
} |
600 |
> |
} |
601 |
|
} |
602 |
|
|
603 |
|
/** |
604 |
< |
* |
604 |
> |
* remove(x) removes x and returns true if present |
605 |
|
*/ |
606 |
|
public void testRemoveElement() { |
607 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
614 |
|
} |
615 |
|
assertTrue(q.isEmpty()); |
616 |
|
} |
617 |
< |
|
617 |
> |
|
618 |
|
/** |
619 |
< |
* |
619 |
> |
* An add following remove(x) succeeds |
620 |
> |
*/ |
621 |
> |
public void testRemoveElementAndAdd() { |
622 |
> |
try { |
623 |
> |
LinkedBlockingQueue q = new LinkedBlockingQueue(); |
624 |
> |
assertTrue(q.add(new Integer(1))); |
625 |
> |
assertTrue(q.add(new Integer(2))); |
626 |
> |
assertTrue(q.remove(new Integer(1))); |
627 |
> |
assertTrue(q.remove(new Integer(2))); |
628 |
> |
assertTrue(q.add(new Integer(3))); |
629 |
> |
assertTrue(q.take() != null); |
630 |
> |
} catch (Exception e){ |
631 |
> |
unexpectedException(); |
632 |
> |
} |
633 |
> |
} |
634 |
> |
|
635 |
> |
/** |
636 |
> |
* contains(x) reports true when elements added but not yet removed |
637 |
|
*/ |
638 |
|
public void testContains() { |
639 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
645 |
|
} |
646 |
|
|
647 |
|
/** |
648 |
< |
* |
648 |
> |
* clear removes all elements |
649 |
|
*/ |
650 |
|
public void testClear() { |
651 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
655 |
|
assertEquals(SIZE, q.remainingCapacity()); |
656 |
|
q.add(one); |
657 |
|
assertFalse(q.isEmpty()); |
658 |
+ |
assertTrue(q.contains(one)); |
659 |
|
q.clear(); |
660 |
|
assertTrue(q.isEmpty()); |
661 |
|
} |
662 |
|
|
663 |
|
/** |
664 |
< |
* |
664 |
> |
* containsAll(c) is true when c contains a subset of elements |
665 |
|
*/ |
666 |
|
public void testContainsAll() { |
667 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
675 |
|
} |
676 |
|
|
677 |
|
/** |
678 |
< |
* |
678 |
> |
* retainAll(c) retains only those elements of c and reports true if changed |
679 |
|
*/ |
680 |
|
public void testRetainAll() { |
681 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
694 |
|
} |
695 |
|
|
696 |
|
/** |
697 |
< |
* |
697 |
> |
* removeAll(c) removes only those elements of c and reports true if changed |
698 |
|
*/ |
699 |
|
public void testRemoveAll() { |
700 |
|
for (int i = 1; i < SIZE; ++i) { |
709 |
|
} |
710 |
|
} |
711 |
|
|
666 |
– |
|
712 |
|
/** |
713 |
< |
* |
713 |
> |
* toArray contains all elements |
714 |
|
*/ |
715 |
|
public void testToArray() { |
716 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
720 |
|
assertEquals(o[i], q.take()); |
721 |
|
} catch (InterruptedException e){ |
722 |
|
unexpectedException(); |
723 |
< |
} |
723 |
> |
} |
724 |
|
} |
725 |
|
|
726 |
|
/** |
727 |
< |
* |
727 |
> |
* toArray(a) contains all elements |
728 |
|
*/ |
729 |
|
public void testToArray2() { |
730 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
735 |
|
assertEquals(ints[i], q.take()); |
736 |
|
} catch (InterruptedException e){ |
737 |
|
unexpectedException(); |
738 |
< |
} |
738 |
> |
} |
739 |
> |
} |
740 |
> |
|
741 |
> |
/** |
742 |
> |
* toArray(null) throws NPE |
743 |
> |
*/ |
744 |
> |
public void testToArray_BadArg() { |
745 |
> |
try { |
746 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
747 |
> |
Object o[] = q.toArray(null); |
748 |
> |
shouldThrow(); |
749 |
> |
} catch(NullPointerException success){} |
750 |
> |
} |
751 |
> |
|
752 |
> |
/** |
753 |
> |
* toArray with incompatible array type throws CCE |
754 |
> |
*/ |
755 |
> |
public void testToArray1_BadArg() { |
756 |
> |
try { |
757 |
> |
LinkedBlockingQueue q = populatedQueue(SIZE); |
758 |
> |
Object o[] = q.toArray(new String[10] ); |
759 |
> |
shouldThrow(); |
760 |
> |
} catch(ArrayStoreException success){} |
761 |
|
} |
762 |
< |
|
762 |
> |
|
763 |
> |
|
764 |
|
/** |
765 |
< |
* |
765 |
> |
* iterator iterates through all elements |
766 |
|
*/ |
767 |
|
public void testIterator() { |
768 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
773 |
|
} |
774 |
|
} catch (InterruptedException e){ |
775 |
|
unexpectedException(); |
776 |
< |
} |
776 |
> |
} |
777 |
|
} |
778 |
|
|
779 |
|
/** |
780 |
< |
* |
780 |
> |
* iterator.remove removes current element |
781 |
|
*/ |
782 |
< |
public void testIteratorOrdering() { |
715 |
< |
|
782 |
> |
public void testIteratorRemove () { |
783 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(3); |
784 |
+ |
q.add(two); |
785 |
+ |
q.add(one); |
786 |
+ |
q.add(three); |
787 |
|
|
788 |
+ |
Iterator it = q.iterator(); |
789 |
+ |
it.next(); |
790 |
+ |
it.remove(); |
791 |
+ |
|
792 |
+ |
it = q.iterator(); |
793 |
+ |
assertEquals(it.next(), one); |
794 |
+ |
assertEquals(it.next(), three); |
795 |
+ |
assertFalse(it.hasNext()); |
796 |
+ |
} |
797 |
+ |
|
798 |
+ |
|
799 |
+ |
/** |
800 |
+ |
* iterator ordering is FIFO |
801 |
+ |
*/ |
802 |
+ |
public void testIteratorOrdering() { |
803 |
+ |
final LinkedBlockingQueue q = new LinkedBlockingQueue(3); |
804 |
|
q.add(one); |
805 |
|
q.add(two); |
806 |
|
q.add(three); |
721 |
– |
|
807 |
|
assertEquals(0, q.remainingCapacity()); |
723 |
– |
|
808 |
|
int k = 0; |
809 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
810 |
|
int i = ((Integer)(it.next())).intValue(); |
811 |
|
assertEquals(++k, i); |
812 |
|
} |
729 |
– |
|
813 |
|
assertEquals(3, k); |
814 |
|
} |
815 |
|
|
816 |
|
/** |
817 |
< |
* |
817 |
> |
* Modifications do not cause iterators to fail |
818 |
|
*/ |
819 |
|
public void testWeaklyConsistentIteration () { |
737 |
– |
|
820 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(3); |
739 |
– |
|
821 |
|
q.add(one); |
822 |
|
q.add(two); |
823 |
|
q.add(three); |
743 |
– |
|
824 |
|
try { |
825 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
826 |
|
q.remove(); |
830 |
|
catch (ConcurrentModificationException e) { |
831 |
|
unexpectedException(); |
832 |
|
} |
753 |
– |
|
833 |
|
assertEquals(0, q.size()); |
834 |
|
} |
835 |
|
|
836 |
|
|
837 |
|
/** |
838 |
< |
* |
838 |
> |
* toString contains toStrings of elements |
839 |
|
*/ |
840 |
|
public void testToString() { |
841 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
843 |
|
for (int i = 0; i < SIZE; ++i) { |
844 |
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
845 |
|
} |
846 |
< |
} |
846 |
> |
} |
847 |
|
|
848 |
|
|
849 |
|
/** |
850 |
< |
* |
850 |
> |
* offer transfers elements across Executor tasks |
851 |
|
*/ |
852 |
|
public void testOfferInExecutor() { |
774 |
– |
|
853 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
776 |
– |
|
854 |
|
q.add(one); |
855 |
|
q.add(two); |
779 |
– |
|
856 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
781 |
– |
|
857 |
|
executor.execute(new Runnable() { |
858 |
|
public void run() { |
859 |
|
threadAssertFalse(q.offer(three)); |
878 |
|
} |
879 |
|
} |
880 |
|
}); |
806 |
– |
|
807 |
– |
joinPool(executor); |
881 |
|
|
882 |
+ |
joinPool(executor); |
883 |
|
} |
884 |
|
|
885 |
|
/** |
886 |
< |
* |
886 |
> |
* poll retrieves elements across Executor threads |
887 |
|
*/ |
888 |
|
public void testPollInExecutor() { |
815 |
– |
|
889 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
817 |
– |
|
890 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
819 |
– |
|
891 |
|
executor.execute(new Runnable() { |
892 |
|
public void run() { |
893 |
|
threadAssertNull(q.poll()); |
912 |
|
} |
913 |
|
} |
914 |
|
}); |
915 |
< |
|
915 |
> |
|
916 |
|
joinPool(executor); |
917 |
|
} |
918 |
|
|
919 |
|
/** |
920 |
< |
* |
920 |
> |
* A deserialized serialized queue has same elements in same order |
921 |
|
*/ |
922 |
|
public void testSerialization() { |
923 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
932 |
|
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
933 |
|
LinkedBlockingQueue r = (LinkedBlockingQueue)in.readObject(); |
934 |
|
assertEquals(q.size(), r.size()); |
935 |
< |
while (!q.isEmpty()) |
935 |
> |
while (!q.isEmpty()) |
936 |
|
assertEquals(q.remove(), r.remove()); |
937 |
|
} catch(Exception e){ |
938 |
|
unexpectedException(); |
939 |
|
} |
940 |
|
} |
941 |
|
|
942 |
+ |
/** |
943 |
+ |
* drainTo(null) throws NPE |
944 |
+ |
*/ |
945 |
+ |
public void testDrainToNull() { |
946 |
+ |
LinkedBlockingQueue q = populatedQueue(SIZE); |
947 |
+ |
try { |
948 |
+ |
q.drainTo(null); |
949 |
+ |
shouldThrow(); |
950 |
+ |
} catch(NullPointerException success) { |
951 |
+ |
} |
952 |
+ |
} |
953 |
+ |
|
954 |
+ |
/** |
955 |
+ |
* drainTo(this) throws IAE |
956 |
+ |
*/ |
957 |
+ |
public void testDrainToSelf() { |
958 |
+ |
LinkedBlockingQueue q = populatedQueue(SIZE); |
959 |
+ |
try { |
960 |
+ |
q.drainTo(q); |
961 |
+ |
shouldThrow(); |
962 |
+ |
} catch(IllegalArgumentException success) { |
963 |
+ |
} |
964 |
+ |
} |
965 |
+ |
|
966 |
+ |
/** |
967 |
+ |
* drainTo(c) empties queue into another collection c |
968 |
+ |
*/ |
969 |
+ |
public void testDrainTo() { |
970 |
+ |
LinkedBlockingQueue q = populatedQueue(SIZE); |
971 |
+ |
ArrayList l = new ArrayList(); |
972 |
+ |
q.drainTo(l); |
973 |
+ |
assertEquals(q.size(), 0); |
974 |
+ |
assertEquals(l.size(), SIZE); |
975 |
+ |
for (int i = 0; i < SIZE; ++i) |
976 |
+ |
assertEquals(l.get(i), new Integer(i)); |
977 |
+ |
q.add(zero); |
978 |
+ |
q.add(one); |
979 |
+ |
assertFalse(q.isEmpty()); |
980 |
+ |
assertTrue(q.contains(zero)); |
981 |
+ |
assertTrue(q.contains(one)); |
982 |
+ |
l.clear(); |
983 |
+ |
q.drainTo(l); |
984 |
+ |
assertEquals(q.size(), 0); |
985 |
+ |
assertEquals(l.size(), 2); |
986 |
+ |
for (int i = 0; i < 2; ++i) |
987 |
+ |
assertEquals(l.get(i), new Integer(i)); |
988 |
+ |
} |
989 |
+ |
|
990 |
+ |
/** |
991 |
+ |
* drainTo empties full queue, unblocking a waiting put. |
992 |
+ |
*/ |
993 |
+ |
public void testDrainToWithActivePut() { |
994 |
+ |
final LinkedBlockingQueue q = populatedQueue(SIZE); |
995 |
+ |
Thread t = new Thread(new Runnable() { |
996 |
+ |
public void run() { |
997 |
+ |
try { |
998 |
+ |
q.put(new Integer(SIZE+1)); |
999 |
+ |
} catch (InterruptedException ie){ |
1000 |
+ |
threadUnexpectedException(); |
1001 |
+ |
} |
1002 |
+ |
} |
1003 |
+ |
}); |
1004 |
+ |
try { |
1005 |
+ |
t.start(); |
1006 |
+ |
ArrayList l = new ArrayList(); |
1007 |
+ |
q.drainTo(l); |
1008 |
+ |
assertTrue(l.size() >= SIZE); |
1009 |
+ |
for (int i = 0; i < SIZE; ++i) |
1010 |
+ |
assertEquals(l.get(i), new Integer(i)); |
1011 |
+ |
t.join(); |
1012 |
+ |
assertTrue(q.size() + l.size() >= SIZE); |
1013 |
+ |
} catch(Exception e){ |
1014 |
+ |
unexpectedException(); |
1015 |
+ |
} |
1016 |
+ |
} |
1017 |
+ |
|
1018 |
+ |
/** |
1019 |
+ |
* drainTo(null, n) throws NPE |
1020 |
+ |
*/ |
1021 |
+ |
public void testDrainToNullN() { |
1022 |
+ |
LinkedBlockingQueue q = populatedQueue(SIZE); |
1023 |
+ |
try { |
1024 |
+ |
q.drainTo(null, 0); |
1025 |
+ |
shouldThrow(); |
1026 |
+ |
} catch(NullPointerException success) { |
1027 |
+ |
} |
1028 |
+ |
} |
1029 |
+ |
|
1030 |
+ |
/** |
1031 |
+ |
* drainTo(this, n) throws IAE |
1032 |
+ |
*/ |
1033 |
+ |
public void testDrainToSelfN() { |
1034 |
+ |
LinkedBlockingQueue q = populatedQueue(SIZE); |
1035 |
+ |
try { |
1036 |
+ |
q.drainTo(q, 0); |
1037 |
+ |
shouldThrow(); |
1038 |
+ |
} catch(IllegalArgumentException success) { |
1039 |
+ |
} |
1040 |
+ |
} |
1041 |
+ |
|
1042 |
+ |
/** |
1043 |
+ |
* drainTo(c, n) empties first max {n, size} elements of queue into c |
1044 |
+ |
*/ |
1045 |
+ |
public void testDrainToN() { |
1046 |
+ |
LinkedBlockingQueue q = new LinkedBlockingQueue(); |
1047 |
+ |
for (int i = 0; i < SIZE + 2; ++i) { |
1048 |
+ |
for(int j = 0; j < SIZE; j++) |
1049 |
+ |
assertTrue(q.offer(new Integer(j))); |
1050 |
+ |
ArrayList l = new ArrayList(); |
1051 |
+ |
q.drainTo(l, i); |
1052 |
+ |
int k = (i < SIZE)? i : SIZE; |
1053 |
+ |
assertEquals(l.size(), k); |
1054 |
+ |
assertEquals(q.size(), SIZE-k); |
1055 |
+ |
for (int j = 0; j < k; ++j) |
1056 |
+ |
assertEquals(l.get(j), new Integer(j)); |
1057 |
+ |
while (q.poll() != null) ; |
1058 |
+ |
} |
1059 |
+ |
} |
1060 |
+ |
|
1061 |
|
} |