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 |
> |
|
23 |
> |
import junit.framework.Test; |
24 |
|
|
25 |
|
public class ArrayBlockingQueueTest extends JSR166TestCase { |
26 |
|
|
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() { |
47 |
|
} |
48 |
|
|
49 |
|
/** |
50 |
< |
* Create a queue of given size containing consecutive |
51 |
< |
* Integers 0 ... n. |
50 |
> |
* Returns a new queue of given size containing consecutive |
51 |
> |
* Integers 0 ... n - 1. |
52 |
|
*/ |
53 |
|
private ArrayBlockingQueue<Integer> populatedQueue(int n) { |
54 |
|
ArrayBlockingQueue<Integer> q = new ArrayBlockingQueue<Integer>(n); |
58 |
|
assertFalse(q.isEmpty()); |
59 |
|
assertEquals(0, q.remainingCapacity()); |
60 |
|
assertEquals(n, q.size()); |
61 |
+ |
assertEquals((Integer) 0, q.peek()); |
62 |
|
return q; |
63 |
|
} |
64 |
|
|
105 |
|
*/ |
106 |
|
public void testConstructor5() { |
107 |
|
Integer[] ints = new Integer[SIZE]; |
108 |
< |
for (int i = 0; i < SIZE-1; ++i) |
108 |
> |
for (int i = 0; i < SIZE - 1; ++i) |
109 |
|
ints[i] = i; |
110 |
|
Collection<Integer> elements = Arrays.asList(ints); |
111 |
|
try { |
112 |
< |
new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints)); |
112 |
> |
new ArrayBlockingQueue(SIZE, false, elements); |
113 |
|
shouldThrow(); |
114 |
|
} catch (NullPointerException success) {} |
115 |
|
} |
160 |
|
* remainingCapacity decreases on add, increases on remove |
161 |
|
*/ |
162 |
|
public void testRemainingCapacity() { |
163 |
< |
ArrayBlockingQueue q = populatedQueue(SIZE); |
163 |
> |
BlockingQueue q = populatedQueue(SIZE); |
164 |
|
for (int i = 0; i < SIZE; ++i) { |
165 |
|
assertEquals(i, q.remainingCapacity()); |
166 |
< |
assertEquals(SIZE-i, q.size()); |
167 |
< |
q.remove(); |
166 |
> |
assertEquals(SIZE, q.size() + q.remainingCapacity()); |
167 |
> |
assertEquals(i, q.remove()); |
168 |
|
} |
169 |
|
for (int i = 0; i < SIZE; ++i) { |
170 |
< |
assertEquals(SIZE-i, q.remainingCapacity()); |
171 |
< |
assertEquals(i, q.size()); |
172 |
< |
q.add(new Integer(i)); |
170 |
> |
assertEquals(SIZE - i, q.remainingCapacity()); |
171 |
> |
assertEquals(SIZE, q.size() + q.remainingCapacity()); |
172 |
> |
assertTrue(q.add(i)); |
173 |
|
} |
174 |
|
} |
175 |
|
|
186 |
|
* add succeeds if not full; throws ISE if full |
187 |
|
*/ |
188 |
|
public void testAdd() { |
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 |
|
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()); |
195 |
|
q.add(new Integer(SIZE)); |
196 |
|
shouldThrow(); |
197 |
|
} catch (IllegalStateException success) {} |
201 |
|
* addAll(this) throws IAE |
202 |
|
*/ |
203 |
|
public void testAddAllSelf() { |
204 |
+ |
ArrayBlockingQueue q = populatedQueue(SIZE); |
205 |
|
try { |
202 |
– |
ArrayBlockingQueue q = populatedQueue(SIZE); |
206 |
|
q.addAll(q); |
207 |
|
shouldThrow(); |
208 |
|
} catch (IllegalArgumentException success) {} |
213 |
|
* possibly adding some elements |
214 |
|
*/ |
215 |
|
public void testAddAll3() { |
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 |
|
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); |
221 |
|
q.addAll(Arrays.asList(ints)); |
222 |
|
shouldThrow(); |
223 |
|
} catch (NullPointerException success) {} |
227 |
|
* addAll throws ISE if not enough room |
228 |
|
*/ |
229 |
|
public void testAddAll4() { |
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 |
|
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); |
235 |
|
q.addAll(Arrays.asList(ints)); |
236 |
|
shouldThrow(); |
237 |
|
} catch (IllegalStateException success) {} |
258 |
|
public void testPut() throws InterruptedException { |
259 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
260 |
|
for (int i = 0; i < SIZE; ++i) { |
261 |
< |
Integer I = new Integer(i); |
262 |
< |
q.put(I); |
263 |
< |
assertTrue(q.contains(I)); |
261 |
> |
Integer x = new Integer(i); |
262 |
> |
q.put(x); |
263 |
> |
assertTrue(q.contains(x)); |
264 |
|
} |
265 |
|
assertEquals(0, q.remainingCapacity()); |
266 |
|
} |
452 |
|
final CountDownLatch aboutToWait = new CountDownLatch(1); |
453 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
454 |
|
public void realRun() throws InterruptedException { |
455 |
+ |
long startTime = System.nanoTime(); |
456 |
|
for (int i = 0; i < SIZE; ++i) { |
453 |
– |
long t0 = System.nanoTime(); |
457 |
|
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
455 |
– |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
458 |
|
} |
457 |
– |
long t0 = System.nanoTime(); |
459 |
|
aboutToWait.countDown(); |
460 |
|
try { |
461 |
< |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
461 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
462 |
|
shouldThrow(); |
463 |
|
} catch (InterruptedException success) { |
464 |
< |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
464 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
465 |
|
} |
466 |
|
}}); |
467 |
|
|
468 |
< |
aboutToWait.await(); |
469 |
< |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
468 |
> |
await(aboutToWait); |
469 |
> |
waitForThreadToEnterWaitState(t); |
470 |
|
t.interrupt(); |
471 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
471 |
> |
awaitTermination(t); |
472 |
|
checkEmpty(q); |
473 |
|
} |
474 |
|
|
571 |
|
assertTrue(changed); |
572 |
|
|
573 |
|
assertTrue(q.containsAll(p)); |
574 |
< |
assertEquals(SIZE-i, q.size()); |
574 |
> |
assertEquals(SIZE - i, q.size()); |
575 |
|
p.remove(); |
576 |
|
} |
577 |
|
} |
584 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
585 |
|
ArrayBlockingQueue p = populatedQueue(i); |
586 |
|
assertTrue(q.removeAll(p)); |
587 |
< |
assertEquals(SIZE-i, q.size()); |
587 |
> |
assertEquals(SIZE - i, q.size()); |
588 |
|
for (int j = 0; j < i; ++j) { |
589 |
< |
Integer I = (Integer)(p.remove()); |
590 |
< |
assertFalse(q.contains(I)); |
589 |
> |
Integer x = (Integer)(p.remove()); |
590 |
> |
assertFalse(q.contains(x)); |
591 |
|
} |
592 |
|
} |
593 |
|
} |
594 |
|
|
595 |
+ |
void checkToArray(ArrayBlockingQueue q) { |
596 |
+ |
int size = q.size(); |
597 |
+ |
Object[] o = q.toArray(); |
598 |
+ |
assertEquals(size, o.length); |
599 |
+ |
Iterator it = q.iterator(); |
600 |
+ |
for (int i = 0; i < size; i++) { |
601 |
+ |
Integer x = (Integer) it.next(); |
602 |
+ |
assertEquals((Integer)o[0] + i, (int) x); |
603 |
+ |
assertSame(o[i], x); |
604 |
+ |
} |
605 |
+ |
} |
606 |
+ |
|
607 |
|
/** |
608 |
< |
* toArray contains all elements in FIFO order |
608 |
> |
* toArray() contains all elements in FIFO order |
609 |
|
*/ |
610 |
|
public void testToArray() { |
611 |
< |
ArrayBlockingQueue q = populatedQueue(SIZE); |
612 |
< |
Object[] o = q.toArray(); |
613 |
< |
for (int i = 0; i < o.length; i++) |
614 |
< |
assertSame(o[i], q.poll()); |
611 |
> |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
612 |
> |
for (int i = 0; i < SIZE; i++) { |
613 |
> |
checkToArray(q); |
614 |
> |
q.add(i); |
615 |
> |
} |
616 |
> |
// Provoke wraparound |
617 |
> |
for (int i = 0; i < SIZE; i++) { |
618 |
> |
checkToArray(q); |
619 |
> |
assertEquals(i, q.poll()); |
620 |
> |
checkToArray(q); |
621 |
> |
q.add(SIZE + i); |
622 |
> |
} |
623 |
> |
for (int i = 0; i < SIZE; i++) { |
624 |
> |
checkToArray(q); |
625 |
> |
assertEquals(SIZE + i, q.poll()); |
626 |
> |
} |
627 |
> |
} |
628 |
> |
|
629 |
> |
void checkToArray2(ArrayBlockingQueue q) { |
630 |
> |
int size = q.size(); |
631 |
> |
Integer[] a1 = (size == 0) ? null : new Integer[size - 1]; |
632 |
> |
Integer[] a2 = new Integer[size]; |
633 |
> |
Integer[] a3 = new Integer[size + 2]; |
634 |
> |
if (size > 0) Arrays.fill(a1, 42); |
635 |
> |
Arrays.fill(a2, 42); |
636 |
> |
Arrays.fill(a3, 42); |
637 |
> |
Integer[] b1 = (size == 0) ? null : (Integer[]) q.toArray(a1); |
638 |
> |
Integer[] b2 = (Integer[]) q.toArray(a2); |
639 |
> |
Integer[] b3 = (Integer[]) q.toArray(a3); |
640 |
> |
assertSame(a2, b2); |
641 |
> |
assertSame(a3, b3); |
642 |
> |
Iterator it = q.iterator(); |
643 |
> |
for (int i = 0; i < size; i++) { |
644 |
> |
Integer x = (Integer) it.next(); |
645 |
> |
assertSame(b1[i], x); |
646 |
> |
assertEquals(b1[0] + i, (int) x); |
647 |
> |
assertSame(b2[i], x); |
648 |
> |
assertSame(b3[i], x); |
649 |
> |
} |
650 |
> |
assertNull(a3[size]); |
651 |
> |
assertEquals(42, (int) a3[size + 1]); |
652 |
> |
if (size > 0) { |
653 |
> |
assertNotSame(a1, b1); |
654 |
> |
assertEquals(size, b1.length); |
655 |
> |
for (int i = 0; i < a1.length; i++) { |
656 |
> |
assertEquals(42, (int) a1[i]); |
657 |
> |
} |
658 |
> |
} |
659 |
|
} |
660 |
|
|
661 |
|
/** |
662 |
|
* toArray(a) contains all elements in FIFO order |
663 |
|
*/ |
664 |
|
public void testToArray2() { |
665 |
< |
ArrayBlockingQueue<Integer> q = populatedQueue(SIZE); |
666 |
< |
Integer[] ints = new Integer[SIZE]; |
667 |
< |
Integer[] array = q.toArray(ints); |
668 |
< |
assertSame(ints, array); |
669 |
< |
for (int i = 0; i < ints.length; i++) |
670 |
< |
assertSame(ints[i], q.poll()); |
665 |
> |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
666 |
> |
for (int i = 0; i < SIZE; i++) { |
667 |
> |
checkToArray2(q); |
668 |
> |
q.add(i); |
669 |
> |
} |
670 |
> |
// Provoke wraparound |
671 |
> |
for (int i = 0; i < SIZE; i++) { |
672 |
> |
checkToArray2(q); |
673 |
> |
assertEquals(i, q.poll()); |
674 |
> |
checkToArray2(q); |
675 |
> |
q.add(SIZE + i); |
676 |
> |
} |
677 |
> |
for (int i = 0; i < SIZE; i++) { |
678 |
> |
checkToArray2(q); |
679 |
> |
assertEquals(SIZE + i, q.poll()); |
680 |
> |
} |
681 |
|
} |
682 |
|
|
683 |
|
/** |
697 |
|
public void testIterator() throws InterruptedException { |
698 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
699 |
|
Iterator it = q.iterator(); |
700 |
< |
while (it.hasNext()) { |
700 |
> |
int i; |
701 |
> |
for (i = 0; it.hasNext(); i++) |
702 |
> |
assertTrue(q.contains(it.next())); |
703 |
> |
assertEquals(i, SIZE); |
704 |
> |
assertIteratorExhausted(it); |
705 |
> |
|
706 |
> |
it = q.iterator(); |
707 |
> |
for (i = 0; it.hasNext(); i++) |
708 |
|
assertEquals(it.next(), q.take()); |
709 |
< |
} |
709 |
> |
assertEquals(i, SIZE); |
710 |
> |
assertIteratorExhausted(it); |
711 |
> |
} |
712 |
> |
|
713 |
> |
/** |
714 |
> |
* iterator of empty collection has no elements |
715 |
> |
*/ |
716 |
> |
public void testEmptyIterator() { |
717 |
> |
assertIteratorExhausted(new ArrayBlockingQueue(SIZE).iterator()); |
718 |
|
} |
719 |
|
|
720 |
|
/** |
787 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
788 |
|
q.add(one); |
789 |
|
q.add(two); |
708 |
– |
ExecutorService executor = Executors.newFixedThreadPool(2); |
790 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
791 |
< |
executor.execute(new CheckedRunnable() { |
792 |
< |
public void realRun() throws InterruptedException { |
793 |
< |
assertFalse(q.offer(three)); |
794 |
< |
threadsStarted.await(); |
795 |
< |
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
796 |
< |
assertEquals(0, q.remainingCapacity()); |
797 |
< |
}}); |
798 |
< |
|
799 |
< |
executor.execute(new CheckedRunnable() { |
800 |
< |
public void realRun() throws InterruptedException { |
801 |
< |
threadsStarted.await(); |
802 |
< |
assertEquals(0, q.remainingCapacity()); |
803 |
< |
assertSame(one, q.take()); |
804 |
< |
}}); |
805 |
< |
|
806 |
< |
joinPool(executor); |
791 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
792 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
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 |
|
} |
809 |
|
|
810 |
|
/** |
813 |
|
public void testPollInExecutor() { |
814 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
815 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
816 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
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 |
< |
joinPool(executor); |
816 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
817 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
818 |
> |
executor.execute(new CheckedRunnable() { |
819 |
> |
public void realRun() throws InterruptedException { |
820 |
> |
assertNull(q.poll()); |
821 |
> |
threadsStarted.await(); |
822 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
823 |
> |
checkEmpty(q); |
824 |
> |
}}); |
825 |
> |
|
826 |
> |
executor.execute(new CheckedRunnable() { |
827 |
> |
public void realRun() throws InterruptedException { |
828 |
> |
threadsStarted.await(); |
829 |
> |
q.put(one); |
830 |
> |
}}); |
831 |
> |
} |
832 |
|
} |
833 |
|
|
834 |
|
/** |
838 |
|
Queue x = populatedQueue(SIZE); |
839 |
|
Queue y = serialClone(x); |
840 |
|
|
841 |
< |
assertTrue(x != y); |
841 |
> |
assertNotSame(x, y); |
842 |
|
assertEquals(x.size(), y.size()); |
843 |
|
assertEquals(x.toString(), y.toString()); |
844 |
|
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
880 |
|
final ArrayBlockingQueue q = populatedQueue(SIZE); |
881 |
|
Thread t = new Thread(new CheckedRunnable() { |
882 |
|
public void realRun() throws InterruptedException { |
883 |
< |
q.put(new Integer(SIZE+1)); |
883 |
> |
q.put(new Integer(SIZE + 1)); |
884 |
|
}}); |
885 |
|
|
886 |
|
t.start(); |
897 |
|
* drainTo(c, n) empties first min(n, size) elements of queue into c |
898 |
|
*/ |
899 |
|
public void testDrainToN() { |
900 |
< |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE*2); |
900 |
> |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE * 2); |
901 |
|
for (int i = 0; i < SIZE + 2; ++i) { |
902 |
|
for (int j = 0; j < SIZE; j++) |
903 |
|
assertTrue(q.offer(new Integer(j))); |
904 |
|
ArrayList l = new ArrayList(); |
905 |
|
q.drainTo(l, i); |
906 |
|
int k = (i < SIZE) ? i : SIZE; |
907 |
< |
assertEquals(l.size(), k); |
908 |
< |
assertEquals(q.size(), SIZE-k); |
907 |
> |
assertEquals(k, l.size()); |
908 |
> |
assertEquals(SIZE - k, q.size()); |
909 |
|
for (int j = 0; j < k; ++j) |
910 |
|
assertEquals(l.get(j), new Integer(j)); |
911 |
< |
while (q.poll() != null) ; |
911 |
> |
do {} while (q.poll() != null); |
912 |
|
} |
913 |
|
} |
914 |
|
|
915 |
+ |
/** |
916 |
+ |
* remove(null), contains(null) always return false |
917 |
+ |
*/ |
918 |
+ |
public void testNeverContainsNull() { |
919 |
+ |
Collection<?>[] qs = { |
920 |
+ |
new ArrayBlockingQueue<Object>(10), |
921 |
+ |
populatedQueue(2), |
922 |
+ |
}; |
923 |
+ |
|
924 |
+ |
for (Collection<?> q : qs) { |
925 |
+ |
assertFalse(q.contains(null)); |
926 |
+ |
assertFalse(q.remove(null)); |
927 |
+ |
} |
928 |
+ |
} |
929 |
|
} |