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 |
|
|
14 |
|
|
15 |
|
public class ArrayBlockingQueueTest extends JSR166TestCase { |
16 |
|
public static void main(String[] args) { |
17 |
< |
junit.textui.TestRunner.run (suite()); |
17 |
> |
junit.textui.TestRunner.run (suite()); |
18 |
|
} |
19 |
|
public static Test suite() { |
20 |
|
return new TestSuite(ArrayBlockingQueueTest.class); |
34 |
|
assertEquals(n, q.size()); |
35 |
|
return q; |
36 |
|
} |
37 |
< |
|
37 |
> |
|
38 |
|
/** |
39 |
|
* A new queue has the indicated capacity |
40 |
|
*/ |
159 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(1); |
160 |
|
q.offer(null); |
161 |
|
shouldThrow(); |
162 |
< |
} catch (NullPointerException success) { } |
162 |
> |
} catch (NullPointerException success) { } |
163 |
|
} |
164 |
|
|
165 |
|
/** |
170 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(1); |
171 |
|
q.add(null); |
172 |
|
shouldThrow(); |
173 |
< |
} catch (NullPointerException success) { } |
173 |
> |
} catch (NullPointerException success) { } |
174 |
|
} |
175 |
|
|
176 |
|
/** |
194 |
|
assertEquals(0, q.remainingCapacity()); |
195 |
|
q.add(new Integer(SIZE)); |
196 |
|
} catch (IllegalStateException success){ |
197 |
< |
} |
197 |
> |
} |
198 |
|
} |
199 |
|
|
200 |
|
/** |
289 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
290 |
|
q.put(null); |
291 |
|
shouldThrow(); |
292 |
< |
} |
292 |
> |
} |
293 |
|
catch (NullPointerException success){ |
294 |
< |
} |
294 |
> |
} |
295 |
|
catch (InterruptedException ie) { |
296 |
|
unexpectedException(); |
297 |
|
} |
332 |
|
threadShouldThrow(); |
333 |
|
} catch (InterruptedException ie){ |
334 |
|
threadAssertEquals(added, SIZE); |
335 |
< |
} |
335 |
> |
} |
336 |
|
}}); |
337 |
< |
try { |
337 |
> |
try { |
338 |
|
t.start(); |
339 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
339 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
340 |
|
t.interrupt(); |
341 |
|
t.join(); |
342 |
|
} |
395 |
|
} catch (InterruptedException success){} |
396 |
|
} |
397 |
|
}); |
398 |
< |
|
398 |
> |
|
399 |
|
try { |
400 |
|
t.start(); |
401 |
|
Thread.sleep(SHORT_DELAY_MS); |
417 |
|
} |
418 |
|
} catch (InterruptedException e){ |
419 |
|
unexpectedException(); |
420 |
< |
} |
420 |
> |
} |
421 |
|
} |
422 |
|
|
423 |
|
/** |
430 |
|
try { |
431 |
|
q.take(); |
432 |
|
threadShouldThrow(); |
433 |
< |
} catch (InterruptedException success){ } |
433 |
> |
} catch (InterruptedException success){ } |
434 |
|
} |
435 |
|
}); |
436 |
|
try { |
457 |
|
q.take(); |
458 |
|
threadShouldThrow(); |
459 |
|
} catch (InterruptedException success){ |
460 |
< |
} |
460 |
> |
} |
461 |
|
}}); |
462 |
< |
try { |
462 |
> |
try { |
463 |
|
t.start(); |
464 |
< |
Thread.sleep(SHORT_DELAY_MS); |
464 |
> |
Thread.sleep(SHORT_DELAY_MS); |
465 |
|
t.interrupt(); |
466 |
|
t.join(); |
467 |
|
} |
494 |
|
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
495 |
|
} catch (InterruptedException e){ |
496 |
|
unexpectedException(); |
497 |
< |
} |
497 |
> |
} |
498 |
|
} |
499 |
|
|
500 |
|
/** |
509 |
|
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
510 |
|
} catch (InterruptedException e){ |
511 |
|
unexpectedException(); |
512 |
< |
} |
512 |
> |
} |
513 |
|
} |
514 |
|
|
515 |
|
/** |
526 |
|
} |
527 |
|
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
528 |
|
} catch (InterruptedException success){ |
529 |
< |
} |
529 |
> |
} |
530 |
|
}}); |
531 |
< |
try { |
531 |
> |
try { |
532 |
|
t.start(); |
533 |
< |
Thread.sleep(SHORT_DELAY_MS); |
533 |
> |
Thread.sleep(SHORT_DELAY_MS); |
534 |
|
t.interrupt(); |
535 |
|
t.join(); |
536 |
|
} |
552 |
|
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
553 |
|
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
554 |
|
threadShouldThrow(); |
555 |
< |
} catch (InterruptedException success) { } |
555 |
> |
} catch (InterruptedException success) { } |
556 |
|
} |
557 |
|
}); |
558 |
|
try { |
564 |
|
} catch (Exception e){ |
565 |
|
unexpectedException(); |
566 |
|
} |
567 |
< |
} |
567 |
> |
} |
568 |
|
|
569 |
|
|
570 |
|
/** |
609 |
|
q.remove(); |
610 |
|
shouldThrow(); |
611 |
|
} catch (NoSuchElementException success){ |
612 |
< |
} |
612 |
> |
} |
613 |
|
} |
614 |
|
|
615 |
|
/** |
626 |
|
} |
627 |
|
assertTrue(q.isEmpty()); |
628 |
|
} |
629 |
< |
|
629 |
> |
|
630 |
|
/** |
631 |
|
* contains(x) reports true when elements added but not yet removed |
632 |
|
*/ |
715 |
|
assertEquals(o[i], q.take()); |
716 |
|
} catch (InterruptedException e){ |
717 |
|
unexpectedException(); |
718 |
< |
} |
718 |
> |
} |
719 |
|
} |
720 |
|
|
721 |
|
/** |
730 |
|
assertEquals(ints[i], q.take()); |
731 |
|
} catch (InterruptedException e){ |
732 |
|
unexpectedException(); |
733 |
< |
} |
733 |
> |
} |
734 |
|
} |
735 |
|
|
736 |
|
/** |
755 |
|
} catch(ArrayStoreException success){} |
756 |
|
} |
757 |
|
|
758 |
< |
|
758 |
> |
|
759 |
|
/** |
760 |
|
* iterator iterates through all elements |
761 |
|
*/ |
768 |
|
} |
769 |
|
} catch (InterruptedException e){ |
770 |
|
unexpectedException(); |
771 |
< |
} |
771 |
> |
} |
772 |
|
} |
773 |
|
|
774 |
|
/** |
783 |
|
Iterator it = q.iterator(); |
784 |
|
it.next(); |
785 |
|
it.remove(); |
786 |
< |
|
786 |
> |
|
787 |
|
it = q.iterator(); |
788 |
|
assertEquals(it.next(), one); |
789 |
|
assertEquals(it.next(), three); |
839 |
|
for (int i = 0; i < SIZE; ++i) { |
840 |
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
841 |
|
} |
842 |
< |
} |
842 |
> |
} |
843 |
|
|
844 |
|
|
845 |
|
/** |
874 |
|
} |
875 |
|
} |
876 |
|
}); |
877 |
< |
|
877 |
> |
|
878 |
|
joinPool(executor); |
879 |
|
|
880 |
|
} |
909 |
|
} |
910 |
|
} |
911 |
|
}); |
912 |
< |
|
912 |
> |
|
913 |
|
joinPool(executor); |
914 |
|
} |
915 |
|
|
929 |
|
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
930 |
|
ArrayBlockingQueue r = (ArrayBlockingQueue)in.readObject(); |
931 |
|
assertEquals(q.size(), r.size()); |
932 |
< |
while (!q.isEmpty()) |
932 |
> |
while (!q.isEmpty()) |
933 |
|
assertEquals(q.remove(), r.remove()); |
934 |
|
} catch(Exception e){ |
935 |
|
unexpectedException(); |
938 |
|
|
939 |
|
/** |
940 |
|
* drainTo(null) throws NPE |
941 |
< |
*/ |
941 |
> |
*/ |
942 |
|
public void testDrainToNull() { |
943 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
944 |
|
try { |
950 |
|
|
951 |
|
/** |
952 |
|
* drainTo(this) throws IAE |
953 |
< |
*/ |
953 |
> |
*/ |
954 |
|
public void testDrainToSelf() { |
955 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
956 |
|
try { |
962 |
|
|
963 |
|
/** |
964 |
|
* drainTo(c) empties queue into another collection c |
965 |
< |
*/ |
965 |
> |
*/ |
966 |
|
public void testDrainTo() { |
967 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
968 |
|
ArrayList l = new ArrayList(); |
969 |
|
q.drainTo(l); |
970 |
|
assertEquals(q.size(), 0); |
971 |
|
assertEquals(l.size(), SIZE); |
972 |
< |
for (int i = 0; i < SIZE; ++i) |
972 |
> |
for (int i = 0; i < SIZE; ++i) |
973 |
|
assertEquals(l.get(i), new Integer(i)); |
974 |
|
q.add(zero); |
975 |
|
q.add(one); |
980 |
|
q.drainTo(l); |
981 |
|
assertEquals(q.size(), 0); |
982 |
|
assertEquals(l.size(), 2); |
983 |
< |
for (int i = 0; i < 2; ++i) |
983 |
> |
for (int i = 0; i < 2; ++i) |
984 |
|
assertEquals(l.get(i), new Integer(i)); |
985 |
|
} |
986 |
|
|
987 |
|
/** |
988 |
|
* drainTo empties full queue, unblocking a waiting put. |
989 |
< |
*/ |
989 |
> |
*/ |
990 |
|
public void testDrainToWithActivePut() { |
991 |
|
final ArrayBlockingQueue q = populatedQueue(SIZE); |
992 |
|
Thread t = new Thread(new Runnable() { |
993 |
|
public void run() { |
994 |
|
try { |
995 |
|
q.put(new Integer(SIZE+1)); |
996 |
< |
} catch (InterruptedException ie){ |
996 |
> |
} catch (InterruptedException ie){ |
997 |
|
threadUnexpectedException(); |
998 |
|
} |
999 |
|
} |
1003 |
|
ArrayList l = new ArrayList(); |
1004 |
|
q.drainTo(l); |
1005 |
|
assertTrue(l.size() >= SIZE); |
1006 |
< |
for (int i = 0; i < SIZE; ++i) |
1006 |
> |
for (int i = 0; i < SIZE; ++i) |
1007 |
|
assertEquals(l.get(i), new Integer(i)); |
1008 |
|
t.join(); |
1009 |
|
assertTrue(q.size() + l.size() >= SIZE); |
1014 |
|
|
1015 |
|
/** |
1016 |
|
* drainTo(null, n) throws NPE |
1017 |
< |
*/ |
1017 |
> |
*/ |
1018 |
|
public void testDrainToNullN() { |
1019 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
1020 |
|
try { |
1026 |
|
|
1027 |
|
/** |
1028 |
|
* drainTo(this, n) throws IAE |
1029 |
< |
*/ |
1029 |
> |
*/ |
1030 |
|
public void testDrainToSelfN() { |
1031 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
1032 |
|
try { |
1038 |
|
|
1039 |
|
/** |
1040 |
|
* drainTo(c, n) empties first max {n, size} elements of queue into c |
1041 |
< |
*/ |
1041 |
> |
*/ |
1042 |
|
public void testDrainToN() { |
1043 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE*2); |
1044 |
|
for (int i = 0; i < SIZE + 2; ++i) { |
1049 |
|
int k = (i < SIZE)? i : SIZE; |
1050 |
|
assertEquals(l.size(), k); |
1051 |
|
assertEquals(q.size(), SIZE-k); |
1052 |
< |
for (int j = 0; j < k; ++j) |
1052 |
> |
for (int j = 0; j < k; ++j) |
1053 |
|
assertEquals(l.get(j), new Integer(j)); |
1054 |
|
while (q.poll() != null) ; |
1055 |
|
} |