6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
< |
import junit.framework.*; |
10 |
< |
import java.util.Arrays; |
9 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
> |
|
11 |
|
import java.util.ArrayList; |
12 |
+ |
import java.util.Arrays; |
13 |
|
import java.util.Collection; |
14 |
|
import java.util.Iterator; |
15 |
|
import java.util.NoSuchElementException; |
19 |
|
import java.util.concurrent.CountDownLatch; |
20 |
|
import java.util.concurrent.Executors; |
21 |
|
import java.util.concurrent.ExecutorService; |
22 |
< |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
22 |
> |
import java.util.concurrent.ThreadLocalRandom; |
23 |
> |
|
24 |
> |
import junit.framework.Test; |
25 |
|
|
26 |
|
public class ArrayBlockingQueueTest extends JSR166TestCase { |
27 |
|
|
28 |
|
public static class Fair extends BlockingQueueTest { |
29 |
|
protected BlockingQueue emptyCollection() { |
30 |
< |
return new ArrayBlockingQueue(SIZE, true); |
30 |
> |
return populatedQueue(0, SIZE, 2 * SIZE, true); |
31 |
|
} |
32 |
|
} |
33 |
|
|
34 |
|
public static class NonFair extends BlockingQueueTest { |
35 |
|
protected BlockingQueue emptyCollection() { |
36 |
< |
return new ArrayBlockingQueue(SIZE, false); |
36 |
> |
return populatedQueue(0, SIZE, 2 * SIZE, false); |
37 |
|
} |
38 |
|
} |
39 |
|
|
40 |
|
public static void main(String[] args) { |
41 |
< |
junit.textui.TestRunner.run(suite()); |
41 |
> |
main(suite(), args); |
42 |
|
} |
43 |
|
|
44 |
|
public static Test suite() { |
45 |
+ |
class Implementation implements CollectionImplementation { |
46 |
+ |
public Class<?> klazz() { return ArrayBlockingQueue.class; } |
47 |
+ |
public Collection emptyCollection() { |
48 |
+ |
boolean fair = ThreadLocalRandom.current().nextBoolean(); |
49 |
+ |
return populatedQueue(0, SIZE, 2 * SIZE, fair); |
50 |
+ |
} |
51 |
+ |
public Object makeElement(int i) { return i; } |
52 |
+ |
public boolean isConcurrent() { return true; } |
53 |
+ |
public boolean permitsNulls() { return false; } |
54 |
+ |
} |
55 |
|
return newTestSuite(ArrayBlockingQueueTest.class, |
56 |
|
new Fair().testSuite(), |
57 |
< |
new NonFair().testSuite()); |
57 |
> |
new NonFair().testSuite(), |
58 |
> |
CollectionTest.testSuite(new Implementation())); |
59 |
|
} |
60 |
|
|
61 |
|
/** |
62 |
< |
* Create a queue of given size containing consecutive |
63 |
< |
* Integers 0 ... n. |
62 |
> |
* Returns a new queue of given size containing consecutive |
63 |
> |
* Integers 0 ... n - 1. |
64 |
|
*/ |
65 |
< |
private ArrayBlockingQueue<Integer> populatedQueue(int n) { |
66 |
< |
ArrayBlockingQueue<Integer> q = new ArrayBlockingQueue<Integer>(n); |
65 |
> |
static ArrayBlockingQueue<Integer> populatedQueue(int n) { |
66 |
> |
return populatedQueue(n, n, n, false); |
67 |
> |
} |
68 |
> |
|
69 |
> |
/** |
70 |
> |
* Returns a new queue of given size containing consecutive |
71 |
> |
* Integers 0 ... n - 1, with given capacity range and fairness. |
72 |
> |
*/ |
73 |
> |
static ArrayBlockingQueue<Integer> populatedQueue( |
74 |
> |
int size, int minCapacity, int maxCapacity, boolean fair) { |
75 |
> |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
76 |
> |
int capacity = rnd.nextInt(minCapacity, maxCapacity + 1); |
77 |
> |
ArrayBlockingQueue<Integer> q = new ArrayBlockingQueue<>(capacity); |
78 |
|
assertTrue(q.isEmpty()); |
79 |
< |
for (int i = 0; i < n; i++) |
80 |
< |
assertTrue(q.offer(new Integer(i))); |
81 |
< |
assertFalse(q.isEmpty()); |
82 |
< |
assertEquals(0, q.remainingCapacity()); |
83 |
< |
assertEquals(n, q.size()); |
79 |
> |
// shuffle circular array elements so they wrap |
80 |
> |
{ |
81 |
> |
int n = rnd.nextInt(capacity); |
82 |
> |
for (int i = 0; i < n; i++) q.add(42); |
83 |
> |
for (int i = 0; i < n; i++) q.remove(); |
84 |
> |
} |
85 |
> |
for (int i = 0; i < size; i++) |
86 |
> |
assertTrue(q.offer((Integer) i)); |
87 |
> |
assertEquals(size == 0, q.isEmpty()); |
88 |
> |
assertEquals(capacity - size, q.remainingCapacity()); |
89 |
> |
assertEquals(size, q.size()); |
90 |
> |
if (size > 0) |
91 |
> |
assertEquals((Integer) 0, q.peek()); |
92 |
|
return q; |
93 |
|
} |
94 |
|
|
135 |
|
*/ |
136 |
|
public void testConstructor5() { |
137 |
|
Integer[] ints = new Integer[SIZE]; |
138 |
< |
for (int i = 0; i < SIZE-1; ++i) |
138 |
> |
for (int i = 0; i < SIZE - 1; ++i) |
139 |
|
ints[i] = i; |
140 |
|
Collection<Integer> elements = Arrays.asList(ints); |
141 |
|
try { |
142 |
< |
new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints)); |
142 |
> |
new ArrayBlockingQueue(SIZE, false, elements); |
143 |
|
shouldThrow(); |
144 |
|
} catch (NullPointerException success) {} |
145 |
|
} |
190 |
|
* remainingCapacity decreases on add, increases on remove |
191 |
|
*/ |
192 |
|
public void testRemainingCapacity() { |
193 |
< |
ArrayBlockingQueue q = populatedQueue(SIZE); |
193 |
> |
BlockingQueue q = populatedQueue(SIZE); |
194 |
|
for (int i = 0; i < SIZE; ++i) { |
195 |
|
assertEquals(i, q.remainingCapacity()); |
196 |
< |
assertEquals(SIZE-i, q.size()); |
197 |
< |
q.remove(); |
196 |
> |
assertEquals(SIZE, q.size() + q.remainingCapacity()); |
197 |
> |
assertEquals(i, q.remove()); |
198 |
|
} |
199 |
|
for (int i = 0; i < SIZE; ++i) { |
200 |
< |
assertEquals(SIZE-i, q.remainingCapacity()); |
201 |
< |
assertEquals(i, q.size()); |
202 |
< |
q.add(new Integer(i)); |
200 |
> |
assertEquals(SIZE - i, q.remainingCapacity()); |
201 |
> |
assertEquals(SIZE, q.size() + q.remainingCapacity()); |
202 |
> |
assertTrue(q.add(i)); |
203 |
|
} |
204 |
|
} |
205 |
|
|
216 |
|
* add succeeds if not full; throws ISE if full |
217 |
|
*/ |
218 |
|
public void testAdd() { |
219 |
+ |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
220 |
+ |
for (int i = 0; i < SIZE; ++i) { |
221 |
+ |
assertTrue(q.add(new Integer(i))); |
222 |
+ |
} |
223 |
+ |
assertEquals(0, q.remainingCapacity()); |
224 |
|
try { |
187 |
– |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
188 |
– |
for (int i = 0; i < SIZE; ++i) { |
189 |
– |
assertTrue(q.add(new Integer(i))); |
190 |
– |
} |
191 |
– |
assertEquals(0, q.remainingCapacity()); |
225 |
|
q.add(new Integer(SIZE)); |
226 |
|
shouldThrow(); |
227 |
|
} catch (IllegalStateException success) {} |
231 |
|
* addAll(this) throws IAE |
232 |
|
*/ |
233 |
|
public void testAddAllSelf() { |
234 |
+ |
ArrayBlockingQueue q = populatedQueue(SIZE); |
235 |
|
try { |
202 |
– |
ArrayBlockingQueue q = populatedQueue(SIZE); |
236 |
|
q.addAll(q); |
237 |
|
shouldThrow(); |
238 |
|
} catch (IllegalArgumentException success) {} |
243 |
|
* possibly adding some elements |
244 |
|
*/ |
245 |
|
public void testAddAll3() { |
246 |
+ |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
247 |
+ |
Integer[] ints = new Integer[SIZE]; |
248 |
+ |
for (int i = 0; i < SIZE - 1; ++i) |
249 |
+ |
ints[i] = new Integer(i); |
250 |
|
try { |
214 |
– |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
215 |
– |
Integer[] ints = new Integer[SIZE]; |
216 |
– |
for (int i = 0; i < SIZE-1; ++i) |
217 |
– |
ints[i] = new Integer(i); |
251 |
|
q.addAll(Arrays.asList(ints)); |
252 |
|
shouldThrow(); |
253 |
|
} catch (NullPointerException success) {} |
257 |
|
* addAll throws ISE if not enough room |
258 |
|
*/ |
259 |
|
public void testAddAll4() { |
260 |
+ |
ArrayBlockingQueue q = new ArrayBlockingQueue(1); |
261 |
+ |
Integer[] ints = new Integer[SIZE]; |
262 |
+ |
for (int i = 0; i < SIZE; ++i) |
263 |
+ |
ints[i] = new Integer(i); |
264 |
|
try { |
228 |
– |
ArrayBlockingQueue q = new ArrayBlockingQueue(1); |
229 |
– |
Integer[] ints = new Integer[SIZE]; |
230 |
– |
for (int i = 0; i < SIZE; ++i) |
231 |
– |
ints[i] = new Integer(i); |
265 |
|
q.addAll(Arrays.asList(ints)); |
266 |
|
shouldThrow(); |
267 |
|
} catch (IllegalStateException success) {} |
288 |
|
public void testPut() throws InterruptedException { |
289 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
290 |
|
for (int i = 0; i < SIZE; ++i) { |
291 |
< |
Integer I = new Integer(i); |
292 |
< |
q.put(I); |
293 |
< |
assertTrue(q.contains(I)); |
291 |
> |
Integer x = new Integer(i); |
292 |
> |
q.put(x); |
293 |
> |
assertTrue(q.contains(x)); |
294 |
|
} |
295 |
|
assertEquals(0, q.remainingCapacity()); |
296 |
|
} |
482 |
|
final CountDownLatch aboutToWait = new CountDownLatch(1); |
483 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
484 |
|
public void realRun() throws InterruptedException { |
485 |
+ |
long startTime = System.nanoTime(); |
486 |
|
for (int i = 0; i < SIZE; ++i) { |
453 |
– |
long t0 = System.nanoTime(); |
487 |
|
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
455 |
– |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
488 |
|
} |
457 |
– |
long t0 = System.nanoTime(); |
489 |
|
aboutToWait.countDown(); |
490 |
|
try { |
491 |
< |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
491 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
492 |
|
shouldThrow(); |
493 |
|
} catch (InterruptedException success) { |
494 |
< |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
494 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
495 |
|
} |
496 |
|
}}); |
497 |
|
|
498 |
< |
aboutToWait.await(); |
499 |
< |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
498 |
> |
await(aboutToWait); |
499 |
> |
waitForThreadToEnterWaitState(t); |
500 |
|
t.interrupt(); |
501 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
501 |
> |
awaitTermination(t); |
502 |
|
checkEmpty(q); |
503 |
|
} |
504 |
|
|
601 |
|
assertTrue(changed); |
602 |
|
|
603 |
|
assertTrue(q.containsAll(p)); |
604 |
< |
assertEquals(SIZE-i, q.size()); |
604 |
> |
assertEquals(SIZE - i, q.size()); |
605 |
|
p.remove(); |
606 |
|
} |
607 |
|
} |
614 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
615 |
|
ArrayBlockingQueue p = populatedQueue(i); |
616 |
|
assertTrue(q.removeAll(p)); |
617 |
< |
assertEquals(SIZE-i, q.size()); |
617 |
> |
assertEquals(SIZE - i, q.size()); |
618 |
|
for (int j = 0; j < i; ++j) { |
619 |
< |
Integer I = (Integer)(p.remove()); |
620 |
< |
assertFalse(q.contains(I)); |
619 |
> |
Integer x = (Integer)(p.remove()); |
620 |
> |
assertFalse(q.contains(x)); |
621 |
|
} |
622 |
|
} |
623 |
|
} |
624 |
|
|
625 |
< |
/** |
626 |
< |
* toArray contains all elements in FIFO order |
627 |
< |
*/ |
628 |
< |
public void testToArray() { |
629 |
< |
ArrayBlockingQueue q = populatedQueue(SIZE); |
630 |
< |
Object[] o = q.toArray(); |
631 |
< |
for (int i = 0; i < o.length; i++) |
632 |
< |
assertSame(o[i], q.poll()); |
625 |
> |
void checkToArray(ArrayBlockingQueue<Integer> q) { |
626 |
> |
int size = q.size(); |
627 |
> |
Object[] a1 = q.toArray(); |
628 |
> |
assertEquals(size, a1.length); |
629 |
> |
Integer[] a2 = q.toArray(new Integer[0]); |
630 |
> |
assertEquals(size, a2.length); |
631 |
> |
Integer[] a3 = q.toArray(new Integer[Math.max(0, size - 1)]); |
632 |
> |
assertEquals(size, a3.length); |
633 |
> |
Integer[] a4 = new Integer[size]; |
634 |
> |
assertSame(a4, q.toArray(a4)); |
635 |
> |
Integer[] a5 = new Integer[size + 1]; |
636 |
> |
Arrays.fill(a5, 42); |
637 |
> |
assertSame(a5, q.toArray(a5)); |
638 |
> |
Integer[] a6 = new Integer[size + 2]; |
639 |
> |
Arrays.fill(a6, 42); |
640 |
> |
assertSame(a6, q.toArray(a6)); |
641 |
> |
Object[][] as = { a1, a2, a3, a4, a5, a6 }; |
642 |
> |
for (Object[] a : as) { |
643 |
> |
if (a.length > size) assertNull(a[size]); |
644 |
> |
if (a.length > size + 1) assertEquals(42, a[size + 1]); |
645 |
> |
} |
646 |
> |
Iterator it = q.iterator(); |
647 |
> |
Integer s = q.peek(); |
648 |
> |
for (int i = 0; i < size; i++) { |
649 |
> |
Integer x = (Integer) it.next(); |
650 |
> |
assertEquals(s + i, (int) x); |
651 |
> |
for (Object[] a : as) |
652 |
> |
assertSame(a1[i], x); |
653 |
> |
} |
654 |
|
} |
655 |
|
|
656 |
|
/** |
657 |
< |
* toArray(a) contains all elements in FIFO order |
657 |
> |
* toArray() and toArray(a) contain all elements in FIFO order |
658 |
|
*/ |
659 |
< |
public void testToArray2() { |
660 |
< |
ArrayBlockingQueue<Integer> q = populatedQueue(SIZE); |
661 |
< |
Integer[] ints = new Integer[SIZE]; |
662 |
< |
Integer[] array = q.toArray(ints); |
663 |
< |
assertSame(ints, array); |
664 |
< |
for (int i = 0; i < ints.length; i++) |
665 |
< |
assertSame(ints[i], q.poll()); |
659 |
> |
public void testToArray() { |
660 |
> |
final ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
661 |
> |
final int size = rnd.nextInt(6); |
662 |
> |
final int capacity = Math.max(1, size + rnd.nextInt(size + 1)); |
663 |
> |
ArrayBlockingQueue<Integer> q = new ArrayBlockingQueue<>(capacity); |
664 |
> |
for (int i = 0; i < size; i++) { |
665 |
> |
checkToArray(q); |
666 |
> |
q.add(i); |
667 |
> |
} |
668 |
> |
// Provoke wraparound |
669 |
> |
int added = size * 2; |
670 |
> |
for (int i = 0; i < added; i++) { |
671 |
> |
checkToArray(q); |
672 |
> |
assertEquals((Integer) i, q.poll()); |
673 |
> |
q.add(size + i); |
674 |
> |
} |
675 |
> |
for (int i = 0; i < size; i++) { |
676 |
> |
checkToArray(q); |
677 |
> |
assertEquals((Integer) (added + i), q.poll()); |
678 |
> |
} |
679 |
|
} |
680 |
|
|
681 |
|
/** |
682 |
|
* toArray(incompatible array type) throws ArrayStoreException |
683 |
|
*/ |
684 |
< |
public void testToArray1_BadArg() { |
684 |
> |
public void testToArray_incompatibleArrayType() { |
685 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
686 |
|
try { |
687 |
|
q.toArray(new String[10]); |
688 |
|
shouldThrow(); |
689 |
|
} catch (ArrayStoreException success) {} |
690 |
+ |
try { |
691 |
+ |
q.toArray(new String[0]); |
692 |
+ |
shouldThrow(); |
693 |
+ |
} catch (ArrayStoreException success) {} |
694 |
|
} |
695 |
|
|
696 |
|
/** |
699 |
|
public void testIterator() throws InterruptedException { |
700 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
701 |
|
Iterator it = q.iterator(); |
702 |
< |
while (it.hasNext()) { |
702 |
> |
int i; |
703 |
> |
for (i = 0; it.hasNext(); i++) |
704 |
> |
assertTrue(q.contains(it.next())); |
705 |
> |
assertEquals(i, SIZE); |
706 |
> |
assertIteratorExhausted(it); |
707 |
> |
|
708 |
> |
it = q.iterator(); |
709 |
> |
for (i = 0; it.hasNext(); i++) |
710 |
|
assertEquals(it.next(), q.take()); |
711 |
< |
} |
711 |
> |
assertEquals(i, SIZE); |
712 |
> |
assertIteratorExhausted(it); |
713 |
> |
} |
714 |
> |
|
715 |
> |
/** |
716 |
> |
* iterator of empty collection has no elements |
717 |
> |
*/ |
718 |
> |
public void testEmptyIterator() { |
719 |
> |
assertIteratorExhausted(new ArrayBlockingQueue(SIZE).iterator()); |
720 |
|
} |
721 |
|
|
722 |
|
/** |
789 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
790 |
|
q.add(one); |
791 |
|
q.add(two); |
708 |
– |
ExecutorService executor = Executors.newFixedThreadPool(2); |
792 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
793 |
< |
executor.execute(new CheckedRunnable() { |
794 |
< |
public void realRun() throws InterruptedException { |
795 |
< |
assertFalse(q.offer(three)); |
796 |
< |
threadsStarted.await(); |
797 |
< |
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
798 |
< |
assertEquals(0, q.remainingCapacity()); |
799 |
< |
}}); |
800 |
< |
|
801 |
< |
executor.execute(new CheckedRunnable() { |
802 |
< |
public void realRun() throws InterruptedException { |
803 |
< |
threadsStarted.await(); |
804 |
< |
assertEquals(0, q.remainingCapacity()); |
805 |
< |
assertSame(one, q.take()); |
806 |
< |
}}); |
807 |
< |
|
808 |
< |
joinPool(executor); |
793 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
794 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
795 |
> |
executor.execute(new CheckedRunnable() { |
796 |
> |
public void realRun() throws InterruptedException { |
797 |
> |
assertFalse(q.offer(three)); |
798 |
> |
threadsStarted.await(); |
799 |
> |
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
800 |
> |
assertEquals(0, q.remainingCapacity()); |
801 |
> |
}}); |
802 |
> |
|
803 |
> |
executor.execute(new CheckedRunnable() { |
804 |
> |
public void realRun() throws InterruptedException { |
805 |
> |
threadsStarted.await(); |
806 |
> |
assertEquals(0, q.remainingCapacity()); |
807 |
> |
assertSame(one, q.take()); |
808 |
> |
}}); |
809 |
> |
} |
810 |
|
} |
811 |
|
|
812 |
|
/** |
815 |
|
public void testPollInExecutor() { |
816 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
817 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
818 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
819 |
< |
executor.execute(new CheckedRunnable() { |
820 |
< |
public void realRun() throws InterruptedException { |
821 |
< |
assertNull(q.poll()); |
822 |
< |
threadsStarted.await(); |
823 |
< |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
824 |
< |
checkEmpty(q); |
825 |
< |
}}); |
826 |
< |
|
827 |
< |
executor.execute(new CheckedRunnable() { |
828 |
< |
public void realRun() throws InterruptedException { |
829 |
< |
threadsStarted.await(); |
830 |
< |
q.put(one); |
831 |
< |
}}); |
832 |
< |
|
833 |
< |
joinPool(executor); |
818 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
819 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
820 |
> |
executor.execute(new CheckedRunnable() { |
821 |
> |
public void realRun() throws InterruptedException { |
822 |
> |
assertNull(q.poll()); |
823 |
> |
threadsStarted.await(); |
824 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
825 |
> |
checkEmpty(q); |
826 |
> |
}}); |
827 |
> |
|
828 |
> |
executor.execute(new CheckedRunnable() { |
829 |
> |
public void realRun() throws InterruptedException { |
830 |
> |
threadsStarted.await(); |
831 |
> |
q.put(one); |
832 |
> |
}}); |
833 |
> |
} |
834 |
|
} |
835 |
|
|
836 |
|
/** |
840 |
|
Queue x = populatedQueue(SIZE); |
841 |
|
Queue y = serialClone(x); |
842 |
|
|
843 |
< |
assertTrue(x != y); |
843 |
> |
assertNotSame(x, y); |
844 |
|
assertEquals(x.size(), y.size()); |
845 |
|
assertEquals(x.toString(), y.toString()); |
846 |
|
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
882 |
|
final ArrayBlockingQueue q = populatedQueue(SIZE); |
883 |
|
Thread t = new Thread(new CheckedRunnable() { |
884 |
|
public void realRun() throws InterruptedException { |
885 |
< |
q.put(new Integer(SIZE+1)); |
885 |
> |
q.put(new Integer(SIZE + 1)); |
886 |
|
}}); |
887 |
|
|
888 |
|
t.start(); |
899 |
|
* drainTo(c, n) empties first min(n, size) elements of queue into c |
900 |
|
*/ |
901 |
|
public void testDrainToN() { |
902 |
< |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE*2); |
902 |
> |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE * 2); |
903 |
|
for (int i = 0; i < SIZE + 2; ++i) { |
904 |
|
for (int j = 0; j < SIZE; j++) |
905 |
|
assertTrue(q.offer(new Integer(j))); |
906 |
|
ArrayList l = new ArrayList(); |
907 |
|
q.drainTo(l, i); |
908 |
|
int k = (i < SIZE) ? i : SIZE; |
909 |
< |
assertEquals(l.size(), k); |
910 |
< |
assertEquals(q.size(), SIZE-k); |
909 |
> |
assertEquals(k, l.size()); |
910 |
> |
assertEquals(SIZE - k, q.size()); |
911 |
|
for (int j = 0; j < k; ++j) |
912 |
|
assertEquals(l.get(j), new Integer(j)); |
913 |
< |
while (q.poll() != null) ; |
913 |
> |
do {} while (q.poll() != null); |
914 |
|
} |
915 |
|
} |
916 |
|
|
917 |
+ |
/** |
918 |
+ |
* remove(null), contains(null) always return false |
919 |
+ |
*/ |
920 |
+ |
public void testNeverContainsNull() { |
921 |
+ |
Collection<?>[] qs = { |
922 |
+ |
new ArrayBlockingQueue<Object>(10), |
923 |
+ |
populatedQueue(2), |
924 |
+ |
}; |
925 |
+ |
|
926 |
+ |
for (Collection<?> q : qs) { |
927 |
+ |
assertFalse(q.contains(null)); |
928 |
+ |
assertFalse(q.remove(null)); |
929 |
+ |
} |
930 |
+ |
} |
931 |
|
} |