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() { |
104 |
|
*/ |
105 |
|
public void testConstructor5() { |
106 |
|
Integer[] ints = new Integer[SIZE]; |
107 |
< |
for (int i = 0; i < SIZE-1; ++i) |
107 |
> |
for (int i = 0; i < SIZE - 1; ++i) |
108 |
|
ints[i] = i; |
109 |
|
Collection<Integer> elements = Arrays.asList(ints); |
110 |
|
try { |
111 |
< |
new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints)); |
111 |
> |
new ArrayBlockingQueue(SIZE, false, elements); |
112 |
|
shouldThrow(); |
113 |
|
} catch (NullPointerException success) {} |
114 |
|
} |
159 |
|
* remainingCapacity decreases on add, increases on remove |
160 |
|
*/ |
161 |
|
public void testRemainingCapacity() { |
162 |
< |
ArrayBlockingQueue q = populatedQueue(SIZE); |
162 |
> |
BlockingQueue q = populatedQueue(SIZE); |
163 |
|
for (int i = 0; i < SIZE; ++i) { |
164 |
|
assertEquals(i, q.remainingCapacity()); |
165 |
< |
assertEquals(SIZE-i, q.size()); |
166 |
< |
q.remove(); |
165 |
> |
assertEquals(SIZE, q.size() + q.remainingCapacity()); |
166 |
> |
assertEquals(i, q.remove()); |
167 |
|
} |
168 |
|
for (int i = 0; i < SIZE; ++i) { |
169 |
< |
assertEquals(SIZE-i, q.remainingCapacity()); |
170 |
< |
assertEquals(i, q.size()); |
171 |
< |
q.add(new Integer(i)); |
169 |
> |
assertEquals(SIZE - i, q.remainingCapacity()); |
170 |
> |
assertEquals(SIZE, q.size() + q.remainingCapacity()); |
171 |
> |
assertTrue(q.add(i)); |
172 |
|
} |
173 |
|
} |
174 |
|
|
185 |
|
* add succeeds if not full; throws ISE if full |
186 |
|
*/ |
187 |
|
public void testAdd() { |
188 |
+ |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
189 |
+ |
for (int i = 0; i < SIZE; ++i) { |
190 |
+ |
assertTrue(q.add(new Integer(i))); |
191 |
+ |
} |
192 |
+ |
assertEquals(0, q.remainingCapacity()); |
193 |
|
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()); |
194 |
|
q.add(new Integer(SIZE)); |
195 |
|
shouldThrow(); |
196 |
|
} catch (IllegalStateException success) {} |
200 |
|
* addAll(this) throws IAE |
201 |
|
*/ |
202 |
|
public void testAddAllSelf() { |
203 |
+ |
ArrayBlockingQueue q = populatedQueue(SIZE); |
204 |
|
try { |
204 |
– |
ArrayBlockingQueue q = populatedQueue(SIZE); |
205 |
|
q.addAll(q); |
206 |
|
shouldThrow(); |
207 |
|
} catch (IllegalArgumentException success) {} |
212 |
|
* possibly adding some elements |
213 |
|
*/ |
214 |
|
public void testAddAll3() { |
215 |
+ |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
216 |
+ |
Integer[] ints = new Integer[SIZE]; |
217 |
+ |
for (int i = 0; i < SIZE - 1; ++i) |
218 |
+ |
ints[i] = new Integer(i); |
219 |
|
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); |
220 |
|
q.addAll(Arrays.asList(ints)); |
221 |
|
shouldThrow(); |
222 |
|
} catch (NullPointerException success) {} |
226 |
|
* addAll throws ISE if not enough room |
227 |
|
*/ |
228 |
|
public void testAddAll4() { |
229 |
+ |
ArrayBlockingQueue q = new ArrayBlockingQueue(1); |
230 |
+ |
Integer[] ints = new Integer[SIZE]; |
231 |
+ |
for (int i = 0; i < SIZE; ++i) |
232 |
+ |
ints[i] = new Integer(i); |
233 |
|
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); |
234 |
|
q.addAll(Arrays.asList(ints)); |
235 |
|
shouldThrow(); |
236 |
|
} catch (IllegalStateException success) {} |
451 |
|
final CountDownLatch aboutToWait = new CountDownLatch(1); |
452 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
453 |
|
public void realRun() throws InterruptedException { |
454 |
+ |
long startTime = System.nanoTime(); |
455 |
|
for (int i = 0; i < SIZE; ++i) { |
455 |
– |
long t0 = System.nanoTime(); |
456 |
|
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
457 |
– |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
457 |
|
} |
459 |
– |
long t0 = System.nanoTime(); |
458 |
|
aboutToWait.countDown(); |
459 |
|
try { |
460 |
< |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
460 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
461 |
|
shouldThrow(); |
462 |
|
} catch (InterruptedException success) { |
463 |
< |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
463 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
464 |
|
} |
465 |
|
}}); |
466 |
|
|
467 |
< |
aboutToWait.await(); |
468 |
< |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
467 |
> |
await(aboutToWait); |
468 |
> |
waitForThreadToEnterWaitState(t, LONG_DELAY_MS); |
469 |
|
t.interrupt(); |
470 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
470 |
> |
awaitTermination(t); |
471 |
|
checkEmpty(q); |
472 |
|
} |
473 |
|
|
570 |
|
assertTrue(changed); |
571 |
|
|
572 |
|
assertTrue(q.containsAll(p)); |
573 |
< |
assertEquals(SIZE-i, q.size()); |
573 |
> |
assertEquals(SIZE - i, q.size()); |
574 |
|
p.remove(); |
575 |
|
} |
576 |
|
} |
583 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
584 |
|
ArrayBlockingQueue p = populatedQueue(i); |
585 |
|
assertTrue(q.removeAll(p)); |
586 |
< |
assertEquals(SIZE-i, q.size()); |
586 |
> |
assertEquals(SIZE - i, q.size()); |
587 |
|
for (int j = 0; j < i; ++j) { |
588 |
|
Integer x = (Integer)(p.remove()); |
589 |
|
assertFalse(q.contains(x)); |
617 |
|
checkToArray(q); |
618 |
|
assertEquals(i, q.poll()); |
619 |
|
checkToArray(q); |
620 |
< |
q.add(SIZE+i); |
620 |
> |
q.add(SIZE + i); |
621 |
|
} |
622 |
|
for (int i = 0; i < SIZE; i++) { |
623 |
|
checkToArray(q); |
624 |
< |
assertEquals(SIZE+i, q.poll()); |
624 |
> |
assertEquals(SIZE + i, q.poll()); |
625 |
|
} |
626 |
|
} |
627 |
|
|
628 |
|
void checkToArray2(ArrayBlockingQueue q) { |
629 |
|
int size = q.size(); |
630 |
< |
Integer[] a1 = size == 0 ? null : new Integer[size-1]; |
630 |
> |
Integer[] a1 = (size == 0) ? null : new Integer[size - 1]; |
631 |
|
Integer[] a2 = new Integer[size]; |
632 |
< |
Integer[] a3 = new Integer[size+2]; |
632 |
> |
Integer[] a3 = new Integer[size + 2]; |
633 |
|
if (size > 0) Arrays.fill(a1, 42); |
634 |
|
Arrays.fill(a2, 42); |
635 |
|
Arrays.fill(a3, 42); |
636 |
< |
Integer[] b1 = size == 0 ? null : (Integer[]) q.toArray(a1); |
636 |
> |
Integer[] b1 = (size == 0) ? null : (Integer[]) q.toArray(a1); |
637 |
|
Integer[] b2 = (Integer[]) q.toArray(a2); |
638 |
|
Integer[] b3 = (Integer[]) q.toArray(a3); |
639 |
|
assertSame(a2, b2); |
647 |
|
assertSame(b3[i], x); |
648 |
|
} |
649 |
|
assertNull(a3[size]); |
650 |
< |
assertEquals(42, (int) a3[size+1]); |
650 |
> |
assertEquals(42, (int) a3[size + 1]); |
651 |
|
if (size > 0) { |
652 |
|
assertNotSame(a1, b1); |
653 |
|
assertEquals(size, b1.length); |
671 |
|
checkToArray2(q); |
672 |
|
assertEquals(i, q.poll()); |
673 |
|
checkToArray2(q); |
674 |
< |
q.add(SIZE+i); |
674 |
> |
q.add(SIZE + i); |
675 |
|
} |
676 |
|
for (int i = 0; i < SIZE; i++) { |
677 |
|
checkToArray2(q); |
678 |
< |
assertEquals(SIZE+i, q.poll()); |
678 |
> |
assertEquals(SIZE + i, q.poll()); |
679 |
|
} |
680 |
|
} |
681 |
|
|
696 |
|
public void testIterator() throws InterruptedException { |
697 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
698 |
|
Iterator it = q.iterator(); |
699 |
< |
while (it.hasNext()) { |
699 |
> |
int i; |
700 |
> |
for (i = 0; it.hasNext(); i++) |
701 |
> |
assertTrue(q.contains(it.next())); |
702 |
> |
assertEquals(i, SIZE); |
703 |
> |
assertIteratorExhausted(it); |
704 |
> |
|
705 |
> |
it = q.iterator(); |
706 |
> |
for (i = 0; it.hasNext(); i++) |
707 |
|
assertEquals(it.next(), q.take()); |
708 |
< |
} |
708 |
> |
assertEquals(i, SIZE); |
709 |
> |
assertIteratorExhausted(it); |
710 |
> |
} |
711 |
> |
|
712 |
> |
/** |
713 |
> |
* iterator of empty collection has no elements |
714 |
> |
*/ |
715 |
> |
public void testEmptyIterator() { |
716 |
> |
assertIteratorExhausted(new ArrayBlockingQueue(SIZE).iterator()); |
717 |
|
} |
718 |
|
|
719 |
|
/** |
786 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
787 |
|
q.add(one); |
788 |
|
q.add(two); |
776 |
– |
ExecutorService executor = Executors.newFixedThreadPool(2); |
789 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
790 |
< |
executor.execute(new CheckedRunnable() { |
791 |
< |
public void realRun() throws InterruptedException { |
792 |
< |
assertFalse(q.offer(three)); |
793 |
< |
threadsStarted.await(); |
794 |
< |
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
795 |
< |
assertEquals(0, q.remainingCapacity()); |
796 |
< |
}}); |
797 |
< |
|
798 |
< |
executor.execute(new CheckedRunnable() { |
799 |
< |
public void realRun() throws InterruptedException { |
800 |
< |
threadsStarted.await(); |
801 |
< |
assertEquals(0, q.remainingCapacity()); |
802 |
< |
assertSame(one, q.take()); |
803 |
< |
}}); |
804 |
< |
|
805 |
< |
joinPool(executor); |
790 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
791 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
792 |
> |
executor.execute(new CheckedRunnable() { |
793 |
> |
public void realRun() throws InterruptedException { |
794 |
> |
assertFalse(q.offer(three)); |
795 |
> |
threadsStarted.await(); |
796 |
> |
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
797 |
> |
assertEquals(0, q.remainingCapacity()); |
798 |
> |
}}); |
799 |
> |
|
800 |
> |
executor.execute(new CheckedRunnable() { |
801 |
> |
public void realRun() throws InterruptedException { |
802 |
> |
threadsStarted.await(); |
803 |
> |
assertEquals(0, q.remainingCapacity()); |
804 |
> |
assertSame(one, q.take()); |
805 |
> |
}}); |
806 |
> |
} |
807 |
|
} |
808 |
|
|
809 |
|
/** |
812 |
|
public void testPollInExecutor() { |
813 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
814 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
815 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
816 |
< |
executor.execute(new CheckedRunnable() { |
817 |
< |
public void realRun() throws InterruptedException { |
818 |
< |
assertNull(q.poll()); |
819 |
< |
threadsStarted.await(); |
820 |
< |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
821 |
< |
checkEmpty(q); |
822 |
< |
}}); |
823 |
< |
|
824 |
< |
executor.execute(new CheckedRunnable() { |
825 |
< |
public void realRun() throws InterruptedException { |
826 |
< |
threadsStarted.await(); |
827 |
< |
q.put(one); |
828 |
< |
}}); |
829 |
< |
|
830 |
< |
joinPool(executor); |
815 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
816 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
817 |
> |
executor.execute(new CheckedRunnable() { |
818 |
> |
public void realRun() throws InterruptedException { |
819 |
> |
assertNull(q.poll()); |
820 |
> |
threadsStarted.await(); |
821 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
822 |
> |
checkEmpty(q); |
823 |
> |
}}); |
824 |
> |
|
825 |
> |
executor.execute(new CheckedRunnable() { |
826 |
> |
public void realRun() throws InterruptedException { |
827 |
> |
threadsStarted.await(); |
828 |
> |
q.put(one); |
829 |
> |
}}); |
830 |
> |
} |
831 |
|
} |
832 |
|
|
833 |
|
/** |
879 |
|
final ArrayBlockingQueue q = populatedQueue(SIZE); |
880 |
|
Thread t = new Thread(new CheckedRunnable() { |
881 |
|
public void realRun() throws InterruptedException { |
882 |
< |
q.put(new Integer(SIZE+1)); |
882 |
> |
q.put(new Integer(SIZE + 1)); |
883 |
|
}}); |
884 |
|
|
885 |
|
t.start(); |
896 |
|
* drainTo(c, n) empties first min(n, size) elements of queue into c |
897 |
|
*/ |
898 |
|
public void testDrainToN() { |
899 |
< |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE*2); |
899 |
> |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE * 2); |
900 |
|
for (int i = 0; i < SIZE + 2; ++i) { |
901 |
|
for (int j = 0; j < SIZE; j++) |
902 |
|
assertTrue(q.offer(new Integer(j))); |
904 |
|
q.drainTo(l, i); |
905 |
|
int k = (i < SIZE) ? i : SIZE; |
906 |
|
assertEquals(k, l.size()); |
907 |
< |
assertEquals(SIZE-k, q.size()); |
907 |
> |
assertEquals(SIZE - k, q.size()); |
908 |
|
for (int j = 0; j < k; ++j) |
909 |
|
assertEquals(l.get(j), new Integer(j)); |
910 |
|
do {} while (q.poll() != null); |