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.*; |
10 |
|
import java.util.*; |
11 |
|
import java.util.concurrent.*; |
12 |
+ |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
13 |
|
import java.io.*; |
14 |
|
|
15 |
|
public class LinkedBlockingQueueTest extends JSR166TestCase { |
16 |
|
|
17 |
|
public static void main(String[] args) { |
18 |
< |
junit.textui.TestRunner.run (suite()); |
18 |
> |
junit.textui.TestRunner.run (suite()); |
19 |
|
} |
20 |
|
|
21 |
|
public static Test suite() { |
22 |
< |
return new TestSuite(LinkedBlockingQueueTest.class); |
22 |
> |
return new TestSuite(LinkedBlockingQueueTest.class); |
23 |
|
} |
24 |
|
|
25 |
|
|
30 |
|
private LinkedBlockingQueue populatedQueue(int n) { |
31 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(n); |
32 |
|
assertTrue(q.isEmpty()); |
33 |
< |
for(int i = 0; i < n; i++) |
34 |
< |
assertTrue(q.offer(new Integer(i))); |
33 |
> |
for (int i = 0; i < n; i++) |
34 |
> |
assertTrue(q.offer(new Integer(i))); |
35 |
|
assertFalse(q.isEmpty()); |
36 |
|
assertEquals(0, q.remainingCapacity()); |
37 |
< |
assertEquals(n, q.size()); |
37 |
> |
assertEquals(n, q.size()); |
38 |
|
return q; |
39 |
|
} |
40 |
< |
|
40 |
> |
|
41 |
|
/** |
42 |
|
* A new queue has the indicated capacity, or Integer.MAX_VALUE if |
43 |
|
* none given |
146 |
|
* offer(null) throws NPE |
147 |
|
*/ |
148 |
|
public void testOfferNull() { |
149 |
< |
try { |
149 |
> |
try { |
150 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
151 |
|
q.offer(null); |
152 |
|
shouldThrow(); |
153 |
< |
} catch (NullPointerException success) { } |
153 |
> |
} catch (NullPointerException success) { } |
154 |
|
} |
155 |
|
|
156 |
|
/** |
157 |
|
* add(null) throws NPE |
158 |
|
*/ |
159 |
|
public void testAddNull() { |
160 |
< |
try { |
160 |
> |
try { |
161 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
162 |
|
q.add(null); |
163 |
|
shouldThrow(); |
164 |
< |
} catch (NullPointerException success) { } |
164 |
> |
} catch (NullPointerException success) { } |
165 |
|
} |
166 |
|
|
167 |
|
/** |
177 |
|
* add succeeds if not full; throws ISE if full |
178 |
|
*/ |
179 |
|
public void testAdd() { |
180 |
< |
try { |
180 |
> |
try { |
181 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
182 |
|
for (int i = 0; i < SIZE; ++i) { |
183 |
|
assertTrue(q.add(new Integer(i))); |
184 |
|
} |
185 |
|
assertEquals(0, q.remainingCapacity()); |
186 |
|
q.add(new Integer(SIZE)); |
187 |
< |
} catch (IllegalStateException success){ |
188 |
< |
} |
187 |
> |
} catch (IllegalStateException success) { |
188 |
> |
} |
189 |
|
} |
190 |
|
|
191 |
|
/** |
275 |
|
* put(null) throws NPE |
276 |
|
*/ |
277 |
|
public void testPutNull() { |
278 |
< |
try { |
278 |
> |
try { |
279 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
280 |
|
q.put(null); |
281 |
|
shouldThrow(); |
282 |
< |
} |
283 |
< |
catch (NullPointerException success){ |
284 |
< |
} |
282 |
> |
} |
283 |
> |
catch (NullPointerException success) { |
284 |
> |
} |
285 |
|
catch (InterruptedException ie) { |
286 |
< |
unexpectedException(); |
286 |
> |
unexpectedException(); |
287 |
|
} |
288 |
|
} |
289 |
|
|
301 |
|
assertEquals(0, q.remainingCapacity()); |
302 |
|
} |
303 |
|
catch (InterruptedException ie) { |
304 |
< |
unexpectedException(); |
304 |
> |
unexpectedException(); |
305 |
|
} |
306 |
|
} |
307 |
|
|
320 |
|
} |
321 |
|
q.put(new Integer(SIZE)); |
322 |
|
threadShouldThrow(); |
323 |
< |
} catch (InterruptedException ie){ |
323 |
> |
} catch (InterruptedException ie) { |
324 |
|
threadAssertEquals(added, SIZE); |
325 |
< |
} |
325 |
> |
} |
326 |
|
}}); |
327 |
|
t.start(); |
328 |
< |
try { |
329 |
< |
Thread.sleep(SHORT_DELAY_MS); |
328 |
> |
try { |
329 |
> |
Thread.sleep(SHORT_DELAY_MS); |
330 |
|
t.interrupt(); |
331 |
|
t.join(); |
332 |
|
} |
333 |
|
catch (InterruptedException ie) { |
334 |
< |
unexpectedException(); |
334 |
> |
unexpectedException(); |
335 |
|
} |
336 |
|
} |
337 |
|
|
352 |
|
++added; |
353 |
|
q.put(new Object()); |
354 |
|
++added; |
355 |
< |
threadShouldThrow(); |
356 |
< |
} catch (InterruptedException e){ |
355 |
> |
threadShouldThrow(); |
356 |
> |
} catch (InterruptedException e) { |
357 |
|
threadAssertTrue(added >= 2); |
358 |
|
} |
359 |
|
} |
364 |
|
q.take(); |
365 |
|
t.interrupt(); |
366 |
|
t.join(); |
367 |
< |
} catch (Exception e){ |
367 |
> |
} catch (Exception e) { |
368 |
|
unexpectedException(); |
369 |
|
} |
370 |
|
} |
379 |
|
try { |
380 |
|
q.put(new Object()); |
381 |
|
q.put(new Object()); |
382 |
< |
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
383 |
< |
q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
384 |
< |
threadShouldThrow(); |
385 |
< |
} catch (InterruptedException success){} |
382 |
> |
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
383 |
> |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
384 |
> |
threadShouldThrow(); |
385 |
> |
} catch (InterruptedException success) {} |
386 |
|
} |
387 |
|
}); |
388 |
< |
|
388 |
> |
|
389 |
|
try { |
390 |
|
t.start(); |
391 |
|
Thread.sleep(SMALL_DELAY_MS); |
392 |
|
t.interrupt(); |
393 |
|
t.join(); |
394 |
< |
} catch (Exception e){ |
394 |
> |
} catch (Exception e) { |
395 |
|
unexpectedException(); |
396 |
|
} |
397 |
|
} |
400 |
|
* take retrieves elements in FIFO order |
401 |
|
*/ |
402 |
|
public void testTake() { |
403 |
< |
try { |
403 |
> |
try { |
404 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
405 |
|
for (int i = 0; i < SIZE; ++i) { |
406 |
|
assertEquals(i, ((Integer)q.take()).intValue()); |
407 |
|
} |
408 |
< |
} catch (InterruptedException e){ |
409 |
< |
unexpectedException(); |
410 |
< |
} |
408 |
> |
} catch (InterruptedException e) { |
409 |
> |
unexpectedException(); |
410 |
> |
} |
411 |
|
} |
412 |
|
|
413 |
|
/** |
419 |
|
public void run() { |
420 |
|
try { |
421 |
|
q.take(); |
422 |
< |
threadShouldThrow(); |
423 |
< |
} catch (InterruptedException success){ } |
422 |
> |
threadShouldThrow(); |
423 |
> |
} catch (InterruptedException success) { } |
424 |
|
} |
425 |
|
}); |
426 |
|
try { |
428 |
|
Thread.sleep(SHORT_DELAY_MS); |
429 |
|
t.interrupt(); |
430 |
|
t.join(); |
431 |
< |
} catch (Exception e){ |
431 |
> |
} catch (Exception e) { |
432 |
|
unexpectedException(); |
433 |
|
} |
434 |
|
} |
446 |
|
} |
447 |
|
q.take(); |
448 |
|
threadShouldThrow(); |
449 |
< |
} catch (InterruptedException success){ |
450 |
< |
} |
449 |
> |
} catch (InterruptedException success) { |
450 |
> |
} |
451 |
|
}}); |
452 |
|
t.start(); |
453 |
< |
try { |
454 |
< |
Thread.sleep(SHORT_DELAY_MS); |
453 |
> |
try { |
454 |
> |
Thread.sleep(SHORT_DELAY_MS); |
455 |
|
t.interrupt(); |
456 |
|
t.join(); |
457 |
|
} |
458 |
|
catch (InterruptedException ie) { |
459 |
< |
unexpectedException(); |
459 |
> |
unexpectedException(); |
460 |
|
} |
461 |
|
} |
462 |
|
|
469 |
|
for (int i = 0; i < SIZE; ++i) { |
470 |
|
assertEquals(i, ((Integer)q.poll()).intValue()); |
471 |
|
} |
472 |
< |
assertNull(q.poll()); |
472 |
> |
assertNull(q.poll()); |
473 |
|
} |
474 |
|
|
475 |
|
/** |
479 |
|
try { |
480 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
481 |
|
for (int i = 0; i < SIZE; ++i) { |
482 |
< |
assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue()); |
482 |
> |
assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue()); |
483 |
|
} |
484 |
< |
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
485 |
< |
} catch (InterruptedException e){ |
486 |
< |
unexpectedException(); |
487 |
< |
} |
484 |
> |
assertNull(q.poll(0, MILLISECONDS)); |
485 |
> |
} catch (InterruptedException e) { |
486 |
> |
unexpectedException(); |
487 |
> |
} |
488 |
|
} |
489 |
|
|
490 |
|
/** |
494 |
|
try { |
495 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
496 |
|
for (int i = 0; i < SIZE; ++i) { |
497 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
497 |
> |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
498 |
|
} |
499 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
500 |
< |
} catch (InterruptedException e){ |
501 |
< |
unexpectedException(); |
502 |
< |
} |
499 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
500 |
> |
} catch (InterruptedException e) { |
501 |
> |
unexpectedException(); |
502 |
> |
} |
503 |
|
} |
504 |
|
|
505 |
|
/** |
512 |
|
try { |
513 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
514 |
|
for (int i = 0; i < SIZE; ++i) { |
515 |
< |
threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
515 |
> |
threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
516 |
|
} |
517 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
518 |
< |
} catch (InterruptedException success){ |
519 |
< |
} |
517 |
> |
threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
518 |
> |
} catch (InterruptedException success) { |
519 |
> |
} |
520 |
|
}}); |
521 |
|
t.start(); |
522 |
< |
try { |
523 |
< |
Thread.sleep(SHORT_DELAY_MS); |
522 |
> |
try { |
523 |
> |
Thread.sleep(SHORT_DELAY_MS); |
524 |
|
t.interrupt(); |
525 |
|
t.join(); |
526 |
|
} |
527 |
|
catch (InterruptedException ie) { |
528 |
< |
unexpectedException(); |
528 |
> |
unexpectedException(); |
529 |
|
} |
530 |
|
} |
531 |
|
|
538 |
|
Thread t = new Thread(new Runnable() { |
539 |
|
public void run() { |
540 |
|
try { |
541 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
542 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
543 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
544 |
< |
threadShouldThrow(); |
545 |
< |
} catch (InterruptedException success) { } |
541 |
> |
threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
542 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
543 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
544 |
> |
threadShouldThrow(); |
545 |
> |
} catch (InterruptedException success) { } |
546 |
|
} |
547 |
|
}); |
548 |
|
try { |
549 |
|
t.start(); |
550 |
|
Thread.sleep(SMALL_DELAY_MS); |
551 |
< |
assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
551 |
> |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
552 |
|
t.interrupt(); |
553 |
|
t.join(); |
554 |
< |
} catch (Exception e){ |
554 |
> |
} catch (Exception e) { |
555 |
|
unexpectedException(); |
556 |
|
} |
557 |
< |
} |
557 |
> |
} |
558 |
|
|
559 |
|
/** |
560 |
|
* peek returns next element, or null if empty |
567 |
|
assertTrue(q.peek() == null || |
568 |
|
i != ((Integer)q.peek()).intValue()); |
569 |
|
} |
570 |
< |
assertNull(q.peek()); |
570 |
> |
assertNull(q.peek()); |
571 |
|
} |
572 |
|
|
573 |
|
/** |
597 |
|
try { |
598 |
|
q.remove(); |
599 |
|
shouldThrow(); |
600 |
< |
} catch (NoSuchElementException success){ |
601 |
< |
} |
600 |
> |
} catch (NoSuchElementException success) { |
601 |
> |
} |
602 |
|
} |
603 |
|
|
604 |
|
/** |
615 |
|
} |
616 |
|
assertTrue(q.isEmpty()); |
617 |
|
} |
618 |
< |
|
618 |
> |
|
619 |
> |
/** |
620 |
> |
* An add following remove(x) succeeds |
621 |
> |
*/ |
622 |
> |
public void testRemoveElementAndAdd() { |
623 |
> |
try { |
624 |
> |
LinkedBlockingQueue q = new LinkedBlockingQueue(); |
625 |
> |
assertTrue(q.add(new Integer(1))); |
626 |
> |
assertTrue(q.add(new Integer(2))); |
627 |
> |
assertTrue(q.remove(new Integer(1))); |
628 |
> |
assertTrue(q.remove(new Integer(2))); |
629 |
> |
assertTrue(q.add(new Integer(3))); |
630 |
> |
assertTrue(q.take() != null); |
631 |
> |
} catch (Exception e) { |
632 |
> |
unexpectedException(); |
633 |
> |
} |
634 |
> |
} |
635 |
> |
|
636 |
|
/** |
637 |
|
* contains(x) reports true when elements added but not yet removed |
638 |
|
*/ |
656 |
|
assertEquals(SIZE, q.remainingCapacity()); |
657 |
|
q.add(one); |
658 |
|
assertFalse(q.isEmpty()); |
659 |
+ |
assertTrue(q.contains(one)); |
660 |
|
q.clear(); |
661 |
|
assertTrue(q.isEmpty()); |
662 |
|
} |
715 |
|
*/ |
716 |
|
public void testToArray() { |
717 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
718 |
< |
Object[] o = q.toArray(); |
719 |
< |
try { |
720 |
< |
for(int i = 0; i < o.length; i++) |
721 |
< |
assertEquals(o[i], q.take()); |
722 |
< |
} catch (InterruptedException e){ |
723 |
< |
unexpectedException(); |
724 |
< |
} |
718 |
> |
Object[] o = q.toArray(); |
719 |
> |
try { |
720 |
> |
for (int i = 0; i < o.length; i++) |
721 |
> |
assertEquals(o[i], q.take()); |
722 |
> |
} catch (InterruptedException e) { |
723 |
> |
unexpectedException(); |
724 |
> |
} |
725 |
|
} |
726 |
|
|
727 |
|
/** |
729 |
|
*/ |
730 |
|
public void testToArray2() { |
731 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
732 |
< |
Integer[] ints = new Integer[SIZE]; |
733 |
< |
ints = (Integer[])q.toArray(ints); |
734 |
< |
try { |
735 |
< |
for(int i = 0; i < ints.length; i++) |
736 |
< |
assertEquals(ints[i], q.take()); |
737 |
< |
} catch (InterruptedException e){ |
738 |
< |
unexpectedException(); |
739 |
< |
} |
732 |
> |
Integer[] ints = new Integer[SIZE]; |
733 |
> |
ints = (Integer[])q.toArray(ints); |
734 |
> |
try { |
735 |
> |
for (int i = 0; i < ints.length; i++) |
736 |
> |
assertEquals(ints[i], q.take()); |
737 |
> |
} catch (InterruptedException e) { |
738 |
> |
unexpectedException(); |
739 |
> |
} |
740 |
|
} |
741 |
|
|
742 |
|
/** |
743 |
|
* toArray(null) throws NPE |
744 |
|
*/ |
745 |
|
public void testToArray_BadArg() { |
746 |
< |
try { |
746 |
> |
try { |
747 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
748 |
< |
Object o[] = q.toArray(null); |
749 |
< |
shouldThrow(); |
750 |
< |
} catch(NullPointerException success){} |
748 |
> |
Object o[] = q.toArray(null); |
749 |
> |
shouldThrow(); |
750 |
> |
} catch (NullPointerException success) {} |
751 |
|
} |
752 |
|
|
753 |
|
/** |
754 |
|
* toArray with incompatible array type throws CCE |
755 |
|
*/ |
756 |
|
public void testToArray1_BadArg() { |
757 |
< |
try { |
757 |
> |
try { |
758 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
759 |
< |
Object o[] = q.toArray(new String[10] ); |
760 |
< |
shouldThrow(); |
761 |
< |
} catch(ArrayStoreException success){} |
759 |
> |
Object o[] = q.toArray(new String[10] ); |
760 |
> |
shouldThrow(); |
761 |
> |
} catch (ArrayStoreException success) {} |
762 |
|
} |
763 |
|
|
764 |
< |
|
764 |
> |
|
765 |
|
/** |
766 |
|
* iterator iterates through all elements |
767 |
|
*/ |
768 |
|
public void testIterator() { |
769 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
770 |
< |
Iterator it = q.iterator(); |
771 |
< |
try { |
772 |
< |
while(it.hasNext()){ |
773 |
< |
assertEquals(it.next(), q.take()); |
774 |
< |
} |
775 |
< |
} catch (InterruptedException e){ |
776 |
< |
unexpectedException(); |
777 |
< |
} |
770 |
> |
Iterator it = q.iterator(); |
771 |
> |
try { |
772 |
> |
while (it.hasNext()) { |
773 |
> |
assertEquals(it.next(), q.take()); |
774 |
> |
} |
775 |
> |
} catch (InterruptedException e) { |
776 |
> |
unexpectedException(); |
777 |
> |
} |
778 |
|
} |
779 |
|
|
780 |
|
/** |
789 |
|
Iterator it = q.iterator(); |
790 |
|
it.next(); |
791 |
|
it.remove(); |
792 |
< |
|
792 |
> |
|
793 |
|
it = q.iterator(); |
794 |
|
assertEquals(it.next(), one); |
795 |
|
assertEquals(it.next(), three); |
844 |
|
for (int i = 0; i < SIZE; ++i) { |
845 |
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
846 |
|
} |
847 |
< |
} |
847 |
> |
} |
848 |
|
|
849 |
|
|
850 |
|
/** |
859 |
|
public void run() { |
860 |
|
threadAssertFalse(q.offer(three)); |
861 |
|
try { |
862 |
< |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
862 |
> |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
863 |
|
threadAssertEquals(0, q.remainingCapacity()); |
864 |
|
} |
865 |
|
catch (InterruptedException e) { |
879 |
|
} |
880 |
|
} |
881 |
|
}); |
882 |
< |
|
882 |
> |
|
883 |
|
joinPool(executor); |
884 |
|
} |
885 |
|
|
893 |
|
public void run() { |
894 |
|
threadAssertNull(q.poll()); |
895 |
|
try { |
896 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
896 |
> |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
897 |
|
threadAssertTrue(q.isEmpty()); |
898 |
|
} |
899 |
|
catch (InterruptedException e) { |
913 |
|
} |
914 |
|
} |
915 |
|
}); |
916 |
< |
|
916 |
> |
|
917 |
|
joinPool(executor); |
918 |
|
} |
919 |
|
|
933 |
|
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
934 |
|
LinkedBlockingQueue r = (LinkedBlockingQueue)in.readObject(); |
935 |
|
assertEquals(q.size(), r.size()); |
936 |
< |
while (!q.isEmpty()) |
936 |
> |
while (!q.isEmpty()) |
937 |
|
assertEquals(q.remove(), r.remove()); |
938 |
< |
} catch(Exception e){ |
938 |
> |
} catch (Exception e) { |
939 |
|
unexpectedException(); |
940 |
|
} |
941 |
|
} |
942 |
|
|
943 |
|
/** |
944 |
|
* drainTo(null) throws NPE |
945 |
< |
*/ |
945 |
> |
*/ |
946 |
|
public void testDrainToNull() { |
947 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
948 |
|
try { |
949 |
|
q.drainTo(null); |
950 |
|
shouldThrow(); |
951 |
< |
} catch(NullPointerException success) { |
951 |
> |
} catch (NullPointerException success) { |
952 |
|
} |
953 |
|
} |
954 |
|
|
955 |
|
/** |
956 |
|
* drainTo(this) throws IAE |
957 |
< |
*/ |
957 |
> |
*/ |
958 |
|
public void testDrainToSelf() { |
959 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
960 |
|
try { |
961 |
|
q.drainTo(q); |
962 |
|
shouldThrow(); |
963 |
< |
} catch(IllegalArgumentException success) { |
963 |
> |
} catch (IllegalArgumentException success) { |
964 |
|
} |
965 |
|
} |
966 |
|
|
967 |
|
/** |
968 |
|
* drainTo(c) empties queue into another collection c |
969 |
< |
*/ |
969 |
> |
*/ |
970 |
|
public void testDrainTo() { |
971 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
972 |
|
ArrayList l = new ArrayList(); |
973 |
|
q.drainTo(l); |
974 |
|
assertEquals(q.size(), 0); |
975 |
|
assertEquals(l.size(), SIZE); |
976 |
< |
for (int i = 0; i < SIZE; ++i) |
976 |
> |
for (int i = 0; i < SIZE; ++i) |
977 |
> |
assertEquals(l.get(i), new Integer(i)); |
978 |
> |
q.add(zero); |
979 |
> |
q.add(one); |
980 |
> |
assertFalse(q.isEmpty()); |
981 |
> |
assertTrue(q.contains(zero)); |
982 |
> |
assertTrue(q.contains(one)); |
983 |
> |
l.clear(); |
984 |
> |
q.drainTo(l); |
985 |
> |
assertEquals(q.size(), 0); |
986 |
> |
assertEquals(l.size(), 2); |
987 |
> |
for (int i = 0; i < 2; ++i) |
988 |
|
assertEquals(l.get(i), new Integer(i)); |
989 |
|
} |
990 |
|
|
991 |
|
/** |
992 |
|
* drainTo empties full queue, unblocking a waiting put. |
993 |
< |
*/ |
993 |
> |
*/ |
994 |
|
public void testDrainToWithActivePut() { |
995 |
|
final LinkedBlockingQueue q = populatedQueue(SIZE); |
996 |
|
Thread t = new Thread(new Runnable() { |
997 |
|
public void run() { |
998 |
|
try { |
999 |
|
q.put(new Integer(SIZE+1)); |
1000 |
< |
} catch (InterruptedException ie){ |
1000 |
> |
} catch (InterruptedException ie) { |
1001 |
|
threadUnexpectedException(); |
1002 |
|
} |
1003 |
|
} |
1007 |
|
ArrayList l = new ArrayList(); |
1008 |
|
q.drainTo(l); |
1009 |
|
assertTrue(l.size() >= SIZE); |
1010 |
< |
for (int i = 0; i < SIZE; ++i) |
1010 |
> |
for (int i = 0; i < SIZE; ++i) |
1011 |
|
assertEquals(l.get(i), new Integer(i)); |
1012 |
|
t.join(); |
1013 |
|
assertTrue(q.size() + l.size() >= SIZE); |
1014 |
< |
} catch(Exception e){ |
1014 |
> |
} catch (Exception e) { |
1015 |
|
unexpectedException(); |
1016 |
|
} |
1017 |
|
} |
1018 |
|
|
1019 |
|
/** |
1020 |
|
* drainTo(null, n) throws NPE |
1021 |
< |
*/ |
1021 |
> |
*/ |
1022 |
|
public void testDrainToNullN() { |
1023 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
1024 |
|
try { |
1025 |
|
q.drainTo(null, 0); |
1026 |
|
shouldThrow(); |
1027 |
< |
} catch(NullPointerException success) { |
1027 |
> |
} catch (NullPointerException success) { |
1028 |
|
} |
1029 |
|
} |
1030 |
|
|
1031 |
|
/** |
1032 |
|
* drainTo(this, n) throws IAE |
1033 |
< |
*/ |
1033 |
> |
*/ |
1034 |
|
public void testDrainToSelfN() { |
1035 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
1036 |
|
try { |
1037 |
|
q.drainTo(q, 0); |
1038 |
|
shouldThrow(); |
1039 |
< |
} catch(IllegalArgumentException success) { |
1039 |
> |
} catch (IllegalArgumentException success) { |
1040 |
|
} |
1041 |
|
} |
1042 |
|
|
1043 |
|
/** |
1044 |
|
* drainTo(c, n) empties first max {n, size} elements of queue into c |
1045 |
< |
*/ |
1045 |
> |
*/ |
1046 |
|
public void testDrainToN() { |
1047 |
+ |
LinkedBlockingQueue q = new LinkedBlockingQueue(); |
1048 |
|
for (int i = 0; i < SIZE + 2; ++i) { |
1049 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
1049 |
> |
for (int j = 0; j < SIZE; j++) |
1050 |
> |
assertTrue(q.offer(new Integer(j))); |
1051 |
|
ArrayList l = new ArrayList(); |
1052 |
|
q.drainTo(l, i); |
1053 |
|
int k = (i < SIZE)? i : SIZE; |
1022 |
– |
assertEquals(q.size(), SIZE-k); |
1054 |
|
assertEquals(l.size(), k); |
1055 |
< |
for (int j = 0; j < k; ++j) |
1055 |
> |
assertEquals(q.size(), SIZE-k); |
1056 |
> |
for (int j = 0; j < k; ++j) |
1057 |
|
assertEquals(l.get(j), new Integer(j)); |
1058 |
+ |
while (q.poll() != null) ; |
1059 |
|
} |
1060 |
|
} |
1061 |
|
|