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 |
|
|
10 |
|
import junit.framework.*; |
11 |
|
import java.util.*; |
12 |
|
import java.util.concurrent.*; |
13 |
+ |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
14 |
|
import java.io.*; |
15 |
|
|
16 |
|
public class ArrayBlockingQueueTest extends JSR166TestCase { |
17 |
|
public static void main(String[] args) { |
18 |
< |
junit.textui.TestRunner.run (suite()); |
18 |
> |
junit.textui.TestRunner.run (suite()); |
19 |
|
} |
20 |
|
public static Test suite() { |
21 |
< |
return new TestSuite(ArrayBlockingQueueTest.class); |
21 |
> |
return new TestSuite(ArrayBlockingQueueTest.class); |
22 |
|
} |
23 |
|
|
24 |
|
/** |
28 |
|
private ArrayBlockingQueue populatedQueue(int n) { |
29 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(n); |
30 |
|
assertTrue(q.isEmpty()); |
31 |
< |
for(int i = 0; i < n; i++) |
32 |
< |
assertTrue(q.offer(new Integer(i))); |
31 |
> |
for (int i = 0; i < n; i++) |
32 |
> |
assertTrue(q.offer(new Integer(i))); |
33 |
|
assertFalse(q.isEmpty()); |
34 |
|
assertEquals(0, q.remainingCapacity()); |
35 |
< |
assertEquals(n, q.size()); |
35 |
> |
assertEquals(n, q.size()); |
36 |
|
return q; |
37 |
|
} |
38 |
< |
|
38 |
> |
|
39 |
|
/** |
40 |
|
* A new queue has the indicated capacity |
41 |
|
*/ |
156 |
|
* offer(null) throws NPE |
157 |
|
*/ |
158 |
|
public void testOfferNull() { |
159 |
< |
try { |
159 |
> |
try { |
160 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(1); |
161 |
|
q.offer(null); |
162 |
|
shouldThrow(); |
163 |
< |
} catch (NullPointerException success) { } |
163 |
> |
} catch (NullPointerException success) { } |
164 |
> |
} |
165 |
> |
|
166 |
> |
/** |
167 |
> |
* add(null) throws NPE |
168 |
> |
*/ |
169 |
> |
public void testAddNull() { |
170 |
> |
try { |
171 |
> |
ArrayBlockingQueue q = new ArrayBlockingQueue(1); |
172 |
> |
q.add(null); |
173 |
> |
shouldThrow(); |
174 |
> |
} catch (NullPointerException success) { } |
175 |
|
} |
176 |
|
|
177 |
|
/** |
187 |
|
* add succeeds if not full; throws ISE if full |
188 |
|
*/ |
189 |
|
public void testAdd() { |
190 |
< |
try { |
190 |
> |
try { |
191 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
192 |
|
for (int i = 0; i < SIZE; ++i) { |
193 |
|
assertTrue(q.add(new Integer(i))); |
194 |
|
} |
195 |
|
assertEquals(0, q.remainingCapacity()); |
196 |
|
q.add(new Integer(SIZE)); |
197 |
< |
} catch (IllegalStateException success){ |
198 |
< |
} |
197 |
> |
} catch (IllegalStateException success) { |
198 |
> |
} |
199 |
|
} |
200 |
|
|
201 |
|
/** |
209 |
|
} |
210 |
|
catch (NullPointerException success) {} |
211 |
|
} |
212 |
+ |
|
213 |
+ |
/** |
214 |
+ |
* addAll(this) throws IAE |
215 |
+ |
*/ |
216 |
+ |
public void testAddAllSelf() { |
217 |
+ |
try { |
218 |
+ |
ArrayBlockingQueue q = populatedQueue(SIZE); |
219 |
+ |
q.addAll(q); |
220 |
+ |
shouldThrow(); |
221 |
+ |
} |
222 |
+ |
catch (IllegalArgumentException success) {} |
223 |
+ |
} |
224 |
+ |
|
225 |
+ |
|
226 |
|
/** |
227 |
|
* addAll of a collection with null elements throws NPE |
228 |
|
*/ |
286 |
|
* put(null) throws NPE |
287 |
|
*/ |
288 |
|
public void testPutNull() { |
289 |
< |
try { |
289 |
> |
try { |
290 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
291 |
|
q.put(null); |
292 |
|
shouldThrow(); |
293 |
< |
} |
294 |
< |
catch (NullPointerException success){ |
295 |
< |
} |
293 |
> |
} |
294 |
> |
catch (NullPointerException success) { |
295 |
> |
} |
296 |
|
catch (InterruptedException ie) { |
297 |
< |
unexpectedException(); |
297 |
> |
unexpectedException(); |
298 |
|
} |
299 |
|
} |
300 |
|
|
312 |
|
assertEquals(0, q.remainingCapacity()); |
313 |
|
} |
314 |
|
catch (InterruptedException ie) { |
315 |
< |
unexpectedException(); |
315 |
> |
unexpectedException(); |
316 |
|
} |
317 |
|
} |
318 |
|
|
320 |
|
* put blocks interruptibly if full |
321 |
|
*/ |
322 |
|
public void testBlockingPut() { |
323 |
+ |
final ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
324 |
|
Thread t = new Thread(new Runnable() { |
325 |
|
public void run() { |
326 |
|
int added = 0; |
327 |
|
try { |
300 |
– |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
328 |
|
for (int i = 0; i < SIZE; ++i) { |
329 |
|
q.put(new Integer(i)); |
330 |
|
++added; |
331 |
|
} |
332 |
|
q.put(new Integer(SIZE)); |
333 |
|
threadShouldThrow(); |
334 |
< |
} catch (InterruptedException ie){ |
334 |
> |
} catch (InterruptedException ie) { |
335 |
|
threadAssertEquals(added, SIZE); |
336 |
< |
} |
336 |
> |
} |
337 |
|
}}); |
338 |
< |
try { |
338 |
> |
try { |
339 |
|
t.start(); |
340 |
< |
Thread.sleep(SHORT_DELAY_MS); |
340 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
341 |
|
t.interrupt(); |
342 |
|
t.join(); |
343 |
|
} |
344 |
|
catch (InterruptedException ie) { |
345 |
< |
unexpectedException(); |
345 |
> |
unexpectedException(); |
346 |
|
} |
347 |
|
} |
348 |
|
|
363 |
|
++added; |
364 |
|
q.put(new Object()); |
365 |
|
++added; |
366 |
< |
threadShouldThrow(); |
367 |
< |
} catch (InterruptedException e){ |
366 |
> |
threadShouldThrow(); |
367 |
> |
} catch (InterruptedException e) { |
368 |
|
threadAssertTrue(added >= 2); |
369 |
|
} |
370 |
|
} |
375 |
|
q.take(); |
376 |
|
t.interrupt(); |
377 |
|
t.join(); |
378 |
< |
} catch (Exception e){ |
378 |
> |
} catch (Exception e) { |
379 |
|
unexpectedException(); |
380 |
|
} |
381 |
|
} |
390 |
|
try { |
391 |
|
q.put(new Object()); |
392 |
|
q.put(new Object()); |
393 |
< |
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS)); |
394 |
< |
q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
395 |
< |
threadShouldThrow(); |
396 |
< |
} catch (InterruptedException success){} |
393 |
> |
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, MILLISECONDS)); |
394 |
> |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
395 |
> |
threadShouldThrow(); |
396 |
> |
} catch (InterruptedException success) {} |
397 |
|
} |
398 |
|
}); |
399 |
< |
|
399 |
> |
|
400 |
|
try { |
401 |
|
t.start(); |
402 |
|
Thread.sleep(SHORT_DELAY_MS); |
403 |
|
t.interrupt(); |
404 |
|
t.join(); |
405 |
< |
} catch (Exception e){ |
405 |
> |
} catch (Exception e) { |
406 |
|
unexpectedException(); |
407 |
|
} |
408 |
|
} |
411 |
|
* take retrieves elements in FIFO order |
412 |
|
*/ |
413 |
|
public void testTake() { |
414 |
< |
try { |
414 |
> |
try { |
415 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
416 |
|
for (int i = 0; i < SIZE; ++i) { |
417 |
|
assertEquals(i, ((Integer)q.take()).intValue()); |
418 |
|
} |
419 |
< |
} catch (InterruptedException e){ |
420 |
< |
unexpectedException(); |
421 |
< |
} |
419 |
> |
} catch (InterruptedException e) { |
420 |
> |
unexpectedException(); |
421 |
> |
} |
422 |
|
} |
423 |
|
|
424 |
|
/** |
430 |
|
public void run() { |
431 |
|
try { |
432 |
|
q.take(); |
433 |
< |
threadShouldThrow(); |
434 |
< |
} catch (InterruptedException success){ } |
433 |
> |
threadShouldThrow(); |
434 |
> |
} catch (InterruptedException success) { } |
435 |
|
} |
436 |
|
}); |
437 |
|
try { |
439 |
|
Thread.sleep(SHORT_DELAY_MS); |
440 |
|
t.interrupt(); |
441 |
|
t.join(); |
442 |
< |
} catch (Exception e){ |
442 |
> |
} catch (Exception e) { |
443 |
|
unexpectedException(); |
444 |
|
} |
445 |
|
} |
457 |
|
} |
458 |
|
q.take(); |
459 |
|
threadShouldThrow(); |
460 |
< |
} catch (InterruptedException success){ |
461 |
< |
} |
460 |
> |
} catch (InterruptedException success) { |
461 |
> |
} |
462 |
|
}}); |
463 |
< |
try { |
463 |
> |
try { |
464 |
|
t.start(); |
465 |
< |
Thread.sleep(SHORT_DELAY_MS); |
465 |
> |
Thread.sleep(SHORT_DELAY_MS); |
466 |
|
t.interrupt(); |
467 |
|
t.join(); |
468 |
|
} |
469 |
|
catch (InterruptedException ie) { |
470 |
< |
unexpectedException(); |
470 |
> |
unexpectedException(); |
471 |
|
} |
472 |
|
} |
473 |
|
|
480 |
|
for (int i = 0; i < SIZE; ++i) { |
481 |
|
assertEquals(i, ((Integer)q.poll()).intValue()); |
482 |
|
} |
483 |
< |
assertNull(q.poll()); |
483 |
> |
assertNull(q.poll()); |
484 |
|
} |
485 |
|
|
486 |
|
/** |
490 |
|
try { |
491 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
492 |
|
for (int i = 0; i < SIZE; ++i) { |
493 |
< |
assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue()); |
493 |
> |
assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue()); |
494 |
|
} |
495 |
< |
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
496 |
< |
} catch (InterruptedException e){ |
497 |
< |
unexpectedException(); |
498 |
< |
} |
495 |
> |
assertNull(q.poll(0, MILLISECONDS)); |
496 |
> |
} catch (InterruptedException e) { |
497 |
> |
unexpectedException(); |
498 |
> |
} |
499 |
|
} |
500 |
|
|
501 |
|
/** |
505 |
|
try { |
506 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
507 |
|
for (int i = 0; i < SIZE; ++i) { |
508 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
508 |
> |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
509 |
|
} |
510 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
511 |
< |
} catch (InterruptedException e){ |
512 |
< |
unexpectedException(); |
513 |
< |
} |
510 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
511 |
> |
} catch (InterruptedException e) { |
512 |
> |
unexpectedException(); |
513 |
> |
} |
514 |
|
} |
515 |
|
|
516 |
|
/** |
523 |
|
try { |
524 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
525 |
|
for (int i = 0; i < SIZE; ++i) { |
526 |
< |
threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
526 |
> |
threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
527 |
|
} |
528 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
529 |
< |
} catch (InterruptedException success){ |
530 |
< |
} |
528 |
> |
threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
529 |
> |
} catch (InterruptedException success) { |
530 |
> |
} |
531 |
|
}}); |
532 |
< |
try { |
532 |
> |
try { |
533 |
|
t.start(); |
534 |
< |
Thread.sleep(SHORT_DELAY_MS); |
534 |
> |
Thread.sleep(SHORT_DELAY_MS); |
535 |
|
t.interrupt(); |
536 |
|
t.join(); |
537 |
|
} |
538 |
|
catch (InterruptedException ie) { |
539 |
< |
unexpectedException(); |
539 |
> |
unexpectedException(); |
540 |
|
} |
541 |
|
} |
542 |
|
|
549 |
|
Thread t = new Thread(new Runnable() { |
550 |
|
public void run() { |
551 |
|
try { |
552 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
553 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
554 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
555 |
< |
threadShouldThrow(); |
556 |
< |
} catch (InterruptedException success) { } |
552 |
> |
threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
553 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
554 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
555 |
> |
threadShouldThrow(); |
556 |
> |
} catch (InterruptedException success) { } |
557 |
|
} |
558 |
|
}); |
559 |
|
try { |
560 |
|
t.start(); |
561 |
|
Thread.sleep(SMALL_DELAY_MS); |
562 |
< |
assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
562 |
> |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
563 |
|
t.interrupt(); |
564 |
|
t.join(); |
565 |
< |
} catch (Exception e){ |
565 |
> |
} catch (Exception e) { |
566 |
|
unexpectedException(); |
567 |
|
} |
568 |
< |
} |
568 |
> |
} |
569 |
|
|
570 |
|
|
571 |
|
/** |
579 |
|
assertTrue(q.peek() == null || |
580 |
|
i != ((Integer)q.peek()).intValue()); |
581 |
|
} |
582 |
< |
assertNull(q.peek()); |
582 |
> |
assertNull(q.peek()); |
583 |
|
} |
584 |
|
|
585 |
|
/** |
609 |
|
try { |
610 |
|
q.remove(); |
611 |
|
shouldThrow(); |
612 |
< |
} catch (NoSuchElementException success){ |
613 |
< |
} |
612 |
> |
} catch (NoSuchElementException success) { |
613 |
> |
} |
614 |
|
} |
615 |
|
|
616 |
|
/** |
627 |
|
} |
628 |
|
assertTrue(q.isEmpty()); |
629 |
|
} |
630 |
< |
|
630 |
> |
|
631 |
|
/** |
632 |
|
* contains(x) reports true when elements added but not yet removed |
633 |
|
*/ |
651 |
|
assertEquals(SIZE, q.remainingCapacity()); |
652 |
|
q.add(one); |
653 |
|
assertFalse(q.isEmpty()); |
654 |
+ |
assertTrue(q.contains(one)); |
655 |
|
q.clear(); |
656 |
|
assertTrue(q.isEmpty()); |
657 |
|
} |
710 |
|
*/ |
711 |
|
public void testToArray() { |
712 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
713 |
< |
Object[] o = q.toArray(); |
714 |
< |
try { |
715 |
< |
for(int i = 0; i < o.length; i++) |
716 |
< |
assertEquals(o[i], q.take()); |
717 |
< |
} catch (InterruptedException e){ |
718 |
< |
unexpectedException(); |
719 |
< |
} |
713 |
> |
Object[] o = q.toArray(); |
714 |
> |
try { |
715 |
> |
for (int i = 0; i < o.length; i++) |
716 |
> |
assertEquals(o[i], q.take()); |
717 |
> |
} catch (InterruptedException e) { |
718 |
> |
unexpectedException(); |
719 |
> |
} |
720 |
|
} |
721 |
|
|
722 |
|
/** |
724 |
|
*/ |
725 |
|
public void testToArray2() { |
726 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
727 |
< |
Integer[] ints = new Integer[SIZE]; |
728 |
< |
ints = (Integer[])q.toArray(ints); |
729 |
< |
try { |
730 |
< |
for(int i = 0; i < ints.length; i++) |
731 |
< |
assertEquals(ints[i], q.take()); |
732 |
< |
} catch (InterruptedException e){ |
733 |
< |
unexpectedException(); |
734 |
< |
} |
727 |
> |
Integer[] ints = new Integer[SIZE]; |
728 |
> |
ints = (Integer[])q.toArray(ints); |
729 |
> |
try { |
730 |
> |
for (int i = 0; i < ints.length; i++) |
731 |
> |
assertEquals(ints[i], q.take()); |
732 |
> |
} catch (InterruptedException e) { |
733 |
> |
unexpectedException(); |
734 |
> |
} |
735 |
> |
} |
736 |
> |
|
737 |
> |
/** |
738 |
> |
* toArray(null) throws NPE |
739 |
> |
*/ |
740 |
> |
public void testToArray_BadArg() { |
741 |
> |
try { |
742 |
> |
ArrayBlockingQueue q = populatedQueue(SIZE); |
743 |
> |
Object o[] = q.toArray(null); |
744 |
> |
shouldThrow(); |
745 |
> |
} catch (NullPointerException success) {} |
746 |
> |
} |
747 |
> |
|
748 |
> |
/** |
749 |
> |
* toArray with incompatible array type throws CCE |
750 |
> |
*/ |
751 |
> |
public void testToArray1_BadArg() { |
752 |
> |
try { |
753 |
> |
ArrayBlockingQueue q = populatedQueue(SIZE); |
754 |
> |
Object o[] = q.toArray(new String[10] ); |
755 |
> |
shouldThrow(); |
756 |
> |
} catch (ArrayStoreException success) {} |
757 |
|
} |
758 |
< |
|
758 |
> |
|
759 |
> |
|
760 |
|
/** |
761 |
|
* iterator iterates through all elements |
762 |
|
*/ |
763 |
|
public void testIterator() { |
764 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
765 |
< |
Iterator it = q.iterator(); |
766 |
< |
try { |
767 |
< |
while(it.hasNext()){ |
768 |
< |
assertEquals(it.next(), q.take()); |
769 |
< |
} |
770 |
< |
} catch (InterruptedException e){ |
771 |
< |
unexpectedException(); |
772 |
< |
} |
765 |
> |
Iterator it = q.iterator(); |
766 |
> |
try { |
767 |
> |
while (it.hasNext()) { |
768 |
> |
assertEquals(it.next(), q.take()); |
769 |
> |
} |
770 |
> |
} catch (InterruptedException e) { |
771 |
> |
unexpectedException(); |
772 |
> |
} |
773 |
|
} |
774 |
|
|
775 |
|
/** |
784 |
|
Iterator it = q.iterator(); |
785 |
|
it.next(); |
786 |
|
it.remove(); |
787 |
< |
|
787 |
> |
|
788 |
|
it = q.iterator(); |
789 |
|
assertEquals(it.next(), one); |
790 |
|
assertEquals(it.next(), three); |
840 |
|
for (int i = 0; i < SIZE; ++i) { |
841 |
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
842 |
|
} |
843 |
< |
} |
843 |
> |
} |
844 |
|
|
845 |
|
|
846 |
|
/** |
855 |
|
public void run() { |
856 |
|
threadAssertFalse(q.offer(three)); |
857 |
|
try { |
858 |
< |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
858 |
> |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
859 |
|
threadAssertEquals(0, q.remainingCapacity()); |
860 |
|
} |
861 |
|
catch (InterruptedException e) { |
875 |
|
} |
876 |
|
} |
877 |
|
}); |
878 |
< |
|
878 |
> |
|
879 |
|
joinPool(executor); |
880 |
|
|
881 |
|
} |
890 |
|
public void run() { |
891 |
|
threadAssertNull(q.poll()); |
892 |
|
try { |
893 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
893 |
> |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
894 |
|
threadAssertTrue(q.isEmpty()); |
895 |
|
} |
896 |
|
catch (InterruptedException e) { |
910 |
|
} |
911 |
|
} |
912 |
|
}); |
913 |
< |
|
913 |
> |
|
914 |
|
joinPool(executor); |
915 |
|
} |
916 |
|
|
930 |
|
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
931 |
|
ArrayBlockingQueue r = (ArrayBlockingQueue)in.readObject(); |
932 |
|
assertEquals(q.size(), r.size()); |
933 |
< |
while (!q.isEmpty()) |
933 |
> |
while (!q.isEmpty()) |
934 |
|
assertEquals(q.remove(), r.remove()); |
935 |
< |
} catch(Exception e){ |
935 |
> |
} catch (Exception e) { |
936 |
> |
unexpectedException(); |
937 |
> |
} |
938 |
> |
} |
939 |
> |
|
940 |
> |
/** |
941 |
> |
* drainTo(null) throws NPE |
942 |
> |
*/ |
943 |
> |
public void testDrainToNull() { |
944 |
> |
ArrayBlockingQueue q = populatedQueue(SIZE); |
945 |
> |
try { |
946 |
> |
q.drainTo(null); |
947 |
> |
shouldThrow(); |
948 |
> |
} catch (NullPointerException success) { |
949 |
> |
} |
950 |
> |
} |
951 |
> |
|
952 |
> |
/** |
953 |
> |
* drainTo(this) throws IAE |
954 |
> |
*/ |
955 |
> |
public void testDrainToSelf() { |
956 |
> |
ArrayBlockingQueue q = populatedQueue(SIZE); |
957 |
> |
try { |
958 |
> |
q.drainTo(q); |
959 |
> |
shouldThrow(); |
960 |
> |
} catch (IllegalArgumentException success) { |
961 |
> |
} |
962 |
> |
} |
963 |
> |
|
964 |
> |
/** |
965 |
> |
* drainTo(c) empties queue into another collection c |
966 |
> |
*/ |
967 |
> |
public void testDrainTo() { |
968 |
> |
ArrayBlockingQueue q = populatedQueue(SIZE); |
969 |
> |
ArrayList l = new ArrayList(); |
970 |
> |
q.drainTo(l); |
971 |
> |
assertEquals(q.size(), 0); |
972 |
> |
assertEquals(l.size(), SIZE); |
973 |
> |
for (int i = 0; i < SIZE; ++i) |
974 |
> |
assertEquals(l.get(i), new Integer(i)); |
975 |
> |
q.add(zero); |
976 |
> |
q.add(one); |
977 |
> |
assertFalse(q.isEmpty()); |
978 |
> |
assertTrue(q.contains(zero)); |
979 |
> |
assertTrue(q.contains(one)); |
980 |
> |
l.clear(); |
981 |
> |
q.drainTo(l); |
982 |
> |
assertEquals(q.size(), 0); |
983 |
> |
assertEquals(l.size(), 2); |
984 |
> |
for (int i = 0; i < 2; ++i) |
985 |
> |
assertEquals(l.get(i), new Integer(i)); |
986 |
> |
} |
987 |
> |
|
988 |
> |
/** |
989 |
> |
* drainTo empties full queue, unblocking a waiting put. |
990 |
> |
*/ |
991 |
> |
public void testDrainToWithActivePut() { |
992 |
> |
final ArrayBlockingQueue q = populatedQueue(SIZE); |
993 |
> |
Thread t = new Thread(new Runnable() { |
994 |
> |
public void run() { |
995 |
> |
try { |
996 |
> |
q.put(new Integer(SIZE+1)); |
997 |
> |
} catch (InterruptedException ie) { |
998 |
> |
threadUnexpectedException(); |
999 |
> |
} |
1000 |
> |
} |
1001 |
> |
}); |
1002 |
> |
try { |
1003 |
> |
t.start(); |
1004 |
> |
ArrayList l = new ArrayList(); |
1005 |
> |
q.drainTo(l); |
1006 |
> |
assertTrue(l.size() >= SIZE); |
1007 |
> |
for (int i = 0; i < SIZE; ++i) |
1008 |
> |
assertEquals(l.get(i), new Integer(i)); |
1009 |
> |
t.join(); |
1010 |
> |
assertTrue(q.size() + l.size() >= SIZE); |
1011 |
> |
} catch (Exception e) { |
1012 |
|
unexpectedException(); |
1013 |
|
} |
1014 |
|
} |
1015 |
|
|
1016 |
+ |
/** |
1017 |
+ |
* drainTo(null, n) throws NPE |
1018 |
+ |
*/ |
1019 |
+ |
public void testDrainToNullN() { |
1020 |
+ |
ArrayBlockingQueue q = populatedQueue(SIZE); |
1021 |
+ |
try { |
1022 |
+ |
q.drainTo(null, 0); |
1023 |
+ |
shouldThrow(); |
1024 |
+ |
} catch (NullPointerException success) { |
1025 |
+ |
} |
1026 |
+ |
} |
1027 |
+ |
|
1028 |
+ |
/** |
1029 |
+ |
* drainTo(this, n) throws IAE |
1030 |
+ |
*/ |
1031 |
+ |
public void testDrainToSelfN() { |
1032 |
+ |
ArrayBlockingQueue q = populatedQueue(SIZE); |
1033 |
+ |
try { |
1034 |
+ |
q.drainTo(q, 0); |
1035 |
+ |
shouldThrow(); |
1036 |
+ |
} catch (IllegalArgumentException success) { |
1037 |
+ |
} |
1038 |
+ |
} |
1039 |
+ |
|
1040 |
+ |
/** |
1041 |
+ |
* drainTo(c, n) empties first max {n, size} elements of queue into c |
1042 |
+ |
*/ |
1043 |
+ |
public void testDrainToN() { |
1044 |
+ |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE*2); |
1045 |
+ |
for (int i = 0; i < SIZE + 2; ++i) { |
1046 |
+ |
for (int j = 0; j < SIZE; j++) |
1047 |
+ |
assertTrue(q.offer(new Integer(j))); |
1048 |
+ |
ArrayList l = new ArrayList(); |
1049 |
+ |
q.drainTo(l, i); |
1050 |
+ |
int k = (i < SIZE)? i : SIZE; |
1051 |
+ |
assertEquals(l.size(), k); |
1052 |
+ |
assertEquals(q.size(), SIZE-k); |
1053 |
+ |
for (int j = 0; j < k; ++j) |
1054 |
+ |
assertEquals(l.get(j), new Integer(j)); |
1055 |
+ |
while (q.poll() != null) ; |
1056 |
+ |
} |
1057 |
+ |
} |
1058 |
+ |
|
1059 |
|
|
1060 |
|
} |