1 |
dl |
1.1 |
/* |
2 |
dl |
1.7 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
|
* Expert Group and released to the public domain, as explained at |
4 |
|
|
* http://creativecommons.org/licenses/publicdomain |
5 |
jsr166 |
1.12 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
|
* Pat Fisher, Mike Judd. |
7 |
dl |
1.1 |
*/ |
8 |
|
|
|
9 |
dl |
1.4 |
|
10 |
dl |
1.1 |
import junit.framework.*; |
11 |
|
|
import java.util.*; |
12 |
|
|
import java.util.concurrent.*; |
13 |
jsr166 |
1.16 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
14 |
dl |
1.2 |
import java.io.*; |
15 |
dl |
1.1 |
|
16 |
dl |
1.3 |
public class ArrayBlockingQueueTest extends JSR166TestCase { |
17 |
jsr166 |
1.32 |
|
18 |
|
|
public static class Fair extends BlockingQueueTest { |
19 |
|
|
protected BlockingQueue emptyCollection() { |
20 |
|
|
return new ArrayBlockingQueue(20, true); |
21 |
|
|
} |
22 |
|
|
} |
23 |
|
|
|
24 |
|
|
public static class NonFair extends BlockingQueueTest { |
25 |
|
|
protected BlockingQueue emptyCollection() { |
26 |
|
|
return new ArrayBlockingQueue(20, false); |
27 |
|
|
} |
28 |
|
|
} |
29 |
|
|
|
30 |
dl |
1.1 |
public static void main(String[] args) { |
31 |
jsr166 |
1.29 |
junit.textui.TestRunner.run(suite()); |
32 |
dl |
1.1 |
} |
33 |
jsr166 |
1.32 |
|
34 |
dl |
1.1 |
public static Test suite() { |
35 |
jsr166 |
1.32 |
return newTestSuite(ArrayBlockingQueueTest.class, |
36 |
|
|
new Fair().testSuite(), |
37 |
|
|
new NonFair().testSuite()); |
38 |
dl |
1.1 |
} |
39 |
|
|
|
40 |
|
|
/** |
41 |
|
|
* Create a queue of given size containing consecutive |
42 |
|
|
* Integers 0 ... n. |
43 |
|
|
*/ |
44 |
dl |
1.3 |
private ArrayBlockingQueue populatedQueue(int n) { |
45 |
dl |
1.1 |
ArrayBlockingQueue q = new ArrayBlockingQueue(n); |
46 |
|
|
assertTrue(q.isEmpty()); |
47 |
jsr166 |
1.15 |
for (int i = 0; i < n; i++) |
48 |
|
|
assertTrue(q.offer(new Integer(i))); |
49 |
dl |
1.1 |
assertFalse(q.isEmpty()); |
50 |
|
|
assertEquals(0, q.remainingCapacity()); |
51 |
jsr166 |
1.15 |
assertEquals(n, q.size()); |
52 |
dl |
1.1 |
return q; |
53 |
|
|
} |
54 |
jsr166 |
1.12 |
|
55 |
dl |
1.4 |
/** |
56 |
dl |
1.5 |
* A new queue has the indicated capacity |
57 |
dl |
1.4 |
*/ |
58 |
|
|
public void testConstructor1() { |
59 |
dl |
1.3 |
assertEquals(SIZE, new ArrayBlockingQueue(SIZE).remainingCapacity()); |
60 |
dl |
1.1 |
} |
61 |
|
|
|
62 |
dl |
1.4 |
/** |
63 |
jsr166 |
1.18 |
* Constructor throws IAE if capacity argument nonpositive |
64 |
dl |
1.4 |
*/ |
65 |
|
|
public void testConstructor2() { |
66 |
dl |
1.1 |
try { |
67 |
|
|
ArrayBlockingQueue q = new ArrayBlockingQueue(0); |
68 |
dl |
1.4 |
shouldThrow(); |
69 |
jsr166 |
1.17 |
} catch (IllegalArgumentException success) {} |
70 |
dl |
1.1 |
} |
71 |
|
|
|
72 |
dl |
1.4 |
/** |
73 |
dl |
1.5 |
* Initializing from null Collection throws NPE |
74 |
dl |
1.4 |
*/ |
75 |
|
|
public void testConstructor3() { |
76 |
dl |
1.1 |
try { |
77 |
|
|
ArrayBlockingQueue q = new ArrayBlockingQueue(1, true, null); |
78 |
dl |
1.4 |
shouldThrow(); |
79 |
jsr166 |
1.17 |
} catch (NullPointerException success) {} |
80 |
dl |
1.1 |
} |
81 |
|
|
|
82 |
dl |
1.4 |
/** |
83 |
dl |
1.5 |
* Initializing from Collection of null elements throws NPE |
84 |
dl |
1.4 |
*/ |
85 |
|
|
public void testConstructor4() { |
86 |
dl |
1.1 |
try { |
87 |
dl |
1.3 |
Integer[] ints = new Integer[SIZE]; |
88 |
|
|
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints)); |
89 |
dl |
1.4 |
shouldThrow(); |
90 |
jsr166 |
1.17 |
} catch (NullPointerException success) {} |
91 |
dl |
1.1 |
} |
92 |
|
|
|
93 |
dl |
1.4 |
/** |
94 |
dl |
1.5 |
* Initializing from Collection with some null elements throws NPE |
95 |
dl |
1.4 |
*/ |
96 |
|
|
public void testConstructor5() { |
97 |
dl |
1.1 |
try { |
98 |
dl |
1.3 |
Integer[] ints = new Integer[SIZE]; |
99 |
|
|
for (int i = 0; i < SIZE-1; ++i) |
100 |
dl |
1.1 |
ints[i] = new Integer(i); |
101 |
dl |
1.3 |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints)); |
102 |
dl |
1.4 |
shouldThrow(); |
103 |
jsr166 |
1.17 |
} catch (NullPointerException success) {} |
104 |
dl |
1.1 |
} |
105 |
|
|
|
106 |
dl |
1.4 |
/** |
107 |
dl |
1.5 |
* Initializing from too large collection throws IAE |
108 |
dl |
1.4 |
*/ |
109 |
|
|
public void testConstructor6() { |
110 |
dl |
1.1 |
try { |
111 |
dl |
1.3 |
Integer[] ints = new Integer[SIZE]; |
112 |
|
|
for (int i = 0; i < SIZE; ++i) |
113 |
dl |
1.1 |
ints[i] = new Integer(i); |
114 |
|
|
ArrayBlockingQueue q = new ArrayBlockingQueue(1, false, Arrays.asList(ints)); |
115 |
dl |
1.4 |
shouldThrow(); |
116 |
jsr166 |
1.17 |
} catch (IllegalArgumentException success) {} |
117 |
dl |
1.1 |
} |
118 |
|
|
|
119 |
dl |
1.4 |
/** |
120 |
dl |
1.5 |
* Queue contains all elements of collection used to initialize |
121 |
dl |
1.4 |
*/ |
122 |
|
|
public void testConstructor7() { |
123 |
jsr166 |
1.19 |
Integer[] ints = new Integer[SIZE]; |
124 |
|
|
for (int i = 0; i < SIZE; ++i) |
125 |
|
|
ints[i] = new Integer(i); |
126 |
|
|
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, true, Arrays.asList(ints)); |
127 |
|
|
for (int i = 0; i < SIZE; ++i) |
128 |
|
|
assertEquals(ints[i], q.poll()); |
129 |
dl |
1.1 |
} |
130 |
|
|
|
131 |
dl |
1.4 |
/** |
132 |
dl |
1.5 |
* Queue transitions from empty to full when elements added |
133 |
dl |
1.4 |
*/ |
134 |
dl |
1.1 |
public void testEmptyFull() { |
135 |
|
|
ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
136 |
|
|
assertTrue(q.isEmpty()); |
137 |
dl |
1.4 |
assertEquals(2, q.remainingCapacity()); |
138 |
dl |
1.3 |
q.add(one); |
139 |
dl |
1.1 |
assertFalse(q.isEmpty()); |
140 |
dl |
1.3 |
q.add(two); |
141 |
dl |
1.1 |
assertFalse(q.isEmpty()); |
142 |
dl |
1.4 |
assertEquals(0, q.remainingCapacity()); |
143 |
|
|
assertFalse(q.offer(three)); |
144 |
dl |
1.1 |
} |
145 |
|
|
|
146 |
dl |
1.4 |
/** |
147 |
dl |
1.5 |
* remainingCapacity decreases on add, increases on remove |
148 |
dl |
1.4 |
*/ |
149 |
|
|
public void testRemainingCapacity() { |
150 |
dl |
1.3 |
ArrayBlockingQueue q = populatedQueue(SIZE); |
151 |
|
|
for (int i = 0; i < SIZE; ++i) { |
152 |
dl |
1.1 |
assertEquals(i, q.remainingCapacity()); |
153 |
dl |
1.3 |
assertEquals(SIZE-i, q.size()); |
154 |
dl |
1.1 |
q.remove(); |
155 |
|
|
} |
156 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
157 |
|
|
assertEquals(SIZE-i, q.remainingCapacity()); |
158 |
dl |
1.1 |
assertEquals(i, q.size()); |
159 |
|
|
q.add(new Integer(i)); |
160 |
|
|
} |
161 |
|
|
} |
162 |
|
|
|
163 |
dl |
1.4 |
/** |
164 |
jsr166 |
1.33 |
* offer(null) throws NPE |
165 |
dl |
1.4 |
*/ |
166 |
|
|
public void testOfferNull() { |
167 |
jsr166 |
1.15 |
try { |
168 |
dl |
1.1 |
ArrayBlockingQueue q = new ArrayBlockingQueue(1); |
169 |
|
|
q.offer(null); |
170 |
dl |
1.4 |
shouldThrow(); |
171 |
jsr166 |
1.17 |
} catch (NullPointerException success) {} |
172 |
dl |
1.1 |
} |
173 |
|
|
|
174 |
dl |
1.4 |
/** |
175 |
jsr166 |
1.33 |
* add(null) throws NPE |
176 |
dl |
1.6 |
*/ |
177 |
|
|
public void testAddNull() { |
178 |
jsr166 |
1.15 |
try { |
179 |
dl |
1.6 |
ArrayBlockingQueue q = new ArrayBlockingQueue(1); |
180 |
|
|
q.add(null); |
181 |
|
|
shouldThrow(); |
182 |
jsr166 |
1.17 |
} catch (NullPointerException success) {} |
183 |
dl |
1.6 |
} |
184 |
|
|
|
185 |
|
|
/** |
186 |
dl |
1.5 |
* Offer succeeds if not full; fails if full |
187 |
dl |
1.4 |
*/ |
188 |
|
|
public void testOffer() { |
189 |
dl |
1.1 |
ArrayBlockingQueue q = new ArrayBlockingQueue(1); |
190 |
dl |
1.3 |
assertTrue(q.offer(zero)); |
191 |
|
|
assertFalse(q.offer(one)); |
192 |
dl |
1.1 |
} |
193 |
|
|
|
194 |
dl |
1.4 |
/** |
195 |
dl |
1.5 |
* add succeeds if not full; throws ISE if full |
196 |
dl |
1.4 |
*/ |
197 |
|
|
public void testAdd() { |
198 |
jsr166 |
1.15 |
try { |
199 |
dl |
1.3 |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
200 |
|
|
for (int i = 0; i < SIZE; ++i) { |
201 |
dl |
1.1 |
assertTrue(q.add(new Integer(i))); |
202 |
|
|
} |
203 |
|
|
assertEquals(0, q.remainingCapacity()); |
204 |
dl |
1.3 |
q.add(new Integer(SIZE)); |
205 |
jsr166 |
1.17 |
shouldThrow(); |
206 |
|
|
} catch (IllegalStateException success) {} |
207 |
dl |
1.1 |
} |
208 |
|
|
|
209 |
dl |
1.4 |
/** |
210 |
jsr166 |
1.33 |
* addAll(null) throws NPE |
211 |
dl |
1.4 |
*/ |
212 |
|
|
public void testAddAll1() { |
213 |
dl |
1.1 |
try { |
214 |
|
|
ArrayBlockingQueue q = new ArrayBlockingQueue(1); |
215 |
|
|
q.addAll(null); |
216 |
dl |
1.4 |
shouldThrow(); |
217 |
jsr166 |
1.17 |
} catch (NullPointerException success) {} |
218 |
dl |
1.1 |
} |
219 |
dl |
1.6 |
|
220 |
|
|
/** |
221 |
|
|
* addAll(this) throws IAE |
222 |
|
|
*/ |
223 |
|
|
public void testAddAllSelf() { |
224 |
|
|
try { |
225 |
|
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
226 |
|
|
q.addAll(q); |
227 |
|
|
shouldThrow(); |
228 |
jsr166 |
1.17 |
} catch (IllegalArgumentException success) {} |
229 |
dl |
1.6 |
} |
230 |
|
|
|
231 |
|
|
|
232 |
dl |
1.4 |
/** |
233 |
jsr166 |
1.33 |
* addAll of a collection with null elements throws NPE |
234 |
dl |
1.4 |
*/ |
235 |
|
|
public void testAddAll2() { |
236 |
dl |
1.1 |
try { |
237 |
dl |
1.3 |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
238 |
|
|
Integer[] ints = new Integer[SIZE]; |
239 |
dl |
1.1 |
q.addAll(Arrays.asList(ints)); |
240 |
dl |
1.4 |
shouldThrow(); |
241 |
jsr166 |
1.17 |
} catch (NullPointerException success) {} |
242 |
dl |
1.1 |
} |
243 |
jsr166 |
1.30 |
|
244 |
dl |
1.4 |
/** |
245 |
dl |
1.5 |
* addAll of a collection with any null elements throws NPE after |
246 |
|
|
* possibly adding some elements |
247 |
dl |
1.4 |
*/ |
248 |
|
|
public void testAddAll3() { |
249 |
dl |
1.1 |
try { |
250 |
dl |
1.3 |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
251 |
|
|
Integer[] ints = new Integer[SIZE]; |
252 |
|
|
for (int i = 0; i < SIZE-1; ++i) |
253 |
dl |
1.1 |
ints[i] = new Integer(i); |
254 |
|
|
q.addAll(Arrays.asList(ints)); |
255 |
dl |
1.4 |
shouldThrow(); |
256 |
jsr166 |
1.17 |
} catch (NullPointerException success) {} |
257 |
dl |
1.1 |
} |
258 |
jsr166 |
1.30 |
|
259 |
dl |
1.4 |
/** |
260 |
dl |
1.5 |
* addAll throws ISE if not enough room |
261 |
dl |
1.4 |
*/ |
262 |
|
|
public void testAddAll4() { |
263 |
dl |
1.1 |
try { |
264 |
|
|
ArrayBlockingQueue q = new ArrayBlockingQueue(1); |
265 |
dl |
1.3 |
Integer[] ints = new Integer[SIZE]; |
266 |
|
|
for (int i = 0; i < SIZE; ++i) |
267 |
dl |
1.1 |
ints[i] = new Integer(i); |
268 |
|
|
q.addAll(Arrays.asList(ints)); |
269 |
dl |
1.4 |
shouldThrow(); |
270 |
jsr166 |
1.17 |
} catch (IllegalStateException success) {} |
271 |
dl |
1.1 |
} |
272 |
jsr166 |
1.30 |
|
273 |
dl |
1.4 |
/** |
274 |
dl |
1.5 |
* Queue contains all elements, in traversal order, of successful addAll |
275 |
dl |
1.4 |
*/ |
276 |
|
|
public void testAddAll5() { |
277 |
jsr166 |
1.17 |
Integer[] empty = new Integer[0]; |
278 |
|
|
Integer[] ints = new Integer[SIZE]; |
279 |
|
|
for (int i = 0; i < SIZE; ++i) |
280 |
|
|
ints[i] = new Integer(i); |
281 |
|
|
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
282 |
|
|
assertFalse(q.addAll(Arrays.asList(empty))); |
283 |
|
|
assertTrue(q.addAll(Arrays.asList(ints))); |
284 |
|
|
for (int i = 0; i < SIZE; ++i) |
285 |
|
|
assertEquals(ints[i], q.poll()); |
286 |
dl |
1.1 |
} |
287 |
|
|
|
288 |
dl |
1.4 |
/** |
289 |
jsr166 |
1.33 |
* put(null) throws NPE |
290 |
dl |
1.4 |
*/ |
291 |
jsr166 |
1.17 |
public void testPutNull() throws InterruptedException { |
292 |
jsr166 |
1.15 |
try { |
293 |
dl |
1.3 |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
294 |
dl |
1.1 |
q.put(null); |
295 |
dl |
1.4 |
shouldThrow(); |
296 |
jsr166 |
1.17 |
} catch (NullPointerException success) {} |
297 |
dl |
1.1 |
} |
298 |
|
|
|
299 |
dl |
1.4 |
/** |
300 |
dl |
1.5 |
* all elements successfully put are contained |
301 |
dl |
1.4 |
*/ |
302 |
jsr166 |
1.17 |
public void testPut() throws InterruptedException { |
303 |
|
|
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
304 |
|
|
for (int i = 0; i < SIZE; ++i) { |
305 |
|
|
Integer I = new Integer(i); |
306 |
|
|
q.put(I); |
307 |
|
|
assertTrue(q.contains(I)); |
308 |
dl |
1.1 |
} |
309 |
jsr166 |
1.17 |
assertEquals(0, q.remainingCapacity()); |
310 |
dl |
1.1 |
} |
311 |
|
|
|
312 |
dl |
1.4 |
/** |
313 |
dl |
1.5 |
* put blocks interruptibly if full |
314 |
dl |
1.4 |
*/ |
315 |
jsr166 |
1.17 |
public void testBlockingPut() throws InterruptedException { |
316 |
dl |
1.10 |
final ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
317 |
jsr166 |
1.17 |
Thread t = new Thread(new CheckedRunnable() { |
318 |
jsr166 |
1.25 |
public void realRun() throws InterruptedException { |
319 |
|
|
for (int i = 0; i < SIZE; ++i) |
320 |
|
|
q.put(i); |
321 |
|
|
assertEquals(SIZE, q.size()); |
322 |
|
|
assertEquals(0, q.remainingCapacity()); |
323 |
jsr166 |
1.17 |
try { |
324 |
jsr166 |
1.25 |
q.put(99); |
325 |
|
|
shouldThrow(); |
326 |
|
|
} catch (InterruptedException success) {} |
327 |
|
|
}}); |
328 |
jsr166 |
1.17 |
|
329 |
|
|
t.start(); |
330 |
jsr166 |
1.25 |
Thread.sleep(SHORT_DELAY_MS); |
331 |
jsr166 |
1.17 |
t.interrupt(); |
332 |
|
|
t.join(); |
333 |
jsr166 |
1.25 |
assertEquals(SIZE, q.size()); |
334 |
|
|
assertEquals(0, q.remainingCapacity()); |
335 |
dl |
1.1 |
} |
336 |
|
|
|
337 |
dl |
1.4 |
/** |
338 |
dl |
1.5 |
* put blocks waiting for take when full |
339 |
dl |
1.4 |
*/ |
340 |
jsr166 |
1.17 |
public void testPutWithTake() throws InterruptedException { |
341 |
jsr166 |
1.27 |
final int capacity = 2; |
342 |
|
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(capacity); |
343 |
jsr166 |
1.17 |
Thread t = new Thread(new CheckedRunnable() { |
344 |
jsr166 |
1.27 |
public void realRun() throws InterruptedException { |
345 |
|
|
for (int i = 0; i < capacity + 1; i++) |
346 |
|
|
q.put(i); |
347 |
jsr166 |
1.17 |
try { |
348 |
jsr166 |
1.27 |
q.put(99); |
349 |
|
|
shouldThrow(); |
350 |
|
|
} catch (InterruptedException success) {} |
351 |
jsr166 |
1.17 |
}}); |
352 |
|
|
|
353 |
|
|
t.start(); |
354 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
355 |
jsr166 |
1.27 |
assertEquals(q.remainingCapacity(), 0); |
356 |
|
|
assertEquals(0, q.take()); |
357 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
358 |
jsr166 |
1.17 |
t.interrupt(); |
359 |
|
|
t.join(); |
360 |
jsr166 |
1.27 |
assertEquals(q.remainingCapacity(), 0); |
361 |
dl |
1.1 |
} |
362 |
|
|
|
363 |
dl |
1.4 |
/** |
364 |
dl |
1.5 |
* timed offer times out if full and elements not taken |
365 |
dl |
1.4 |
*/ |
366 |
jsr166 |
1.17 |
public void testTimedOffer() throws InterruptedException { |
367 |
dl |
1.1 |
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
368 |
jsr166 |
1.24 |
Thread t = new Thread(new CheckedRunnable() { |
369 |
jsr166 |
1.17 |
public void realRun() throws InterruptedException { |
370 |
|
|
q.put(new Object()); |
371 |
|
|
q.put(new Object()); |
372 |
jsr166 |
1.24 |
assertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, MILLISECONDS)); |
373 |
|
|
try { |
374 |
|
|
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
375 |
|
|
shouldThrow(); |
376 |
|
|
} catch (InterruptedException success) {} |
377 |
|
|
}}); |
378 |
jsr166 |
1.17 |
|
379 |
|
|
t.start(); |
380 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
381 |
|
|
t.interrupt(); |
382 |
|
|
t.join(); |
383 |
dl |
1.1 |
} |
384 |
|
|
|
385 |
dl |
1.4 |
/** |
386 |
dl |
1.5 |
* take retrieves elements in FIFO order |
387 |
dl |
1.4 |
*/ |
388 |
jsr166 |
1.17 |
public void testTake() throws InterruptedException { |
389 |
|
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
390 |
|
|
for (int i = 0; i < SIZE; ++i) { |
391 |
jsr166 |
1.25 |
assertEquals(i, q.take()); |
392 |
jsr166 |
1.15 |
} |
393 |
dl |
1.1 |
} |
394 |
|
|
|
395 |
dl |
1.4 |
/** |
396 |
dl |
1.5 |
* Take removes existing elements until empty, then blocks interruptibly |
397 |
dl |
1.4 |
*/ |
398 |
jsr166 |
1.17 |
public void testBlockingTake() throws InterruptedException { |
399 |
jsr166 |
1.25 |
final ArrayBlockingQueue q = populatedQueue(SIZE); |
400 |
|
|
Thread t = new Thread(new CheckedRunnable() { |
401 |
jsr166 |
1.17 |
public void realRun() throws InterruptedException { |
402 |
|
|
for (int i = 0; i < SIZE; ++i) { |
403 |
jsr166 |
1.25 |
assertEquals(i, q.take()); |
404 |
jsr166 |
1.17 |
} |
405 |
jsr166 |
1.25 |
try { |
406 |
|
|
q.take(); |
407 |
|
|
shouldThrow(); |
408 |
|
|
} catch (InterruptedException success) {} |
409 |
|
|
}}); |
410 |
jsr166 |
1.17 |
|
411 |
|
|
t.start(); |
412 |
jsr166 |
1.25 |
Thread.sleep(SHORT_DELAY_MS); |
413 |
|
|
t.interrupt(); |
414 |
|
|
t.join(); |
415 |
dl |
1.1 |
} |
416 |
|
|
|
417 |
|
|
|
418 |
dl |
1.4 |
/** |
419 |
dl |
1.5 |
* poll succeeds unless empty |
420 |
dl |
1.4 |
*/ |
421 |
|
|
public void testPoll() { |
422 |
dl |
1.3 |
ArrayBlockingQueue q = populatedQueue(SIZE); |
423 |
|
|
for (int i = 0; i < SIZE; ++i) { |
424 |
jsr166 |
1.25 |
assertEquals(i, q.poll()); |
425 |
dl |
1.1 |
} |
426 |
jsr166 |
1.15 |
assertNull(q.poll()); |
427 |
dl |
1.1 |
} |
428 |
|
|
|
429 |
dl |
1.4 |
/** |
430 |
jsr166 |
1.37 |
* timed poll with zero timeout succeeds when non-empty, else times out |
431 |
dl |
1.4 |
*/ |
432 |
jsr166 |
1.17 |
public void testTimedPoll0() throws InterruptedException { |
433 |
|
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
434 |
|
|
for (int i = 0; i < SIZE; ++i) { |
435 |
jsr166 |
1.25 |
assertEquals(i, q.poll(0, MILLISECONDS)); |
436 |
jsr166 |
1.15 |
} |
437 |
jsr166 |
1.17 |
assertNull(q.poll(0, MILLISECONDS)); |
438 |
dl |
1.1 |
} |
439 |
|
|
|
440 |
dl |
1.4 |
/** |
441 |
jsr166 |
1.37 |
* timed poll with nonzero timeout succeeds when non-empty, else times out |
442 |
dl |
1.4 |
*/ |
443 |
jsr166 |
1.17 |
public void testTimedPoll() throws InterruptedException { |
444 |
|
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
445 |
|
|
for (int i = 0; i < SIZE; ++i) { |
446 |
jsr166 |
1.25 |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
447 |
jsr166 |
1.15 |
} |
448 |
jsr166 |
1.17 |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
449 |
dl |
1.1 |
} |
450 |
|
|
|
451 |
dl |
1.4 |
/** |
452 |
dl |
1.5 |
* Interrupted timed poll throws InterruptedException instead of |
453 |
|
|
* returning timeout status |
454 |
dl |
1.4 |
*/ |
455 |
jsr166 |
1.17 |
public void testInterruptedTimedPoll() throws InterruptedException { |
456 |
jsr166 |
1.21 |
Thread t = new Thread(new CheckedRunnable() { |
457 |
jsr166 |
1.17 |
public void realRun() throws InterruptedException { |
458 |
|
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
459 |
|
|
for (int i = 0; i < SIZE; ++i) { |
460 |
jsr166 |
1.25 |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));; |
461 |
jsr166 |
1.17 |
} |
462 |
jsr166 |
1.21 |
try { |
463 |
|
|
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
464 |
jsr166 |
1.22 |
shouldThrow(); |
465 |
jsr166 |
1.21 |
} catch (InterruptedException success) {} |
466 |
|
|
}}); |
467 |
jsr166 |
1.17 |
|
468 |
|
|
t.start(); |
469 |
jsr166 |
1.21 |
Thread.sleep(SHORT_DELAY_MS); |
470 |
jsr166 |
1.17 |
t.interrupt(); |
471 |
|
|
t.join(); |
472 |
dl |
1.1 |
} |
473 |
|
|
|
474 |
dl |
1.4 |
/** |
475 |
dl |
1.5 |
* peek returns next element, or null if empty |
476 |
dl |
1.4 |
*/ |
477 |
|
|
public void testPeek() { |
478 |
dl |
1.3 |
ArrayBlockingQueue q = populatedQueue(SIZE); |
479 |
|
|
for (int i = 0; i < SIZE; ++i) { |
480 |
jsr166 |
1.26 |
assertEquals(i, q.peek()); |
481 |
|
|
assertEquals(i, q.poll()); |
482 |
dl |
1.1 |
assertTrue(q.peek() == null || |
483 |
jsr166 |
1.26 |
!q.peek().equals(i)); |
484 |
dl |
1.1 |
} |
485 |
jsr166 |
1.15 |
assertNull(q.peek()); |
486 |
dl |
1.1 |
} |
487 |
|
|
|
488 |
dl |
1.4 |
/** |
489 |
dl |
1.5 |
* element returns next element, or throws NSEE if empty |
490 |
dl |
1.4 |
*/ |
491 |
|
|
public void testElement() { |
492 |
dl |
1.3 |
ArrayBlockingQueue q = populatedQueue(SIZE); |
493 |
|
|
for (int i = 0; i < SIZE; ++i) { |
494 |
jsr166 |
1.26 |
assertEquals(i, q.element()); |
495 |
|
|
assertEquals(i, q.poll()); |
496 |
dl |
1.1 |
} |
497 |
|
|
try { |
498 |
|
|
q.element(); |
499 |
dl |
1.4 |
shouldThrow(); |
500 |
jsr166 |
1.17 |
} catch (NoSuchElementException success) {} |
501 |
dl |
1.1 |
} |
502 |
|
|
|
503 |
dl |
1.4 |
/** |
504 |
dl |
1.5 |
* remove removes next element, or throws NSEE if empty |
505 |
dl |
1.4 |
*/ |
506 |
|
|
public void testRemove() { |
507 |
dl |
1.3 |
ArrayBlockingQueue q = populatedQueue(SIZE); |
508 |
|
|
for (int i = 0; i < SIZE; ++i) { |
509 |
jsr166 |
1.26 |
assertEquals(i, q.remove()); |
510 |
dl |
1.1 |
} |
511 |
|
|
try { |
512 |
|
|
q.remove(); |
513 |
dl |
1.4 |
shouldThrow(); |
514 |
jsr166 |
1.17 |
} catch (NoSuchElementException success) {} |
515 |
dl |
1.1 |
} |
516 |
|
|
|
517 |
dl |
1.4 |
/** |
518 |
dl |
1.5 |
* remove(x) removes x and returns true if present |
519 |
dl |
1.4 |
*/ |
520 |
|
|
public void testRemoveElement() { |
521 |
dl |
1.3 |
ArrayBlockingQueue q = populatedQueue(SIZE); |
522 |
|
|
for (int i = 1; i < SIZE; i+=2) { |
523 |
dl |
1.1 |
assertTrue(q.remove(new Integer(i))); |
524 |
|
|
} |
525 |
dl |
1.3 |
for (int i = 0; i < SIZE; i+=2) { |
526 |
dl |
1.1 |
assertTrue(q.remove(new Integer(i))); |
527 |
|
|
assertFalse(q.remove(new Integer(i+1))); |
528 |
|
|
} |
529 |
dl |
1.2 |
assertTrue(q.isEmpty()); |
530 |
dl |
1.1 |
} |
531 |
jsr166 |
1.12 |
|
532 |
dl |
1.4 |
/** |
533 |
dl |
1.5 |
* contains(x) reports true when elements added but not yet removed |
534 |
dl |
1.4 |
*/ |
535 |
|
|
public void testContains() { |
536 |
dl |
1.3 |
ArrayBlockingQueue q = populatedQueue(SIZE); |
537 |
|
|
for (int i = 0; i < SIZE; ++i) { |
538 |
dl |
1.1 |
assertTrue(q.contains(new Integer(i))); |
539 |
jsr166 |
1.26 |
assertEquals(i, q.poll()); |
540 |
dl |
1.1 |
assertFalse(q.contains(new Integer(i))); |
541 |
|
|
} |
542 |
|
|
} |
543 |
|
|
|
544 |
dl |
1.4 |
/** |
545 |
dl |
1.5 |
* clear removes all elements |
546 |
dl |
1.4 |
*/ |
547 |
|
|
public void testClear() { |
548 |
dl |
1.3 |
ArrayBlockingQueue q = populatedQueue(SIZE); |
549 |
dl |
1.1 |
q.clear(); |
550 |
|
|
assertTrue(q.isEmpty()); |
551 |
|
|
assertEquals(0, q.size()); |
552 |
dl |
1.3 |
assertEquals(SIZE, q.remainingCapacity()); |
553 |
|
|
q.add(one); |
554 |
dl |
1.1 |
assertFalse(q.isEmpty()); |
555 |
dl |
1.11 |
assertTrue(q.contains(one)); |
556 |
dl |
1.1 |
q.clear(); |
557 |
|
|
assertTrue(q.isEmpty()); |
558 |
|
|
} |
559 |
|
|
|
560 |
dl |
1.4 |
/** |
561 |
dl |
1.5 |
* containsAll(c) is true when c contains a subset of elements |
562 |
dl |
1.4 |
*/ |
563 |
|
|
public void testContainsAll() { |
564 |
dl |
1.3 |
ArrayBlockingQueue q = populatedQueue(SIZE); |
565 |
|
|
ArrayBlockingQueue p = new ArrayBlockingQueue(SIZE); |
566 |
|
|
for (int i = 0; i < SIZE; ++i) { |
567 |
dl |
1.1 |
assertTrue(q.containsAll(p)); |
568 |
|
|
assertFalse(p.containsAll(q)); |
569 |
|
|
p.add(new Integer(i)); |
570 |
|
|
} |
571 |
|
|
assertTrue(p.containsAll(q)); |
572 |
|
|
} |
573 |
|
|
|
574 |
dl |
1.4 |
/** |
575 |
dl |
1.5 |
* retainAll(c) retains only those elements of c and reports true if changed |
576 |
dl |
1.4 |
*/ |
577 |
|
|
public void testRetainAll() { |
578 |
dl |
1.3 |
ArrayBlockingQueue q = populatedQueue(SIZE); |
579 |
|
|
ArrayBlockingQueue p = populatedQueue(SIZE); |
580 |
|
|
for (int i = 0; i < SIZE; ++i) { |
581 |
dl |
1.1 |
boolean changed = q.retainAll(p); |
582 |
|
|
if (i == 0) |
583 |
|
|
assertFalse(changed); |
584 |
|
|
else |
585 |
|
|
assertTrue(changed); |
586 |
|
|
|
587 |
|
|
assertTrue(q.containsAll(p)); |
588 |
dl |
1.3 |
assertEquals(SIZE-i, q.size()); |
589 |
dl |
1.1 |
p.remove(); |
590 |
|
|
} |
591 |
|
|
} |
592 |
|
|
|
593 |
dl |
1.4 |
/** |
594 |
dl |
1.5 |
* removeAll(c) removes only those elements of c and reports true if changed |
595 |
dl |
1.4 |
*/ |
596 |
|
|
public void testRemoveAll() { |
597 |
dl |
1.3 |
for (int i = 1; i < SIZE; ++i) { |
598 |
|
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
599 |
|
|
ArrayBlockingQueue p = populatedQueue(i); |
600 |
dl |
1.1 |
assertTrue(q.removeAll(p)); |
601 |
dl |
1.3 |
assertEquals(SIZE-i, q.size()); |
602 |
dl |
1.1 |
for (int j = 0; j < i; ++j) { |
603 |
|
|
Integer I = (Integer)(p.remove()); |
604 |
|
|
assertFalse(q.contains(I)); |
605 |
|
|
} |
606 |
|
|
} |
607 |
|
|
} |
608 |
|
|
|
609 |
dl |
1.4 |
/** |
610 |
jsr166 |
1.40 |
* toArray contains all elements in FIFO order |
611 |
dl |
1.4 |
*/ |
612 |
jsr166 |
1.40 |
public void testToArray() { |
613 |
dl |
1.3 |
ArrayBlockingQueue q = populatedQueue(SIZE); |
614 |
jsr166 |
1.15 |
Object[] o = q.toArray(); |
615 |
|
|
for (int i = 0; i < o.length; i++) |
616 |
jsr166 |
1.40 |
assertSame(o[i], q.poll()); |
617 |
dl |
1.1 |
} |
618 |
|
|
|
619 |
dl |
1.4 |
/** |
620 |
jsr166 |
1.40 |
* toArray(a) contains all elements in FIFO order |
621 |
dl |
1.4 |
*/ |
622 |
jsr166 |
1.40 |
public void testToArray2() { |
623 |
dl |
1.3 |
ArrayBlockingQueue q = populatedQueue(SIZE); |
624 |
jsr166 |
1.15 |
Integer[] ints = new Integer[SIZE]; |
625 |
jsr166 |
1.40 |
assertSame(ints, q.toArray(ints)); |
626 |
jsr166 |
1.17 |
for (int i = 0; i < ints.length; i++) |
627 |
jsr166 |
1.40 |
assertSame(ints[i], q.poll()); |
628 |
dl |
1.1 |
} |
629 |
dl |
1.6 |
|
630 |
|
|
/** |
631 |
jsr166 |
1.39 |
* toArray(null) throws NullPointerException |
632 |
dl |
1.6 |
*/ |
633 |
jsr166 |
1.39 |
public void testToArray_NullArg() { |
634 |
jsr166 |
1.26 |
ArrayBlockingQueue q = populatedQueue(SIZE); |
635 |
jsr166 |
1.15 |
try { |
636 |
jsr166 |
1.39 |
q.toArray(null); |
637 |
jsr166 |
1.15 |
shouldThrow(); |
638 |
|
|
} catch (NullPointerException success) {} |
639 |
dl |
1.6 |
} |
640 |
|
|
|
641 |
|
|
/** |
642 |
jsr166 |
1.38 |
* toArray(incompatible array type) throws ArrayStoreException |
643 |
dl |
1.6 |
*/ |
644 |
|
|
public void testToArray1_BadArg() { |
645 |
jsr166 |
1.26 |
ArrayBlockingQueue q = populatedQueue(SIZE); |
646 |
jsr166 |
1.15 |
try { |
647 |
jsr166 |
1.38 |
q.toArray(new String[10]); |
648 |
jsr166 |
1.15 |
shouldThrow(); |
649 |
jsr166 |
1.18 |
} catch (ArrayStoreException success) {} |
650 |
dl |
1.6 |
} |
651 |
|
|
|
652 |
jsr166 |
1.12 |
|
653 |
dl |
1.4 |
/** |
654 |
dl |
1.5 |
* iterator iterates through all elements |
655 |
dl |
1.4 |
*/ |
656 |
jsr166 |
1.17 |
public void testIterator() throws InterruptedException { |
657 |
dl |
1.3 |
ArrayBlockingQueue q = populatedQueue(SIZE); |
658 |
jsr166 |
1.15 |
Iterator it = q.iterator(); |
659 |
jsr166 |
1.17 |
while (it.hasNext()) { |
660 |
|
|
assertEquals(it.next(), q.take()); |
661 |
jsr166 |
1.15 |
} |
662 |
dl |
1.1 |
} |
663 |
|
|
|
664 |
dl |
1.4 |
/** |
665 |
dl |
1.5 |
* iterator.remove removes current element |
666 |
|
|
*/ |
667 |
jsr166 |
1.29 |
public void testIteratorRemove() { |
668 |
dl |
1.5 |
final ArrayBlockingQueue q = new ArrayBlockingQueue(3); |
669 |
|
|
q.add(two); |
670 |
|
|
q.add(one); |
671 |
|
|
q.add(three); |
672 |
|
|
|
673 |
|
|
Iterator it = q.iterator(); |
674 |
|
|
it.next(); |
675 |
|
|
it.remove(); |
676 |
jsr166 |
1.12 |
|
677 |
dl |
1.5 |
it = q.iterator(); |
678 |
jsr166 |
1.28 |
assertSame(it.next(), one); |
679 |
|
|
assertSame(it.next(), three); |
680 |
dl |
1.5 |
assertFalse(it.hasNext()); |
681 |
|
|
} |
682 |
|
|
|
683 |
|
|
/** |
684 |
|
|
* iterator ordering is FIFO |
685 |
dl |
1.4 |
*/ |
686 |
dl |
1.1 |
public void testIteratorOrdering() { |
687 |
|
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(3); |
688 |
dl |
1.3 |
q.add(one); |
689 |
|
|
q.add(two); |
690 |
|
|
q.add(three); |
691 |
dl |
1.1 |
|
692 |
|
|
assertEquals("queue should be full", 0, q.remainingCapacity()); |
693 |
|
|
|
694 |
|
|
int k = 0; |
695 |
|
|
for (Iterator it = q.iterator(); it.hasNext();) { |
696 |
jsr166 |
1.26 |
assertEquals(++k, it.next()); |
697 |
dl |
1.1 |
} |
698 |
dl |
1.4 |
assertEquals(3, k); |
699 |
dl |
1.1 |
} |
700 |
|
|
|
701 |
dl |
1.4 |
/** |
702 |
dl |
1.5 |
* Modifications do not cause iterators to fail |
703 |
dl |
1.4 |
*/ |
704 |
jsr166 |
1.29 |
public void testWeaklyConsistentIteration() { |
705 |
dl |
1.1 |
final ArrayBlockingQueue q = new ArrayBlockingQueue(3); |
706 |
dl |
1.3 |
q.add(one); |
707 |
|
|
q.add(two); |
708 |
|
|
q.add(three); |
709 |
jsr166 |
1.17 |
for (Iterator it = q.iterator(); it.hasNext();) { |
710 |
|
|
q.remove(); |
711 |
|
|
it.next(); |
712 |
dl |
1.1 |
} |
713 |
dl |
1.4 |
assertEquals(0, q.size()); |
714 |
dl |
1.1 |
} |
715 |
|
|
|
716 |
|
|
|
717 |
dl |
1.4 |
/** |
718 |
dl |
1.5 |
* toString contains toStrings of elements |
719 |
dl |
1.4 |
*/ |
720 |
|
|
public void testToString() { |
721 |
dl |
1.3 |
ArrayBlockingQueue q = populatedQueue(SIZE); |
722 |
dl |
1.1 |
String s = q.toString(); |
723 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
724 |
dl |
1.1 |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
725 |
|
|
} |
726 |
jsr166 |
1.12 |
} |
727 |
dl |
1.1 |
|
728 |
|
|
|
729 |
dl |
1.4 |
/** |
730 |
dl |
1.5 |
* offer transfers elements across Executor tasks |
731 |
dl |
1.4 |
*/ |
732 |
dl |
1.1 |
public void testOfferInExecutor() { |
733 |
|
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
734 |
dl |
1.3 |
q.add(one); |
735 |
|
|
q.add(two); |
736 |
dl |
1.1 |
ExecutorService executor = Executors.newFixedThreadPool(2); |
737 |
jsr166 |
1.17 |
executor.execute(new CheckedRunnable() { |
738 |
|
|
public void realRun() throws InterruptedException { |
739 |
jsr166 |
1.27 |
assertFalse(q.offer(three)); |
740 |
|
|
assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
741 |
|
|
assertEquals(0, q.remainingCapacity()); |
742 |
jsr166 |
1.17 |
}}); |
743 |
|
|
|
744 |
|
|
executor.execute(new CheckedRunnable() { |
745 |
|
|
public void realRun() throws InterruptedException { |
746 |
|
|
Thread.sleep(SMALL_DELAY_MS); |
747 |
jsr166 |
1.27 |
assertSame(one, q.take()); |
748 |
jsr166 |
1.17 |
}}); |
749 |
jsr166 |
1.12 |
|
750 |
dl |
1.3 |
joinPool(executor); |
751 |
dl |
1.1 |
} |
752 |
|
|
|
753 |
dl |
1.4 |
/** |
754 |
dl |
1.5 |
* poll retrieves elements across Executor threads |
755 |
dl |
1.4 |
*/ |
756 |
dl |
1.1 |
public void testPollInExecutor() { |
757 |
|
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
758 |
|
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
759 |
jsr166 |
1.17 |
executor.execute(new CheckedRunnable() { |
760 |
|
|
public void realRun() throws InterruptedException { |
761 |
jsr166 |
1.27 |
assertNull(q.poll()); |
762 |
|
|
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
763 |
|
|
assertTrue(q.isEmpty()); |
764 |
jsr166 |
1.17 |
}}); |
765 |
|
|
|
766 |
|
|
executor.execute(new CheckedRunnable() { |
767 |
|
|
public void realRun() throws InterruptedException { |
768 |
|
|
Thread.sleep(SMALL_DELAY_MS); |
769 |
|
|
q.put(one); |
770 |
|
|
}}); |
771 |
jsr166 |
1.12 |
|
772 |
dl |
1.3 |
joinPool(executor); |
773 |
dl |
1.1 |
} |
774 |
dl |
1.2 |
|
775 |
dl |
1.4 |
/** |
776 |
dl |
1.5 |
* A deserialized serialized queue has same elements in same order |
777 |
dl |
1.4 |
*/ |
778 |
jsr166 |
1.17 |
public void testSerialization() throws Exception { |
779 |
dl |
1.3 |
ArrayBlockingQueue q = populatedQueue(SIZE); |
780 |
dl |
1.2 |
|
781 |
jsr166 |
1.17 |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
782 |
|
|
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
783 |
|
|
out.writeObject(q); |
784 |
|
|
out.close(); |
785 |
|
|
|
786 |
|
|
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
787 |
|
|
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
788 |
|
|
ArrayBlockingQueue r = (ArrayBlockingQueue)in.readObject(); |
789 |
|
|
assertEquals(q.size(), r.size()); |
790 |
|
|
while (!q.isEmpty()) |
791 |
|
|
assertEquals(q.remove(), r.remove()); |
792 |
dl |
1.6 |
} |
793 |
|
|
|
794 |
|
|
/** |
795 |
|
|
* drainTo(null) throws NPE |
796 |
jsr166 |
1.12 |
*/ |
797 |
dl |
1.6 |
public void testDrainToNull() { |
798 |
|
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
799 |
|
|
try { |
800 |
|
|
q.drainTo(null); |
801 |
|
|
shouldThrow(); |
802 |
jsr166 |
1.17 |
} catch (NullPointerException success) {} |
803 |
dl |
1.6 |
} |
804 |
|
|
|
805 |
|
|
/** |
806 |
|
|
* drainTo(this) throws IAE |
807 |
jsr166 |
1.12 |
*/ |
808 |
dl |
1.6 |
public void testDrainToSelf() { |
809 |
|
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
810 |
|
|
try { |
811 |
|
|
q.drainTo(q); |
812 |
|
|
shouldThrow(); |
813 |
jsr166 |
1.17 |
} catch (IllegalArgumentException success) {} |
814 |
dl |
1.6 |
} |
815 |
|
|
|
816 |
|
|
/** |
817 |
|
|
* drainTo(c) empties queue into another collection c |
818 |
jsr166 |
1.12 |
*/ |
819 |
dl |
1.6 |
public void testDrainTo() { |
820 |
|
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
821 |
|
|
ArrayList l = new ArrayList(); |
822 |
|
|
q.drainTo(l); |
823 |
|
|
assertEquals(q.size(), 0); |
824 |
|
|
assertEquals(l.size(), SIZE); |
825 |
jsr166 |
1.12 |
for (int i = 0; i < SIZE; ++i) |
826 |
dl |
1.6 |
assertEquals(l.get(i), new Integer(i)); |
827 |
dl |
1.11 |
q.add(zero); |
828 |
|
|
q.add(one); |
829 |
|
|
assertFalse(q.isEmpty()); |
830 |
|
|
assertTrue(q.contains(zero)); |
831 |
|
|
assertTrue(q.contains(one)); |
832 |
|
|
l.clear(); |
833 |
|
|
q.drainTo(l); |
834 |
|
|
assertEquals(q.size(), 0); |
835 |
|
|
assertEquals(l.size(), 2); |
836 |
jsr166 |
1.12 |
for (int i = 0; i < 2; ++i) |
837 |
dl |
1.11 |
assertEquals(l.get(i), new Integer(i)); |
838 |
dl |
1.6 |
} |
839 |
|
|
|
840 |
|
|
/** |
841 |
|
|
* drainTo empties full queue, unblocking a waiting put. |
842 |
jsr166 |
1.12 |
*/ |
843 |
jsr166 |
1.17 |
public void testDrainToWithActivePut() throws InterruptedException { |
844 |
dl |
1.6 |
final ArrayBlockingQueue q = populatedQueue(SIZE); |
845 |
jsr166 |
1.17 |
Thread t = new Thread(new CheckedRunnable() { |
846 |
|
|
public void realRun() throws InterruptedException { |
847 |
|
|
q.put(new Integer(SIZE+1)); |
848 |
|
|
}}); |
849 |
|
|
|
850 |
|
|
t.start(); |
851 |
|
|
ArrayList l = new ArrayList(); |
852 |
|
|
q.drainTo(l); |
853 |
|
|
assertTrue(l.size() >= SIZE); |
854 |
|
|
for (int i = 0; i < SIZE; ++i) |
855 |
|
|
assertEquals(l.get(i), new Integer(i)); |
856 |
|
|
t.join(); |
857 |
|
|
assertTrue(q.size() + l.size() >= SIZE); |
858 |
dl |
1.6 |
} |
859 |
|
|
|
860 |
|
|
/** |
861 |
|
|
* drainTo(null, n) throws NPE |
862 |
jsr166 |
1.12 |
*/ |
863 |
dl |
1.6 |
public void testDrainToNullN() { |
864 |
|
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
865 |
|
|
try { |
866 |
|
|
q.drainTo(null, 0); |
867 |
|
|
shouldThrow(); |
868 |
jsr166 |
1.17 |
} catch (NullPointerException success) {} |
869 |
dl |
1.6 |
} |
870 |
|
|
|
871 |
|
|
/** |
872 |
|
|
* drainTo(this, n) throws IAE |
873 |
jsr166 |
1.12 |
*/ |
874 |
dl |
1.6 |
public void testDrainToSelfN() { |
875 |
|
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
876 |
|
|
try { |
877 |
|
|
q.drainTo(q, 0); |
878 |
|
|
shouldThrow(); |
879 |
jsr166 |
1.17 |
} catch (IllegalArgumentException success) {} |
880 |
dl |
1.6 |
} |
881 |
|
|
|
882 |
|
|
/** |
883 |
jsr166 |
1.34 |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
884 |
jsr166 |
1.12 |
*/ |
885 |
dl |
1.6 |
public void testDrainToN() { |
886 |
dl |
1.11 |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE*2); |
887 |
dl |
1.6 |
for (int i = 0; i < SIZE + 2; ++i) { |
888 |
jsr166 |
1.13 |
for (int j = 0; j < SIZE; j++) |
889 |
dl |
1.11 |
assertTrue(q.offer(new Integer(j))); |
890 |
dl |
1.6 |
ArrayList l = new ArrayList(); |
891 |
|
|
q.drainTo(l, i); |
892 |
jsr166 |
1.35 |
int k = (i < SIZE) ? i : SIZE; |
893 |
dl |
1.11 |
assertEquals(l.size(), k); |
894 |
dl |
1.6 |
assertEquals(q.size(), SIZE-k); |
895 |
jsr166 |
1.12 |
for (int j = 0; j < k; ++j) |
896 |
dl |
1.6 |
assertEquals(l.get(j), new Integer(j)); |
897 |
dl |
1.11 |
while (q.poll() != null) ; |
898 |
dl |
1.2 |
} |
899 |
|
|
} |
900 |
|
|
|
901 |
dl |
1.1 |
} |