37 |
|
} |
38 |
|
|
39 |
|
public static void main(String[] args) { |
40 |
< |
junit.textui.TestRunner.run(suite()); |
40 |
> |
main(suite(), args); |
41 |
|
} |
42 |
|
|
43 |
|
public static Test suite() { |
44 |
+ |
class Implementation implements CollectionImplementation { |
45 |
+ |
public Class<?> klazz() { return ArrayBlockingQueue.class; } |
46 |
+ |
public Collection emptyCollection() { return new ArrayBlockingQueue(SIZE, false); } |
47 |
+ |
public Object makeElement(int i) { return i; } |
48 |
+ |
public boolean isConcurrent() { return true; } |
49 |
+ |
public boolean permitsNulls() { return false; } |
50 |
+ |
} |
51 |
|
return newTestSuite(ArrayBlockingQueueTest.class, |
52 |
|
new Fair().testSuite(), |
53 |
< |
new NonFair().testSuite()); |
53 |
> |
new NonFair().testSuite(), |
54 |
> |
CollectionTest.testSuite(new Implementation())); |
55 |
|
} |
56 |
|
|
57 |
|
/** |
58 |
|
* Returns a new queue of given size containing consecutive |
59 |
< |
* Integers 0 ... n. |
59 |
> |
* Integers 0 ... n - 1. |
60 |
|
*/ |
61 |
|
private ArrayBlockingQueue<Integer> populatedQueue(int n) { |
62 |
|
ArrayBlockingQueue<Integer> q = new ArrayBlockingQueue<Integer>(n); |
66 |
|
assertFalse(q.isEmpty()); |
67 |
|
assertEquals(0, q.remainingCapacity()); |
68 |
|
assertEquals(n, q.size()); |
69 |
+ |
assertEquals((Integer) 0, q.peek()); |
70 |
|
return q; |
71 |
|
} |
72 |
|
|
113 |
|
*/ |
114 |
|
public void testConstructor5() { |
115 |
|
Integer[] ints = new Integer[SIZE]; |
116 |
< |
for (int i = 0; i < SIZE-1; ++i) |
116 |
> |
for (int i = 0; i < SIZE - 1; ++i) |
117 |
|
ints[i] = i; |
118 |
|
Collection<Integer> elements = Arrays.asList(ints); |
119 |
|
try { |
120 |
< |
new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints)); |
120 |
> |
new ArrayBlockingQueue(SIZE, false, elements); |
121 |
|
shouldThrow(); |
122 |
|
} catch (NullPointerException success) {} |
123 |
|
} |
168 |
|
* remainingCapacity decreases on add, increases on remove |
169 |
|
*/ |
170 |
|
public void testRemainingCapacity() { |
171 |
< |
ArrayBlockingQueue q = populatedQueue(SIZE); |
171 |
> |
BlockingQueue q = populatedQueue(SIZE); |
172 |
|
for (int i = 0; i < SIZE; ++i) { |
173 |
|
assertEquals(i, q.remainingCapacity()); |
174 |
< |
assertEquals(SIZE-i, q.size()); |
175 |
< |
q.remove(); |
174 |
> |
assertEquals(SIZE, q.size() + q.remainingCapacity()); |
175 |
> |
assertEquals(i, q.remove()); |
176 |
|
} |
177 |
|
for (int i = 0; i < SIZE; ++i) { |
178 |
< |
assertEquals(SIZE-i, q.remainingCapacity()); |
179 |
< |
assertEquals(i, q.size()); |
180 |
< |
q.add(new Integer(i)); |
178 |
> |
assertEquals(SIZE - i, q.remainingCapacity()); |
179 |
> |
assertEquals(SIZE, q.size() + q.remainingCapacity()); |
180 |
> |
assertTrue(q.add(i)); |
181 |
|
} |
182 |
|
} |
183 |
|
|
194 |
|
* add succeeds if not full; throws ISE if full |
195 |
|
*/ |
196 |
|
public void testAdd() { |
197 |
+ |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
198 |
+ |
for (int i = 0; i < SIZE; ++i) { |
199 |
+ |
assertTrue(q.add(new Integer(i))); |
200 |
+ |
} |
201 |
+ |
assertEquals(0, q.remainingCapacity()); |
202 |
|
try { |
189 |
– |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
190 |
– |
for (int i = 0; i < SIZE; ++i) { |
191 |
– |
assertTrue(q.add(new Integer(i))); |
192 |
– |
} |
193 |
– |
assertEquals(0, q.remainingCapacity()); |
203 |
|
q.add(new Integer(SIZE)); |
204 |
|
shouldThrow(); |
205 |
|
} catch (IllegalStateException success) {} |
209 |
|
* addAll(this) throws IAE |
210 |
|
*/ |
211 |
|
public void testAddAllSelf() { |
212 |
+ |
ArrayBlockingQueue q = populatedQueue(SIZE); |
213 |
|
try { |
204 |
– |
ArrayBlockingQueue q = populatedQueue(SIZE); |
214 |
|
q.addAll(q); |
215 |
|
shouldThrow(); |
216 |
|
} catch (IllegalArgumentException success) {} |
221 |
|
* possibly adding some elements |
222 |
|
*/ |
223 |
|
public void testAddAll3() { |
224 |
+ |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
225 |
+ |
Integer[] ints = new Integer[SIZE]; |
226 |
+ |
for (int i = 0; i < SIZE - 1; ++i) |
227 |
+ |
ints[i] = new Integer(i); |
228 |
|
try { |
216 |
– |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
217 |
– |
Integer[] ints = new Integer[SIZE]; |
218 |
– |
for (int i = 0; i < SIZE-1; ++i) |
219 |
– |
ints[i] = new Integer(i); |
229 |
|
q.addAll(Arrays.asList(ints)); |
230 |
|
shouldThrow(); |
231 |
|
} catch (NullPointerException success) {} |
235 |
|
* addAll throws ISE if not enough room |
236 |
|
*/ |
237 |
|
public void testAddAll4() { |
238 |
+ |
ArrayBlockingQueue q = new ArrayBlockingQueue(1); |
239 |
+ |
Integer[] ints = new Integer[SIZE]; |
240 |
+ |
for (int i = 0; i < SIZE; ++i) |
241 |
+ |
ints[i] = new Integer(i); |
242 |
|
try { |
230 |
– |
ArrayBlockingQueue q = new ArrayBlockingQueue(1); |
231 |
– |
Integer[] ints = new Integer[SIZE]; |
232 |
– |
for (int i = 0; i < SIZE; ++i) |
233 |
– |
ints[i] = new Integer(i); |
243 |
|
q.addAll(Arrays.asList(ints)); |
244 |
|
shouldThrow(); |
245 |
|
} catch (IllegalStateException success) {} |
266 |
|
public void testPut() throws InterruptedException { |
267 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
268 |
|
for (int i = 0; i < SIZE; ++i) { |
269 |
< |
Integer I = new Integer(i); |
270 |
< |
q.put(I); |
271 |
< |
assertTrue(q.contains(I)); |
269 |
> |
Integer x = new Integer(i); |
270 |
> |
q.put(x); |
271 |
> |
assertTrue(q.contains(x)); |
272 |
|
} |
273 |
|
assertEquals(0, q.remainingCapacity()); |
274 |
|
} |
460 |
|
final CountDownLatch aboutToWait = new CountDownLatch(1); |
461 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
462 |
|
public void realRun() throws InterruptedException { |
463 |
+ |
long startTime = System.nanoTime(); |
464 |
|
for (int i = 0; i < SIZE; ++i) { |
455 |
– |
long t0 = System.nanoTime(); |
465 |
|
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
457 |
– |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
466 |
|
} |
459 |
– |
long t0 = System.nanoTime(); |
467 |
|
aboutToWait.countDown(); |
468 |
|
try { |
469 |
< |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
469 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
470 |
|
shouldThrow(); |
471 |
|
} catch (InterruptedException success) { |
472 |
< |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
472 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
473 |
|
} |
474 |
|
}}); |
475 |
|
|
476 |
< |
aboutToWait.await(); |
477 |
< |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
476 |
> |
await(aboutToWait); |
477 |
> |
waitForThreadToEnterWaitState(t); |
478 |
|
t.interrupt(); |
479 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
479 |
> |
awaitTermination(t); |
480 |
|
checkEmpty(q); |
481 |
|
} |
482 |
|
|
579 |
|
assertTrue(changed); |
580 |
|
|
581 |
|
assertTrue(q.containsAll(p)); |
582 |
< |
assertEquals(SIZE-i, q.size()); |
582 |
> |
assertEquals(SIZE - i, q.size()); |
583 |
|
p.remove(); |
584 |
|
} |
585 |
|
} |
592 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
593 |
|
ArrayBlockingQueue p = populatedQueue(i); |
594 |
|
assertTrue(q.removeAll(p)); |
595 |
< |
assertEquals(SIZE-i, q.size()); |
595 |
> |
assertEquals(SIZE - i, q.size()); |
596 |
|
for (int j = 0; j < i; ++j) { |
597 |
< |
Integer I = (Integer)(p.remove()); |
598 |
< |
assertFalse(q.contains(I)); |
597 |
> |
Integer x = (Integer)(p.remove()); |
598 |
> |
assertFalse(q.contains(x)); |
599 |
|
} |
600 |
|
} |
601 |
|
} |
626 |
|
checkToArray(q); |
627 |
|
assertEquals(i, q.poll()); |
628 |
|
checkToArray(q); |
629 |
< |
q.add(SIZE+i); |
629 |
> |
q.add(SIZE + i); |
630 |
|
} |
631 |
|
for (int i = 0; i < SIZE; i++) { |
632 |
|
checkToArray(q); |
633 |
< |
assertEquals(SIZE+i, q.poll()); |
633 |
> |
assertEquals(SIZE + i, q.poll()); |
634 |
|
} |
635 |
|
} |
636 |
|
|
637 |
|
void checkToArray2(ArrayBlockingQueue q) { |
638 |
|
int size = q.size(); |
639 |
< |
Integer[] a1 = size == 0 ? null : new Integer[size-1]; |
639 |
> |
Integer[] a1 = (size == 0) ? null : new Integer[size - 1]; |
640 |
|
Integer[] a2 = new Integer[size]; |
641 |
< |
Integer[] a3 = new Integer[size+2]; |
641 |
> |
Integer[] a3 = new Integer[size + 2]; |
642 |
|
if (size > 0) Arrays.fill(a1, 42); |
643 |
|
Arrays.fill(a2, 42); |
644 |
|
Arrays.fill(a3, 42); |
645 |
< |
Integer[] b1 = size == 0 ? null : (Integer[]) q.toArray(a1); |
645 |
> |
Integer[] b1 = (size == 0) ? null : (Integer[]) q.toArray(a1); |
646 |
|
Integer[] b2 = (Integer[]) q.toArray(a2); |
647 |
|
Integer[] b3 = (Integer[]) q.toArray(a3); |
648 |
|
assertSame(a2, b2); |
656 |
|
assertSame(b3[i], x); |
657 |
|
} |
658 |
|
assertNull(a3[size]); |
659 |
< |
assertEquals(42, (int) a3[size+1]); |
659 |
> |
assertEquals(42, (int) a3[size + 1]); |
660 |
|
if (size > 0) { |
661 |
|
assertNotSame(a1, b1); |
662 |
|
assertEquals(size, b1.length); |
680 |
|
checkToArray2(q); |
681 |
|
assertEquals(i, q.poll()); |
682 |
|
checkToArray2(q); |
683 |
< |
q.add(SIZE+i); |
683 |
> |
q.add(SIZE + i); |
684 |
|
} |
685 |
|
for (int i = 0; i < SIZE; i++) { |
686 |
|
checkToArray2(q); |
687 |
< |
assertEquals(SIZE+i, q.poll()); |
687 |
> |
assertEquals(SIZE + i, q.poll()); |
688 |
|
} |
689 |
|
} |
690 |
|
|
705 |
|
public void testIterator() throws InterruptedException { |
706 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
707 |
|
Iterator it = q.iterator(); |
708 |
< |
while (it.hasNext()) { |
708 |
> |
int i; |
709 |
> |
for (i = 0; it.hasNext(); i++) |
710 |
> |
assertTrue(q.contains(it.next())); |
711 |
> |
assertEquals(i, SIZE); |
712 |
> |
assertIteratorExhausted(it); |
713 |
> |
|
714 |
> |
it = q.iterator(); |
715 |
> |
for (i = 0; it.hasNext(); i++) |
716 |
|
assertEquals(it.next(), q.take()); |
717 |
< |
} |
717 |
> |
assertEquals(i, SIZE); |
718 |
> |
assertIteratorExhausted(it); |
719 |
> |
} |
720 |
> |
|
721 |
> |
/** |
722 |
> |
* iterator of empty collection has no elements |
723 |
> |
*/ |
724 |
> |
public void testEmptyIterator() { |
725 |
> |
assertIteratorExhausted(new ArrayBlockingQueue(SIZE).iterator()); |
726 |
|
} |
727 |
|
|
728 |
|
/** |
795 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
796 |
|
q.add(one); |
797 |
|
q.add(two); |
776 |
– |
ExecutorService executor = Executors.newFixedThreadPool(2); |
798 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
799 |
< |
executor.execute(new CheckedRunnable() { |
800 |
< |
public void realRun() throws InterruptedException { |
801 |
< |
assertFalse(q.offer(three)); |
802 |
< |
threadsStarted.await(); |
803 |
< |
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
804 |
< |
assertEquals(0, q.remainingCapacity()); |
805 |
< |
}}); |
806 |
< |
|
807 |
< |
executor.execute(new CheckedRunnable() { |
808 |
< |
public void realRun() throws InterruptedException { |
809 |
< |
threadsStarted.await(); |
810 |
< |
assertEquals(0, q.remainingCapacity()); |
811 |
< |
assertSame(one, q.take()); |
812 |
< |
}}); |
813 |
< |
|
814 |
< |
joinPool(executor); |
799 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
800 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
801 |
> |
executor.execute(new CheckedRunnable() { |
802 |
> |
public void realRun() throws InterruptedException { |
803 |
> |
assertFalse(q.offer(three)); |
804 |
> |
threadsStarted.await(); |
805 |
> |
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
806 |
> |
assertEquals(0, q.remainingCapacity()); |
807 |
> |
}}); |
808 |
> |
|
809 |
> |
executor.execute(new CheckedRunnable() { |
810 |
> |
public void realRun() throws InterruptedException { |
811 |
> |
threadsStarted.await(); |
812 |
> |
assertEquals(0, q.remainingCapacity()); |
813 |
> |
assertSame(one, q.take()); |
814 |
> |
}}); |
815 |
> |
} |
816 |
|
} |
817 |
|
|
818 |
|
/** |
821 |
|
public void testPollInExecutor() { |
822 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
823 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
824 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
825 |
< |
executor.execute(new CheckedRunnable() { |
826 |
< |
public void realRun() throws InterruptedException { |
827 |
< |
assertNull(q.poll()); |
828 |
< |
threadsStarted.await(); |
829 |
< |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
830 |
< |
checkEmpty(q); |
831 |
< |
}}); |
832 |
< |
|
833 |
< |
executor.execute(new CheckedRunnable() { |
834 |
< |
public void realRun() throws InterruptedException { |
835 |
< |
threadsStarted.await(); |
836 |
< |
q.put(one); |
837 |
< |
}}); |
838 |
< |
|
839 |
< |
joinPool(executor); |
824 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
825 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
826 |
> |
executor.execute(new CheckedRunnable() { |
827 |
> |
public void realRun() throws InterruptedException { |
828 |
> |
assertNull(q.poll()); |
829 |
> |
threadsStarted.await(); |
830 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
831 |
> |
checkEmpty(q); |
832 |
> |
}}); |
833 |
> |
|
834 |
> |
executor.execute(new CheckedRunnable() { |
835 |
> |
public void realRun() throws InterruptedException { |
836 |
> |
threadsStarted.await(); |
837 |
> |
q.put(one); |
838 |
> |
}}); |
839 |
> |
} |
840 |
|
} |
841 |
|
|
842 |
|
/** |
888 |
|
final ArrayBlockingQueue q = populatedQueue(SIZE); |
889 |
|
Thread t = new Thread(new CheckedRunnable() { |
890 |
|
public void realRun() throws InterruptedException { |
891 |
< |
q.put(new Integer(SIZE+1)); |
891 |
> |
q.put(new Integer(SIZE + 1)); |
892 |
|
}}); |
893 |
|
|
894 |
|
t.start(); |
905 |
|
* drainTo(c, n) empties first min(n, size) elements of queue into c |
906 |
|
*/ |
907 |
|
public void testDrainToN() { |
908 |
< |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE*2); |
908 |
> |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE * 2); |
909 |
|
for (int i = 0; i < SIZE + 2; ++i) { |
910 |
|
for (int j = 0; j < SIZE; j++) |
911 |
|
assertTrue(q.offer(new Integer(j))); |
913 |
|
q.drainTo(l, i); |
914 |
|
int k = (i < SIZE) ? i : SIZE; |
915 |
|
assertEquals(k, l.size()); |
916 |
< |
assertEquals(SIZE-k, q.size()); |
916 |
> |
assertEquals(SIZE - k, q.size()); |
917 |
|
for (int j = 0; j < k; ++j) |
918 |
|
assertEquals(l.get(j), new Integer(j)); |
919 |
|
do {} while (q.poll() != null); |