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. |
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() { |
21 |
< |
return new TestSuite(LinkedBlockingQueueTest.class); |
21 |
> |
return new TestSuite(LinkedBlockingQueueTest.class); |
22 |
|
} |
23 |
|
|
24 |
|
|
29 |
|
private LinkedBlockingQueue populatedQueue(int n) { |
30 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(n); |
31 |
|
assertTrue(q.isEmpty()); |
32 |
< |
for(int i = 0; i < n; i++) |
33 |
< |
assertTrue(q.offer(new Integer(i))); |
32 |
> |
for (int i = 0; i < n; i++) |
33 |
> |
assertTrue(q.offer(new Integer(i))); |
34 |
|
assertFalse(q.isEmpty()); |
35 |
|
assertEquals(0, q.remainingCapacity()); |
36 |
< |
assertEquals(n, q.size()); |
36 |
> |
assertEquals(n, q.size()); |
37 |
|
return q; |
38 |
|
} |
39 |
< |
|
39 |
> |
|
40 |
|
/** |
41 |
|
* A new queue has the indicated capacity, or Integer.MAX_VALUE if |
42 |
|
* none given |
145 |
|
* offer(null) throws NPE |
146 |
|
*/ |
147 |
|
public void testOfferNull() { |
148 |
< |
try { |
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 { |
159 |
> |
try { |
160 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
161 |
|
q.add(null); |
162 |
|
shouldThrow(); |
163 |
< |
} catch (NullPointerException success) { } |
163 |
> |
} catch (NullPointerException success) { } |
164 |
|
} |
165 |
|
|
166 |
|
/** |
176 |
|
* add succeeds if not full; throws ISE if full |
177 |
|
*/ |
178 |
|
public void testAdd() { |
179 |
< |
try { |
179 |
> |
try { |
180 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
181 |
|
for (int i = 0; i < SIZE; ++i) { |
182 |
|
assertTrue(q.add(new Integer(i))); |
183 |
|
} |
184 |
|
assertEquals(0, q.remainingCapacity()); |
185 |
|
q.add(new Integer(SIZE)); |
186 |
< |
} catch (IllegalStateException success){ |
187 |
< |
} |
186 |
> |
} catch (IllegalStateException success) { |
187 |
> |
} |
188 |
|
} |
189 |
|
|
190 |
|
/** |
274 |
|
* put(null) throws NPE |
275 |
|
*/ |
276 |
|
public void testPutNull() { |
277 |
< |
try { |
277 |
> |
try { |
278 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
279 |
|
q.put(null); |
280 |
|
shouldThrow(); |
281 |
< |
} |
282 |
< |
catch (NullPointerException success){ |
283 |
< |
} |
281 |
> |
} |
282 |
> |
catch (NullPointerException success) { |
283 |
> |
} |
284 |
|
catch (InterruptedException ie) { |
285 |
< |
unexpectedException(); |
285 |
> |
unexpectedException(); |
286 |
|
} |
287 |
|
} |
288 |
|
|
300 |
|
assertEquals(0, q.remainingCapacity()); |
301 |
|
} |
302 |
|
catch (InterruptedException ie) { |
303 |
< |
unexpectedException(); |
303 |
> |
unexpectedException(); |
304 |
|
} |
305 |
|
} |
306 |
|
|
319 |
|
} |
320 |
|
q.put(new Integer(SIZE)); |
321 |
|
threadShouldThrow(); |
322 |
< |
} catch (InterruptedException ie){ |
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 |
|
} |
332 |
|
catch (InterruptedException ie) { |
333 |
< |
unexpectedException(); |
333 |
> |
unexpectedException(); |
334 |
|
} |
335 |
|
} |
336 |
|
|
351 |
|
++added; |
352 |
|
q.put(new Object()); |
353 |
|
++added; |
354 |
< |
threadShouldThrow(); |
355 |
< |
} catch (InterruptedException e){ |
354 |
> |
threadShouldThrow(); |
355 |
> |
} catch (InterruptedException e) { |
356 |
|
threadAssertTrue(added >= 2); |
357 |
|
} |
358 |
|
} |
363 |
|
q.take(); |
364 |
|
t.interrupt(); |
365 |
|
t.join(); |
366 |
< |
} catch (Exception e){ |
366 |
> |
} catch (Exception e) { |
367 |
|
unexpectedException(); |
368 |
|
} |
369 |
|
} |
380 |
|
q.put(new Object()); |
381 |
|
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
382 |
|
q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
383 |
< |
threadShouldThrow(); |
384 |
< |
} catch (InterruptedException success){} |
383 |
> |
threadShouldThrow(); |
384 |
> |
} catch (InterruptedException success) {} |
385 |
|
} |
386 |
|
}); |
387 |
< |
|
387 |
> |
|
388 |
|
try { |
389 |
|
t.start(); |
390 |
|
Thread.sleep(SMALL_DELAY_MS); |
391 |
|
t.interrupt(); |
392 |
|
t.join(); |
393 |
< |
} catch (Exception e){ |
393 |
> |
} catch (Exception e) { |
394 |
|
unexpectedException(); |
395 |
|
} |
396 |
|
} |
399 |
|
* take retrieves elements in FIFO order |
400 |
|
*/ |
401 |
|
public void testTake() { |
402 |
< |
try { |
402 |
> |
try { |
403 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
404 |
|
for (int i = 0; i < SIZE; ++i) { |
405 |
|
assertEquals(i, ((Integer)q.take()).intValue()); |
406 |
|
} |
407 |
< |
} catch (InterruptedException e){ |
408 |
< |
unexpectedException(); |
409 |
< |
} |
407 |
> |
} catch (InterruptedException e) { |
408 |
> |
unexpectedException(); |
409 |
> |
} |
410 |
|
} |
411 |
|
|
412 |
|
/** |
418 |
|
public void run() { |
419 |
|
try { |
420 |
|
q.take(); |
421 |
< |
threadShouldThrow(); |
422 |
< |
} catch (InterruptedException success){ } |
421 |
> |
threadShouldThrow(); |
422 |
> |
} catch (InterruptedException success) { } |
423 |
|
} |
424 |
|
}); |
425 |
|
try { |
427 |
|
Thread.sleep(SHORT_DELAY_MS); |
428 |
|
t.interrupt(); |
429 |
|
t.join(); |
430 |
< |
} catch (Exception e){ |
430 |
> |
} catch (Exception e) { |
431 |
|
unexpectedException(); |
432 |
|
} |
433 |
|
} |
445 |
|
} |
446 |
|
q.take(); |
447 |
|
threadShouldThrow(); |
448 |
< |
} catch (InterruptedException success){ |
449 |
< |
} |
448 |
> |
} catch (InterruptedException success) { |
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 |
|
} |
457 |
|
catch (InterruptedException ie) { |
458 |
< |
unexpectedException(); |
458 |
> |
unexpectedException(); |
459 |
|
} |
460 |
|
} |
461 |
|
|
468 |
|
for (int i = 0; i < SIZE; ++i) { |
469 |
|
assertEquals(i, ((Integer)q.poll()).intValue()); |
470 |
|
} |
471 |
< |
assertNull(q.poll()); |
471 |
> |
assertNull(q.poll()); |
472 |
|
} |
473 |
|
|
474 |
|
/** |
481 |
|
assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue()); |
482 |
|
} |
483 |
|
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
484 |
< |
} catch (InterruptedException e){ |
485 |
< |
unexpectedException(); |
486 |
< |
} |
484 |
> |
} catch (InterruptedException e) { |
485 |
> |
unexpectedException(); |
486 |
> |
} |
487 |
|
} |
488 |
|
|
489 |
|
/** |
496 |
|
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
497 |
|
} |
498 |
|
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
499 |
< |
} catch (InterruptedException e){ |
500 |
< |
unexpectedException(); |
501 |
< |
} |
499 |
> |
} catch (InterruptedException e) { |
500 |
> |
unexpectedException(); |
501 |
> |
} |
502 |
|
} |
503 |
|
|
504 |
|
/** |
514 |
|
threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
515 |
|
} |
516 |
|
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
517 |
< |
} catch (InterruptedException success){ |
518 |
< |
} |
517 |
> |
} catch (InterruptedException success) { |
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 |
|
} |
526 |
|
catch (InterruptedException ie) { |
527 |
< |
unexpectedException(); |
527 |
> |
unexpectedException(); |
528 |
|
} |
529 |
|
} |
530 |
|
|
540 |
|
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
541 |
|
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
542 |
|
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
543 |
< |
threadShouldThrow(); |
544 |
< |
} catch (InterruptedException success) { } |
543 |
> |
threadShouldThrow(); |
544 |
> |
} catch (InterruptedException success) { } |
545 |
|
} |
546 |
|
}); |
547 |
|
try { |
550 |
|
assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
551 |
|
t.interrupt(); |
552 |
|
t.join(); |
553 |
< |
} catch (Exception e){ |
553 |
> |
} catch (Exception e) { |
554 |
|
unexpectedException(); |
555 |
|
} |
556 |
< |
} |
556 |
> |
} |
557 |
|
|
558 |
|
/** |
559 |
|
* peek returns next element, or null if empty |
566 |
|
assertTrue(q.peek() == null || |
567 |
|
i != ((Integer)q.peek()).intValue()); |
568 |
|
} |
569 |
< |
assertNull(q.peek()); |
569 |
> |
assertNull(q.peek()); |
570 |
|
} |
571 |
|
|
572 |
|
/** |
596 |
|
try { |
597 |
|
q.remove(); |
598 |
|
shouldThrow(); |
599 |
< |
} catch (NoSuchElementException success){ |
600 |
< |
} |
599 |
> |
} catch (NoSuchElementException success) { |
600 |
> |
} |
601 |
|
} |
602 |
|
|
603 |
|
/** |
614 |
|
} |
615 |
|
assertTrue(q.isEmpty()); |
616 |
|
} |
617 |
< |
|
617 |
> |
|
618 |
> |
/** |
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 |
|
*/ |
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 |
|
} |
714 |
|
*/ |
715 |
|
public void testToArray() { |
716 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
717 |
< |
Object[] o = q.toArray(); |
718 |
< |
try { |
719 |
< |
for(int i = 0; i < o.length; i++) |
720 |
< |
assertEquals(o[i], q.take()); |
721 |
< |
} catch (InterruptedException e){ |
722 |
< |
unexpectedException(); |
723 |
< |
} |
717 |
> |
Object[] o = q.toArray(); |
718 |
> |
try { |
719 |
> |
for (int i = 0; i < o.length; i++) |
720 |
> |
assertEquals(o[i], q.take()); |
721 |
> |
} catch (InterruptedException e) { |
722 |
> |
unexpectedException(); |
723 |
> |
} |
724 |
|
} |
725 |
|
|
726 |
|
/** |
728 |
|
*/ |
729 |
|
public void testToArray2() { |
730 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
731 |
< |
Integer[] ints = new Integer[SIZE]; |
732 |
< |
ints = (Integer[])q.toArray(ints); |
733 |
< |
try { |
734 |
< |
for(int i = 0; i < ints.length; i++) |
735 |
< |
assertEquals(ints[i], q.take()); |
736 |
< |
} catch (InterruptedException e){ |
737 |
< |
unexpectedException(); |
738 |
< |
} |
731 |
> |
Integer[] ints = new Integer[SIZE]; |
732 |
> |
ints = (Integer[])q.toArray(ints); |
733 |
> |
try { |
734 |
> |
for (int i = 0; i < ints.length; i++) |
735 |
> |
assertEquals(ints[i], q.take()); |
736 |
> |
} catch (InterruptedException e) { |
737 |
> |
unexpectedException(); |
738 |
> |
} |
739 |
|
} |
740 |
|
|
741 |
|
/** |
742 |
|
* toArray(null) throws NPE |
743 |
|
*/ |
744 |
|
public void testToArray_BadArg() { |
745 |
< |
try { |
745 |
> |
try { |
746 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
747 |
< |
Object o[] = q.toArray(null); |
748 |
< |
shouldThrow(); |
749 |
< |
} catch(NullPointerException success){} |
747 |
> |
Object o[] = q.toArray(null); |
748 |
> |
shouldThrow(); |
749 |
> |
} catch (NullPointerException success) {} |
750 |
|
} |
751 |
|
|
752 |
|
/** |
753 |
< |
* toArray with incompatable array type throws CCE |
753 |
> |
* toArray with incompatible array type throws CCE |
754 |
|
*/ |
755 |
|
public void testToArray1_BadArg() { |
756 |
< |
try { |
756 |
> |
try { |
757 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
758 |
< |
Object o[] = q.toArray(new String[10] ); |
759 |
< |
shouldThrow(); |
760 |
< |
} catch(ArrayStoreException success){} |
758 |
> |
Object o[] = q.toArray(new String[10] ); |
759 |
> |
shouldThrow(); |
760 |
> |
} catch (ArrayStoreException success) {} |
761 |
|
} |
762 |
|
|
763 |
< |
|
763 |
> |
|
764 |
|
/** |
765 |
|
* iterator iterates through all elements |
766 |
|
*/ |
767 |
|
public void testIterator() { |
768 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
769 |
< |
Iterator it = q.iterator(); |
770 |
< |
try { |
771 |
< |
while(it.hasNext()){ |
772 |
< |
assertEquals(it.next(), q.take()); |
773 |
< |
} |
774 |
< |
} catch (InterruptedException e){ |
775 |
< |
unexpectedException(); |
776 |
< |
} |
769 |
> |
Iterator it = q.iterator(); |
770 |
> |
try { |
771 |
> |
while (it.hasNext()) { |
772 |
> |
assertEquals(it.next(), q.take()); |
773 |
> |
} |
774 |
> |
} catch (InterruptedException e) { |
775 |
> |
unexpectedException(); |
776 |
> |
} |
777 |
|
} |
778 |
|
|
779 |
|
/** |
788 |
|
Iterator it = q.iterator(); |
789 |
|
it.next(); |
790 |
|
it.remove(); |
791 |
< |
|
791 |
> |
|
792 |
|
it = q.iterator(); |
793 |
|
assertEquals(it.next(), one); |
794 |
|
assertEquals(it.next(), three); |
843 |
|
for (int i = 0; i < SIZE; ++i) { |
844 |
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
845 |
|
} |
846 |
< |
} |
846 |
> |
} |
847 |
|
|
848 |
|
|
849 |
|
/** |
878 |
|
} |
879 |
|
} |
880 |
|
}); |
881 |
< |
|
881 |
> |
|
882 |
|
joinPool(executor); |
883 |
|
} |
884 |
|
|
912 |
|
} |
913 |
|
} |
914 |
|
}); |
915 |
< |
|
915 |
> |
|
916 |
|
joinPool(executor); |
917 |
|
} |
918 |
|
|
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){ |
937 |
> |
} catch (Exception e) { |
938 |
|
unexpectedException(); |
939 |
|
} |
940 |
|
} |
941 |
|
|
942 |
|
/** |
943 |
|
* drainTo(null) throws NPE |
944 |
< |
*/ |
944 |
> |
*/ |
945 |
|
public void testDrainToNull() { |
946 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
947 |
|
try { |
948 |
|
q.drainTo(null); |
949 |
|
shouldThrow(); |
950 |
< |
} catch(NullPointerException success) { |
950 |
> |
} catch (NullPointerException success) { |
951 |
|
} |
952 |
|
} |
953 |
|
|
954 |
|
/** |
955 |
|
* drainTo(this) throws IAE |
956 |
< |
*/ |
956 |
> |
*/ |
957 |
|
public void testDrainToSelf() { |
958 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
959 |
|
try { |
960 |
|
q.drainTo(q); |
961 |
|
shouldThrow(); |
962 |
< |
} catch(IllegalArgumentException success) { |
962 |
> |
} catch (IllegalArgumentException success) { |
963 |
|
} |
964 |
|
} |
965 |
|
|
966 |
|
/** |
967 |
|
* drainTo(c) empties queue into another collection c |
968 |
< |
*/ |
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) |
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 |
< |
*/ |
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){ |
999 |
> |
} catch (InterruptedException ie) { |
1000 |
|
threadUnexpectedException(); |
1001 |
|
} |
1002 |
|
} |
1006 |
|
ArrayList l = new ArrayList(); |
1007 |
|
q.drainTo(l); |
1008 |
|
assertTrue(l.size() >= SIZE); |
1009 |
< |
for (int i = 0; i < SIZE; ++i) |
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+1); |
1013 |
< |
} catch(Exception e){ |
1012 |
> |
assertTrue(q.size() + l.size() >= SIZE); |
1013 |
> |
} catch (Exception e) { |
1014 |
|
unexpectedException(); |
1015 |
|
} |
1016 |
|
} |
1017 |
|
|
1018 |
|
/** |
1019 |
|
* drainTo(null, n) throws NPE |
1020 |
< |
*/ |
1020 |
> |
*/ |
1021 |
|
public void testDrainToNullN() { |
1022 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
1023 |
|
try { |
1024 |
|
q.drainTo(null, 0); |
1025 |
|
shouldThrow(); |
1026 |
< |
} catch(NullPointerException success) { |
1026 |
> |
} catch (NullPointerException success) { |
1027 |
|
} |
1028 |
|
} |
1029 |
|
|
1030 |
|
/** |
1031 |
|
* drainTo(this, n) throws IAE |
1032 |
< |
*/ |
1032 |
> |
*/ |
1033 |
|
public void testDrainToSelfN() { |
1034 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
1035 |
|
try { |
1036 |
|
q.drainTo(q, 0); |
1037 |
|
shouldThrow(); |
1038 |
< |
} catch(IllegalArgumentException success) { |
1038 |
> |
} catch (IllegalArgumentException success) { |
1039 |
|
} |
1040 |
|
} |
1041 |
|
|
1042 |
|
/** |
1043 |
|
* drainTo(c, n) empties first max {n, size} elements of queue into c |
1044 |
< |
*/ |
1044 |
> |
*/ |
1045 |
|
public void testDrainToN() { |
1046 |
+ |
LinkedBlockingQueue q = new LinkedBlockingQueue(); |
1047 |
|
for (int i = 0; i < SIZE + 2; ++i) { |
1048 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
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; |
1022 |
– |
assertEquals(q.size(), SIZE-k); |
1053 |
|
assertEquals(l.size(), k); |
1054 |
< |
for (int j = 0; j < k; ++j) |
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 |
|
|