7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
10 |
< |
import java.util.*; |
11 |
< |
import java.util.concurrent.*; |
10 |
> |
import java.util.Arrays; |
11 |
> |
import java.util.ArrayList; |
12 |
> |
import java.util.Collection; |
13 |
> |
import java.util.Iterator; |
14 |
> |
import java.util.NoSuchElementException; |
15 |
> |
import java.util.Queue; |
16 |
> |
import java.util.concurrent.BlockingQueue; |
17 |
> |
import java.util.concurrent.CountDownLatch; |
18 |
> |
import java.util.concurrent.LinkedBlockingQueue; |
19 |
> |
import java.util.concurrent.Executors; |
20 |
> |
import java.util.concurrent.ExecutorService; |
21 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
13 |
– |
import java.io.*; |
22 |
|
|
23 |
|
public class LinkedBlockingQueueTest extends JSR166TestCase { |
24 |
|
|
30 |
|
|
31 |
|
public static class Bounded extends BlockingQueueTest { |
32 |
|
protected BlockingQueue emptyCollection() { |
33 |
< |
return new LinkedBlockingQueue(20); |
33 |
> |
return new LinkedBlockingQueue(SIZE); |
34 |
|
} |
35 |
|
} |
36 |
|
|
45 |
|
} |
46 |
|
|
47 |
|
/** |
48 |
< |
* Create a queue of given size containing consecutive |
48 |
> |
* Returns a new queue of given size containing consecutive |
49 |
|
* Integers 0 ... n. |
50 |
|
*/ |
51 |
|
private LinkedBlockingQueue<Integer> populatedQueue(int n) { |
70 |
|
} |
71 |
|
|
72 |
|
/** |
73 |
< |
* Constructor throws IAE if capacity argument nonpositive |
73 |
> |
* Constructor throws IllegalArgumentException if capacity argument nonpositive |
74 |
|
*/ |
75 |
|
public void testConstructor2() { |
76 |
|
try { |
77 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(0); |
77 |
> |
new LinkedBlockingQueue(0); |
78 |
|
shouldThrow(); |
79 |
|
} catch (IllegalArgumentException success) {} |
80 |
|
} |
81 |
|
|
82 |
|
/** |
83 |
< |
* Initializing from null Collection throws NPE |
83 |
> |
* Initializing from null Collection throws NullPointerException |
84 |
|
*/ |
85 |
|
public void testConstructor3() { |
86 |
|
try { |
87 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(null); |
87 |
> |
new LinkedBlockingQueue(null); |
88 |
|
shouldThrow(); |
89 |
|
} catch (NullPointerException success) {} |
90 |
|
} |
91 |
|
|
92 |
|
/** |
93 |
< |
* Initializing from Collection of null elements throws NPE |
93 |
> |
* Initializing from Collection of null elements throws NullPointerException |
94 |
|
*/ |
95 |
|
public void testConstructor4() { |
96 |
+ |
Collection<Integer> elements = Arrays.asList(new Integer[SIZE]); |
97 |
|
try { |
98 |
< |
Integer[] ints = new Integer[SIZE]; |
90 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints)); |
98 |
> |
new LinkedBlockingQueue(elements); |
99 |
|
shouldThrow(); |
100 |
|
} catch (NullPointerException success) {} |
101 |
|
} |
102 |
|
|
103 |
|
/** |
104 |
< |
* Initializing from Collection with some null elements throws NPE |
104 |
> |
* Initializing from Collection with some null elements throws |
105 |
> |
* NullPointerException |
106 |
|
*/ |
107 |
|
public void testConstructor5() { |
108 |
+ |
Integer[] ints = new Integer[SIZE]; |
109 |
+ |
for (int i = 0; i < SIZE-1; ++i) |
110 |
+ |
ints[i] = new Integer(i); |
111 |
+ |
Collection<Integer> elements = Arrays.asList(ints); |
112 |
|
try { |
113 |
< |
Integer[] ints = new Integer[SIZE]; |
101 |
< |
for (int i = 0; i < SIZE-1; ++i) |
102 |
< |
ints[i] = new Integer(i); |
103 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(Arrays.asList(ints)); |
113 |
> |
new LinkedBlockingQueue(elements); |
114 |
|
shouldThrow(); |
115 |
|
} catch (NullPointerException success) {} |
116 |
|
} |
160 |
|
} |
161 |
|
|
162 |
|
/** |
153 |
– |
* offer(null) throws NPE |
154 |
– |
*/ |
155 |
– |
public void testOfferNull() { |
156 |
– |
try { |
157 |
– |
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
158 |
– |
q.offer(null); |
159 |
– |
shouldThrow(); |
160 |
– |
} catch (NullPointerException success) {} |
161 |
– |
} |
162 |
– |
|
163 |
– |
/** |
164 |
– |
* add(null) throws NPE |
165 |
– |
*/ |
166 |
– |
public void testAddNull() { |
167 |
– |
try { |
168 |
– |
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
169 |
– |
q.add(null); |
170 |
– |
shouldThrow(); |
171 |
– |
} catch (NullPointerException success) {} |
172 |
– |
} |
173 |
– |
|
174 |
– |
/** |
163 |
|
* Offer succeeds if not full; fails if full |
164 |
|
*/ |
165 |
|
public void testOffer() { |
169 |
|
} |
170 |
|
|
171 |
|
/** |
172 |
< |
* add succeeds if not full; throws ISE if full |
172 |
> |
* add succeeds if not full; throws IllegalStateException if full |
173 |
|
*/ |
174 |
|
public void testAdd() { |
175 |
+ |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
176 |
+ |
for (int i = 0; i < SIZE; ++i) |
177 |
+ |
assertTrue(q.add(new Integer(i))); |
178 |
+ |
assertEquals(0, q.remainingCapacity()); |
179 |
|
try { |
188 |
– |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
189 |
– |
for (int i = 0; i < SIZE; ++i) { |
190 |
– |
assertTrue(q.add(new Integer(i))); |
191 |
– |
} |
192 |
– |
assertEquals(0, q.remainingCapacity()); |
180 |
|
q.add(new Integer(SIZE)); |
181 |
|
shouldThrow(); |
182 |
|
} catch (IllegalStateException success) {} |
183 |
|
} |
184 |
|
|
185 |
|
/** |
186 |
< |
* addAll(null) throws NPE |
200 |
< |
*/ |
201 |
< |
public void testAddAll1() { |
202 |
< |
try { |
203 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
204 |
< |
q.addAll(null); |
205 |
< |
shouldThrow(); |
206 |
< |
} catch (NullPointerException success) {} |
207 |
< |
} |
208 |
< |
|
209 |
< |
/** |
210 |
< |
* addAll(this) throws IAE |
186 |
> |
* addAll(this) throws IllegalArgumentException |
187 |
|
*/ |
188 |
|
public void testAddAllSelf() { |
189 |
+ |
LinkedBlockingQueue q = populatedQueue(SIZE); |
190 |
|
try { |
214 |
– |
LinkedBlockingQueue q = populatedQueue(SIZE); |
191 |
|
q.addAll(q); |
192 |
|
shouldThrow(); |
193 |
|
} catch (IllegalArgumentException success) {} |
194 |
|
} |
195 |
|
|
196 |
|
/** |
221 |
– |
* addAll of a collection with null elements throws NPE |
222 |
– |
*/ |
223 |
– |
public void testAddAll2() { |
224 |
– |
try { |
225 |
– |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
226 |
– |
Integer[] ints = new Integer[SIZE]; |
227 |
– |
q.addAll(Arrays.asList(ints)); |
228 |
– |
shouldThrow(); |
229 |
– |
} catch (NullPointerException success) {} |
230 |
– |
} |
231 |
– |
|
232 |
– |
/** |
197 |
|
* addAll of a collection with any null elements throws NPE after |
198 |
|
* possibly adding some elements |
199 |
|
*/ |
200 |
|
public void testAddAll3() { |
201 |
+ |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
202 |
+ |
Integer[] ints = new Integer[SIZE]; |
203 |
+ |
for (int i = 0; i < SIZE-1; ++i) |
204 |
+ |
ints[i] = new Integer(i); |
205 |
+ |
Collection<Integer> elements = Arrays.asList(ints); |
206 |
|
try { |
207 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
239 |
< |
Integer[] ints = new Integer[SIZE]; |
240 |
< |
for (int i = 0; i < SIZE-1; ++i) |
241 |
< |
ints[i] = new Integer(i); |
242 |
< |
q.addAll(Arrays.asList(ints)); |
207 |
> |
q.addAll(elements); |
208 |
|
shouldThrow(); |
209 |
|
} catch (NullPointerException success) {} |
210 |
|
} |
211 |
|
|
212 |
|
/** |
213 |
< |
* addAll throws ISE if not enough room |
213 |
> |
* addAll throws IllegalStateException if not enough room |
214 |
|
*/ |
215 |
|
public void testAddAll4() { |
216 |
+ |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE - 1); |
217 |
+ |
Integer[] ints = new Integer[SIZE]; |
218 |
+ |
for (int i = 0; i < SIZE; ++i) |
219 |
+ |
ints[i] = new Integer(i); |
220 |
+ |
Collection<Integer> elements = Arrays.asList(ints); |
221 |
|
try { |
222 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
253 |
< |
Integer[] ints = new Integer[SIZE]; |
254 |
< |
for (int i = 0; i < SIZE; ++i) |
255 |
< |
ints[i] = new Integer(i); |
256 |
< |
q.addAll(Arrays.asList(ints)); |
222 |
> |
q.addAll(elements); |
223 |
|
shouldThrow(); |
224 |
|
} catch (IllegalStateException success) {} |
225 |
|
} |
240 |
|
} |
241 |
|
|
242 |
|
/** |
277 |
– |
* put(null) throws NPE |
278 |
– |
*/ |
279 |
– |
public void testPutNull() throws InterruptedException { |
280 |
– |
try { |
281 |
– |
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
282 |
– |
q.put(null); |
283 |
– |
shouldThrow(); |
284 |
– |
} catch (NullPointerException success) {} |
285 |
– |
} |
286 |
– |
|
287 |
– |
/** |
243 |
|
* all elements successfully put are contained |
244 |
|
*/ |
245 |
|
public void testPut() throws InterruptedException { |
312 |
|
}}); |
313 |
|
|
314 |
|
await(pleaseTake); |
315 |
< |
assertEquals(q.remainingCapacity(), 0); |
315 |
> |
assertEquals(0, q.remainingCapacity()); |
316 |
|
assertEquals(0, q.take()); |
317 |
|
|
318 |
|
await(pleaseInterrupt); |
319 |
|
assertThreadStaysAlive(t); |
320 |
|
t.interrupt(); |
321 |
|
awaitTermination(t); |
322 |
< |
assertEquals(q.remainingCapacity(), 0); |
322 |
> |
assertEquals(0, q.remainingCapacity()); |
323 |
|
} |
324 |
|
|
325 |
|
/** |
504 |
|
} |
505 |
|
|
506 |
|
/** |
552 |
– |
* remove(x) removes x and returns true if present |
553 |
– |
*/ |
554 |
– |
public void testRemoveElement() { |
555 |
– |
LinkedBlockingQueue q = populatedQueue(SIZE); |
556 |
– |
for (int i = 1; i < SIZE; i+=2) { |
557 |
– |
assertTrue(q.contains(i)); |
558 |
– |
assertTrue(q.remove(i)); |
559 |
– |
assertFalse(q.contains(i)); |
560 |
– |
assertTrue(q.contains(i-1)); |
561 |
– |
} |
562 |
– |
for (int i = 0; i < SIZE; i+=2) { |
563 |
– |
assertTrue(q.contains(i)); |
564 |
– |
assertTrue(q.remove(i)); |
565 |
– |
assertFalse(q.contains(i)); |
566 |
– |
assertFalse(q.remove(i+1)); |
567 |
– |
assertFalse(q.contains(i+1)); |
568 |
– |
} |
569 |
– |
assertTrue(q.isEmpty()); |
570 |
– |
} |
571 |
– |
|
572 |
– |
/** |
507 |
|
* An add following remove(x) succeeds |
508 |
|
*/ |
509 |
|
public void testRemoveElementAndAdd() throws InterruptedException { |
513 |
|
assertTrue(q.remove(new Integer(1))); |
514 |
|
assertTrue(q.remove(new Integer(2))); |
515 |
|
assertTrue(q.add(new Integer(3))); |
516 |
< |
assertTrue(q.take() != null); |
516 |
> |
assertNotNull(q.take()); |
517 |
|
} |
518 |
|
|
519 |
|
/** |
616 |
|
} |
617 |
|
|
618 |
|
/** |
685 |
– |
* toArray(null) throws NullPointerException |
686 |
– |
*/ |
687 |
– |
public void testToArray_NullArg() { |
688 |
– |
LinkedBlockingQueue q = populatedQueue(SIZE); |
689 |
– |
try { |
690 |
– |
q.toArray(null); |
691 |
– |
shouldThrow(); |
692 |
– |
} catch (NullPointerException success) {} |
693 |
– |
} |
694 |
– |
|
695 |
– |
/** |
619 |
|
* toArray(incompatible array type) throws ArrayStoreException |
620 |
|
*/ |
621 |
|
public void testToArray1_BadArg() { |
752 |
|
* A deserialized serialized queue has same elements in same order |
753 |
|
*/ |
754 |
|
public void testSerialization() throws Exception { |
755 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
756 |
< |
|
834 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
835 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
836 |
< |
out.writeObject(q); |
837 |
< |
out.close(); |
838 |
< |
|
839 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
840 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
841 |
< |
LinkedBlockingQueue r = (LinkedBlockingQueue)in.readObject(); |
842 |
< |
assertEquals(q.size(), r.size()); |
843 |
< |
while (!q.isEmpty()) |
844 |
< |
assertEquals(q.remove(), r.remove()); |
845 |
< |
} |
846 |
< |
|
847 |
< |
/** |
848 |
< |
* drainTo(null) throws NPE |
849 |
< |
*/ |
850 |
< |
public void testDrainToNull() { |
851 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
852 |
< |
try { |
853 |
< |
q.drainTo(null); |
854 |
< |
shouldThrow(); |
855 |
< |
} catch (NullPointerException success) {} |
856 |
< |
} |
755 |
> |
Queue x = populatedQueue(SIZE); |
756 |
> |
Queue y = serialClone(x); |
757 |
|
|
758 |
< |
/** |
759 |
< |
* drainTo(this) throws IAE |
760 |
< |
*/ |
761 |
< |
public void testDrainToSelf() { |
762 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
763 |
< |
try { |
764 |
< |
q.drainTo(q); |
765 |
< |
shouldThrow(); |
766 |
< |
} catch (IllegalArgumentException success) {} |
758 |
> |
assertNotSame(x, y); |
759 |
> |
assertEquals(x.size(), y.size()); |
760 |
> |
assertEquals(x.toString(), y.toString()); |
761 |
> |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
762 |
> |
while (!x.isEmpty()) { |
763 |
> |
assertFalse(y.isEmpty()); |
764 |
> |
assertEquals(x.remove(), y.remove()); |
765 |
> |
} |
766 |
> |
assertTrue(y.isEmpty()); |
767 |
|
} |
768 |
|
|
769 |
|
/** |
773 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
774 |
|
ArrayList l = new ArrayList(); |
775 |
|
q.drainTo(l); |
776 |
< |
assertEquals(q.size(), 0); |
777 |
< |
assertEquals(l.size(), SIZE); |
776 |
> |
assertEquals(0, q.size()); |
777 |
> |
assertEquals(SIZE, l.size()); |
778 |
|
for (int i = 0; i < SIZE; ++i) |
779 |
|
assertEquals(l.get(i), new Integer(i)); |
780 |
|
q.add(zero); |
784 |
|
assertTrue(q.contains(one)); |
785 |
|
l.clear(); |
786 |
|
q.drainTo(l); |
787 |
< |
assertEquals(q.size(), 0); |
788 |
< |
assertEquals(l.size(), 2); |
787 |
> |
assertEquals(0, q.size()); |
788 |
> |
assertEquals(2, l.size()); |
789 |
|
for (int i = 0; i < 2; ++i) |
790 |
|
assertEquals(l.get(i), new Integer(i)); |
791 |
|
} |
811 |
|
} |
812 |
|
|
813 |
|
/** |
914 |
– |
* drainTo(null, n) throws NPE |
915 |
– |
*/ |
916 |
– |
public void testDrainToNullN() { |
917 |
– |
LinkedBlockingQueue q = populatedQueue(SIZE); |
918 |
– |
try { |
919 |
– |
q.drainTo(null, 0); |
920 |
– |
shouldThrow(); |
921 |
– |
} catch (NullPointerException success) {} |
922 |
– |
} |
923 |
– |
|
924 |
– |
/** |
925 |
– |
* drainTo(this, n) throws IAE |
926 |
– |
*/ |
927 |
– |
public void testDrainToSelfN() { |
928 |
– |
LinkedBlockingQueue q = populatedQueue(SIZE); |
929 |
– |
try { |
930 |
– |
q.drainTo(q, 0); |
931 |
– |
shouldThrow(); |
932 |
– |
} catch (IllegalArgumentException success) {} |
933 |
– |
} |
934 |
– |
|
935 |
– |
/** |
814 |
|
* drainTo(c, n) empties first min(n, size) elements of queue into c |
815 |
|
*/ |
816 |
|
public void testDrainToN() { |
821 |
|
ArrayList l = new ArrayList(); |
822 |
|
q.drainTo(l, i); |
823 |
|
int k = (i < SIZE) ? i : SIZE; |
824 |
< |
assertEquals(l.size(), k); |
825 |
< |
assertEquals(q.size(), SIZE-k); |
824 |
> |
assertEquals(k, l.size()); |
825 |
> |
assertEquals(SIZE-k, q.size()); |
826 |
|
for (int j = 0; j < k; ++j) |
827 |
|
assertEquals(l.get(j), new Integer(j)); |
828 |
|
while (q.poll() != null) ; |