2 |
|
* 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 |
< |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
< |
* Pat Fisher, Mike Judd. |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
|
10 |
|
import junit.framework.*; |
11 |
|
import java.util.*; |
12 |
|
import java.util.concurrent.*; |
13 |
+ |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
14 |
|
import java.io.*; |
15 |
|
|
16 |
|
public class ArrayBlockingQueueTest extends JSR166TestCase { |
17 |
+ |
|
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 |
|
public static void main(String[] args) { |
31 |
< |
junit.textui.TestRunner.run (suite()); |
31 |
> |
junit.textui.TestRunner.run(suite()); |
32 |
|
} |
33 |
+ |
|
34 |
|
public static Test suite() { |
35 |
< |
return new TestSuite(ArrayBlockingQueueTest.class); |
35 |
> |
return newTestSuite(ArrayBlockingQueueTest.class, |
36 |
> |
new Fair().testSuite(), |
37 |
> |
new NonFair().testSuite()); |
38 |
|
} |
39 |
|
|
40 |
|
/** |
41 |
|
* Create a queue of given size containing consecutive |
42 |
|
* Integers 0 ... n. |
43 |
|
*/ |
44 |
< |
private ArrayBlockingQueue populatedQueue(int n) { |
45 |
< |
ArrayBlockingQueue q = new ArrayBlockingQueue(n); |
44 |
> |
private ArrayBlockingQueue<Integer> populatedQueue(int n) { |
45 |
> |
ArrayBlockingQueue<Integer> q = new ArrayBlockingQueue<Integer>(n); |
46 |
|
assertTrue(q.isEmpty()); |
47 |
< |
for(int i = 0; i < n; i++) |
48 |
< |
assertTrue(q.offer(new Integer(i))); |
47 |
> |
for (int i = 0; i < n; i++) |
48 |
> |
assertTrue(q.offer(new Integer(i))); |
49 |
|
assertFalse(q.isEmpty()); |
50 |
|
assertEquals(0, q.remainingCapacity()); |
51 |
< |
assertEquals(n, q.size()); |
51 |
> |
assertEquals(n, q.size()); |
52 |
|
return q; |
53 |
|
} |
54 |
< |
|
54 |
> |
|
55 |
|
/** |
56 |
|
* A new queue has the indicated capacity |
57 |
|
*/ |
60 |
|
} |
61 |
|
|
62 |
|
/** |
63 |
< |
* Constructor throws IAE if capacity argument nonpositive |
63 |
> |
* Constructor throws IAE if capacity argument nonpositive |
64 |
|
*/ |
65 |
|
public void testConstructor2() { |
66 |
|
try { |
67 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(0); |
68 |
|
shouldThrow(); |
69 |
< |
} |
53 |
< |
catch (IllegalArgumentException success) {} |
69 |
> |
} catch (IllegalArgumentException success) {} |
70 |
|
} |
71 |
|
|
72 |
|
/** |
76 |
|
try { |
77 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(1, true, null); |
78 |
|
shouldThrow(); |
79 |
< |
} |
64 |
< |
catch (NullPointerException success) {} |
79 |
> |
} catch (NullPointerException success) {} |
80 |
|
} |
81 |
|
|
82 |
|
/** |
87 |
|
Integer[] ints = new Integer[SIZE]; |
88 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints)); |
89 |
|
shouldThrow(); |
90 |
< |
} |
76 |
< |
catch (NullPointerException success) {} |
90 |
> |
} catch (NullPointerException success) {} |
91 |
|
} |
92 |
|
|
93 |
|
/** |
100 |
|
ints[i] = new Integer(i); |
101 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints)); |
102 |
|
shouldThrow(); |
103 |
< |
} |
90 |
< |
catch (NullPointerException success) {} |
103 |
> |
} catch (NullPointerException success) {} |
104 |
|
} |
105 |
|
|
106 |
|
/** |
113 |
|
ints[i] = new Integer(i); |
114 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(1, false, Arrays.asList(ints)); |
115 |
|
shouldThrow(); |
116 |
< |
} |
104 |
< |
catch (IllegalArgumentException success) {} |
116 |
> |
} catch (IllegalArgumentException success) {} |
117 |
|
} |
118 |
|
|
119 |
|
/** |
120 |
|
* Queue contains all elements of collection used to initialize |
121 |
|
*/ |
122 |
|
public void testConstructor7() { |
123 |
< |
try { |
124 |
< |
Integer[] ints = new Integer[SIZE]; |
125 |
< |
for (int i = 0; i < SIZE; ++i) |
126 |
< |
ints[i] = new Integer(i); |
127 |
< |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, true, Arrays.asList(ints)); |
128 |
< |
for (int i = 0; i < SIZE; ++i) |
117 |
< |
assertEquals(ints[i], q.poll()); |
118 |
< |
} |
119 |
< |
finally {} |
123 |
> |
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 |
|
} |
130 |
|
|
131 |
|
/** |
161 |
|
} |
162 |
|
|
163 |
|
/** |
164 |
< |
* offer(null) throws NPE |
164 |
> |
* offer(null) throws NPE |
165 |
|
*/ |
166 |
|
public void testOfferNull() { |
167 |
< |
try { |
167 |
> |
try { |
168 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(1); |
169 |
|
q.offer(null); |
170 |
|
shouldThrow(); |
171 |
< |
} catch (NullPointerException success) { } |
171 |
> |
} catch (NullPointerException success) {} |
172 |
|
} |
173 |
|
|
174 |
|
/** |
175 |
< |
* add(null) throws NPE |
175 |
> |
* add(null) throws NPE |
176 |
|
*/ |
177 |
|
public void testAddNull() { |
178 |
< |
try { |
178 |
> |
try { |
179 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(1); |
180 |
|
q.add(null); |
181 |
|
shouldThrow(); |
182 |
< |
} catch (NullPointerException success) { } |
182 |
> |
} catch (NullPointerException success) {} |
183 |
|
} |
184 |
|
|
185 |
|
/** |
195 |
|
* add succeeds if not full; throws ISE if full |
196 |
|
*/ |
197 |
|
public void testAdd() { |
198 |
< |
try { |
198 |
> |
try { |
199 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
200 |
|
for (int i = 0; i < SIZE; ++i) { |
201 |
|
assertTrue(q.add(new Integer(i))); |
202 |
|
} |
203 |
|
assertEquals(0, q.remainingCapacity()); |
204 |
|
q.add(new Integer(SIZE)); |
205 |
< |
} catch (IllegalStateException success){ |
206 |
< |
} |
205 |
> |
shouldThrow(); |
206 |
> |
} catch (IllegalStateException success) {} |
207 |
|
} |
208 |
|
|
209 |
|
/** |
210 |
< |
* addAll(null) throws NPE |
210 |
> |
* addAll(null) throws NPE |
211 |
|
*/ |
212 |
|
public void testAddAll1() { |
213 |
|
try { |
214 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(1); |
215 |
|
q.addAll(null); |
216 |
|
shouldThrow(); |
217 |
< |
} |
209 |
< |
catch (NullPointerException success) {} |
217 |
> |
} catch (NullPointerException success) {} |
218 |
|
} |
219 |
|
|
220 |
|
/** |
225 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
226 |
|
q.addAll(q); |
227 |
|
shouldThrow(); |
228 |
< |
} |
221 |
< |
catch (IllegalArgumentException success) {} |
228 |
> |
} catch (IllegalArgumentException success) {} |
229 |
|
} |
230 |
|
|
231 |
|
|
232 |
|
/** |
233 |
< |
* addAll of a collection with null elements throws NPE |
233 |
> |
* addAll of a collection with null elements throws NPE |
234 |
|
*/ |
235 |
|
public void testAddAll2() { |
236 |
|
try { |
238 |
|
Integer[] ints = new Integer[SIZE]; |
239 |
|
q.addAll(Arrays.asList(ints)); |
240 |
|
shouldThrow(); |
241 |
< |
} |
235 |
< |
catch (NullPointerException success) {} |
241 |
> |
} catch (NullPointerException success) {} |
242 |
|
} |
243 |
+ |
|
244 |
|
/** |
245 |
|
* addAll of a collection with any null elements throws NPE after |
246 |
|
* possibly adding some elements |
253 |
|
ints[i] = new Integer(i); |
254 |
|
q.addAll(Arrays.asList(ints)); |
255 |
|
shouldThrow(); |
256 |
< |
} |
250 |
< |
catch (NullPointerException success) {} |
256 |
> |
} catch (NullPointerException success) {} |
257 |
|
} |
258 |
+ |
|
259 |
|
/** |
260 |
|
* addAll throws ISE if not enough room |
261 |
|
*/ |
267 |
|
ints[i] = new Integer(i); |
268 |
|
q.addAll(Arrays.asList(ints)); |
269 |
|
shouldThrow(); |
270 |
< |
} |
264 |
< |
catch (IllegalStateException success) {} |
270 |
> |
} catch (IllegalStateException success) {} |
271 |
|
} |
272 |
+ |
|
273 |
|
/** |
274 |
|
* Queue contains all elements, in traversal order, of successful addAll |
275 |
|
*/ |
276 |
|
public void testAddAll5() { |
277 |
< |
try { |
278 |
< |
Integer[] empty = new Integer[0]; |
279 |
< |
Integer[] ints = new Integer[SIZE]; |
280 |
< |
for (int i = 0; i < SIZE; ++i) |
281 |
< |
ints[i] = new Integer(i); |
282 |
< |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
283 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
284 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
285 |
< |
for (int i = 0; i < SIZE; ++i) |
279 |
< |
assertEquals(ints[i], q.poll()); |
280 |
< |
} |
281 |
< |
finally {} |
277 |
> |
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 |
|
} |
287 |
|
|
288 |
|
/** |
289 |
< |
* put(null) throws NPE |
289 |
> |
* put(null) throws NPE |
290 |
|
*/ |
291 |
< |
public void testPutNull() { |
292 |
< |
try { |
291 |
> |
public void testPutNull() throws InterruptedException { |
292 |
> |
try { |
293 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
294 |
|
q.put(null); |
295 |
|
shouldThrow(); |
296 |
< |
} |
293 |
< |
catch (NullPointerException success){ |
294 |
< |
} |
295 |
< |
catch (InterruptedException ie) { |
296 |
< |
unexpectedException(); |
297 |
< |
} |
296 |
> |
} catch (NullPointerException success) {} |
297 |
|
} |
298 |
|
|
299 |
|
/** |
300 |
|
* all elements successfully put are contained |
301 |
|
*/ |
302 |
< |
public void testPut() { |
303 |
< |
try { |
304 |
< |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
305 |
< |
for (int i = 0; i < SIZE; ++i) { |
306 |
< |
Integer I = new Integer(i); |
307 |
< |
q.put(I); |
309 |
< |
assertTrue(q.contains(I)); |
310 |
< |
} |
311 |
< |
assertEquals(0, q.remainingCapacity()); |
312 |
< |
} |
313 |
< |
catch (InterruptedException ie) { |
314 |
< |
unexpectedException(); |
302 |
> |
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 |
|
} |
309 |
+ |
assertEquals(0, q.remainingCapacity()); |
310 |
|
} |
311 |
|
|
312 |
|
/** |
313 |
|
* put blocks interruptibly if full |
314 |
|
*/ |
315 |
< |
public void testBlockingPut() { |
315 |
> |
public void testBlockingPut() throws InterruptedException { |
316 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
317 |
< |
Thread t = new Thread(new Runnable() { |
318 |
< |
public void run() { |
319 |
< |
int added = 0; |
320 |
< |
try { |
321 |
< |
for (int i = 0; i < SIZE; ++i) { |
322 |
< |
q.put(new Integer(i)); |
323 |
< |
++added; |
324 |
< |
} |
325 |
< |
q.put(new Integer(SIZE)); |
326 |
< |
threadShouldThrow(); |
327 |
< |
} catch (InterruptedException ie){ |
328 |
< |
threadAssertEquals(added, SIZE); |
329 |
< |
} |
330 |
< |
}}); |
331 |
< |
try { |
332 |
< |
t.start(); |
333 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
334 |
< |
t.interrupt(); |
341 |
< |
t.join(); |
342 |
< |
} |
343 |
< |
catch (InterruptedException ie) { |
344 |
< |
unexpectedException(); |
345 |
< |
} |
317 |
> |
Thread t = new Thread(new CheckedRunnable() { |
318 |
> |
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 |
> |
try { |
324 |
> |
q.put(99); |
325 |
> |
shouldThrow(); |
326 |
> |
} catch (InterruptedException success) {} |
327 |
> |
}}); |
328 |
> |
|
329 |
> |
t.start(); |
330 |
> |
Thread.sleep(SHORT_DELAY_MS); |
331 |
> |
t.interrupt(); |
332 |
> |
t.join(); |
333 |
> |
assertEquals(SIZE, q.size()); |
334 |
> |
assertEquals(0, q.remainingCapacity()); |
335 |
|
} |
336 |
|
|
337 |
|
/** |
338 |
|
* put blocks waiting for take when full |
339 |
|
*/ |
340 |
< |
public void testPutWithTake() { |
341 |
< |
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
342 |
< |
Thread t = new Thread(new Runnable() { |
343 |
< |
public void run() { |
344 |
< |
int added = 0; |
345 |
< |
try { |
346 |
< |
q.put(new Object()); |
347 |
< |
++added; |
348 |
< |
q.put(new Object()); |
349 |
< |
++added; |
350 |
< |
q.put(new Object()); |
351 |
< |
++added; |
352 |
< |
q.put(new Object()); |
353 |
< |
++added; |
354 |
< |
threadShouldThrow(); |
355 |
< |
} catch (InterruptedException e){ |
356 |
< |
threadAssertTrue(added >= 2); |
357 |
< |
} |
358 |
< |
} |
359 |
< |
}); |
360 |
< |
try { |
372 |
< |
t.start(); |
373 |
< |
Thread.sleep(SHORT_DELAY_MS); |
374 |
< |
q.take(); |
375 |
< |
t.interrupt(); |
376 |
< |
t.join(); |
377 |
< |
} catch (Exception e){ |
378 |
< |
unexpectedException(); |
379 |
< |
} |
340 |
> |
public void testPutWithTake() throws InterruptedException { |
341 |
> |
final int capacity = 2; |
342 |
> |
final ArrayBlockingQueue q = new ArrayBlockingQueue(capacity); |
343 |
> |
Thread t = new Thread(new CheckedRunnable() { |
344 |
> |
public void realRun() throws InterruptedException { |
345 |
> |
for (int i = 0; i < capacity + 1; i++) |
346 |
> |
q.put(i); |
347 |
> |
try { |
348 |
> |
q.put(99); |
349 |
> |
shouldThrow(); |
350 |
> |
} catch (InterruptedException success) {} |
351 |
> |
}}); |
352 |
> |
|
353 |
> |
t.start(); |
354 |
> |
Thread.sleep(SHORT_DELAY_MS); |
355 |
> |
assertEquals(q.remainingCapacity(), 0); |
356 |
> |
assertEquals(0, q.take()); |
357 |
> |
Thread.sleep(SHORT_DELAY_MS); |
358 |
> |
t.interrupt(); |
359 |
> |
t.join(); |
360 |
> |
assertEquals(q.remainingCapacity(), 0); |
361 |
|
} |
362 |
|
|
363 |
|
/** |
364 |
|
* timed offer times out if full and elements not taken |
365 |
|
*/ |
366 |
< |
public void testTimedOffer() { |
366 |
> |
public void testTimedOffer() throws InterruptedException { |
367 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
368 |
< |
Thread t = new Thread(new Runnable() { |
369 |
< |
public void run() { |
370 |
< |
try { |
371 |
< |
q.put(new Object()); |
372 |
< |
q.put(new Object()); |
373 |
< |
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS)); |
374 |
< |
q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
375 |
< |
threadShouldThrow(); |
376 |
< |
} catch (InterruptedException success){} |
377 |
< |
} |
378 |
< |
}); |
379 |
< |
|
380 |
< |
try { |
381 |
< |
t.start(); |
382 |
< |
Thread.sleep(SHORT_DELAY_MS); |
402 |
< |
t.interrupt(); |
403 |
< |
t.join(); |
404 |
< |
} catch (Exception e){ |
405 |
< |
unexpectedException(); |
406 |
< |
} |
368 |
> |
Thread t = new Thread(new CheckedRunnable() { |
369 |
> |
public void realRun() throws InterruptedException { |
370 |
> |
q.put(new Object()); |
371 |
> |
q.put(new Object()); |
372 |
> |
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 |
> |
|
379 |
> |
t.start(); |
380 |
> |
Thread.sleep(SHORT_DELAY_MS); |
381 |
> |
t.interrupt(); |
382 |
> |
t.join(); |
383 |
|
} |
384 |
|
|
385 |
|
/** |
386 |
|
* take retrieves elements in FIFO order |
387 |
|
*/ |
388 |
< |
public void testTake() { |
389 |
< |
try { |
390 |
< |
ArrayBlockingQueue q = populatedQueue(SIZE); |
391 |
< |
for (int i = 0; i < SIZE; ++i) { |
416 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
417 |
< |
} |
418 |
< |
} catch (InterruptedException e){ |
419 |
< |
unexpectedException(); |
420 |
< |
} |
421 |
< |
} |
422 |
< |
|
423 |
< |
/** |
424 |
< |
* take blocks interruptibly when empty |
425 |
< |
*/ |
426 |
< |
public void testTakeFromEmpty() { |
427 |
< |
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
428 |
< |
Thread t = new Thread(new Runnable() { |
429 |
< |
public void run() { |
430 |
< |
try { |
431 |
< |
q.take(); |
432 |
< |
threadShouldThrow(); |
433 |
< |
} catch (InterruptedException success){ } |
434 |
< |
} |
435 |
< |
}); |
436 |
< |
try { |
437 |
< |
t.start(); |
438 |
< |
Thread.sleep(SHORT_DELAY_MS); |
439 |
< |
t.interrupt(); |
440 |
< |
t.join(); |
441 |
< |
} catch (Exception e){ |
442 |
< |
unexpectedException(); |
388 |
> |
public void testTake() throws InterruptedException { |
389 |
> |
ArrayBlockingQueue q = populatedQueue(SIZE); |
390 |
> |
for (int i = 0; i < SIZE; ++i) { |
391 |
> |
assertEquals(i, q.take()); |
392 |
|
} |
393 |
|
} |
394 |
|
|
395 |
|
/** |
396 |
|
* Take removes existing elements until empty, then blocks interruptibly |
397 |
|
*/ |
398 |
< |
public void testBlockingTake() { |
399 |
< |
Thread t = new Thread(new Runnable() { |
400 |
< |
public void run() { |
401 |
< |
try { |
402 |
< |
ArrayBlockingQueue q = populatedQueue(SIZE); |
403 |
< |
for (int i = 0; i < SIZE; ++i) { |
404 |
< |
threadAssertEquals(i, ((Integer)q.take()).intValue()); |
405 |
< |
} |
406 |
< |
q.take(); |
407 |
< |
threadShouldThrow(); |
408 |
< |
} catch (InterruptedException success){ |
409 |
< |
} |
410 |
< |
}}); |
411 |
< |
try { |
412 |
< |
t.start(); |
413 |
< |
Thread.sleep(SHORT_DELAY_MS); |
414 |
< |
t.interrupt(); |
466 |
< |
t.join(); |
467 |
< |
} |
468 |
< |
catch (InterruptedException ie) { |
469 |
< |
unexpectedException(); |
470 |
< |
} |
398 |
> |
public void testBlockingTake() throws InterruptedException { |
399 |
> |
final ArrayBlockingQueue q = populatedQueue(SIZE); |
400 |
> |
Thread t = new Thread(new CheckedRunnable() { |
401 |
> |
public void realRun() throws InterruptedException { |
402 |
> |
for (int i = 0; i < SIZE; ++i) { |
403 |
> |
assertEquals(i, q.take()); |
404 |
> |
} |
405 |
> |
try { |
406 |
> |
q.take(); |
407 |
> |
shouldThrow(); |
408 |
> |
} catch (InterruptedException success) {} |
409 |
> |
}}); |
410 |
> |
|
411 |
> |
t.start(); |
412 |
> |
Thread.sleep(SHORT_DELAY_MS); |
413 |
> |
t.interrupt(); |
414 |
> |
t.join(); |
415 |
|
} |
416 |
|
|
417 |
|
|
421 |
|
public void testPoll() { |
422 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
423 |
|
for (int i = 0; i < SIZE; ++i) { |
424 |
< |
assertEquals(i, ((Integer)q.poll()).intValue()); |
424 |
> |
assertEquals(i, q.poll()); |
425 |
|
} |
426 |
< |
assertNull(q.poll()); |
426 |
> |
assertNull(q.poll()); |
427 |
|
} |
428 |
|
|
429 |
|
/** |
430 |
< |
* timed pool with zero timeout succeeds when non-empty, else times out |
430 |
> |
* timed poll with zero timeout succeeds when non-empty, else times out |
431 |
|
*/ |
432 |
< |
public void testTimedPoll0() { |
433 |
< |
try { |
434 |
< |
ArrayBlockingQueue q = populatedQueue(SIZE); |
435 |
< |
for (int i = 0; i < SIZE; ++i) { |
436 |
< |
assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue()); |
437 |
< |
} |
494 |
< |
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
495 |
< |
} catch (InterruptedException e){ |
496 |
< |
unexpectedException(); |
497 |
< |
} |
432 |
> |
public void testTimedPoll0() throws InterruptedException { |
433 |
> |
ArrayBlockingQueue q = populatedQueue(SIZE); |
434 |
> |
for (int i = 0; i < SIZE; ++i) { |
435 |
> |
assertEquals(i, q.poll(0, MILLISECONDS)); |
436 |
> |
} |
437 |
> |
assertNull(q.poll(0, MILLISECONDS)); |
438 |
|
} |
439 |
|
|
440 |
|
/** |
441 |
< |
* timed pool with nonzero timeout succeeds when non-empty, else times out |
441 |
> |
* timed poll with nonzero timeout succeeds when non-empty, else times out |
442 |
|
*/ |
443 |
< |
public void testTimedPoll() { |
444 |
< |
try { |
445 |
< |
ArrayBlockingQueue q = populatedQueue(SIZE); |
446 |
< |
for (int i = 0; i < SIZE; ++i) { |
447 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
448 |
< |
} |
509 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
510 |
< |
} catch (InterruptedException e){ |
511 |
< |
unexpectedException(); |
512 |
< |
} |
443 |
> |
public void testTimedPoll() throws InterruptedException { |
444 |
> |
ArrayBlockingQueue q = populatedQueue(SIZE); |
445 |
> |
for (int i = 0; i < SIZE; ++i) { |
446 |
> |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
447 |
> |
} |
448 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
449 |
|
} |
450 |
|
|
451 |
|
/** |
452 |
|
* Interrupted timed poll throws InterruptedException instead of |
453 |
|
* returning timeout status |
454 |
|
*/ |
455 |
< |
public void testInterruptedTimedPoll() { |
456 |
< |
Thread t = new Thread(new Runnable() { |
457 |
< |
public void run() { |
458 |
< |
try { |
459 |
< |
ArrayBlockingQueue q = populatedQueue(SIZE); |
460 |
< |
for (int i = 0; i < SIZE; ++i) { |
461 |
< |
threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
462 |
< |
} |
463 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
528 |
< |
} catch (InterruptedException success){ |
529 |
< |
} |
530 |
< |
}}); |
531 |
< |
try { |
532 |
< |
t.start(); |
533 |
< |
Thread.sleep(SHORT_DELAY_MS); |
534 |
< |
t.interrupt(); |
535 |
< |
t.join(); |
536 |
< |
} |
537 |
< |
catch (InterruptedException ie) { |
538 |
< |
unexpectedException(); |
539 |
< |
} |
540 |
< |
} |
541 |
< |
|
542 |
< |
/** |
543 |
< |
* timed poll before a delayed offer fails; after offer succeeds; |
544 |
< |
* on interruption throws |
545 |
< |
*/ |
546 |
< |
public void testTimedPollWithOffer() { |
547 |
< |
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
548 |
< |
Thread t = new Thread(new Runnable() { |
549 |
< |
public void run() { |
550 |
< |
try { |
551 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
552 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
553 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
554 |
< |
threadShouldThrow(); |
555 |
< |
} catch (InterruptedException success) { } |
455 |
> |
public void testInterruptedTimedPoll() throws InterruptedException { |
456 |
> |
final BlockingQueue<Integer> q = populatedQueue(SIZE); |
457 |
> |
final CountDownLatch aboutToWait = new CountDownLatch(1); |
458 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
459 |
> |
public void realRun() throws InterruptedException { |
460 |
> |
for (int i = 0; i < SIZE; ++i) { |
461 |
> |
long t0 = System.nanoTime(); |
462 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
463 |
> |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
464 |
|
} |
465 |
< |
}); |
466 |
< |
try { |
467 |
< |
t.start(); |
468 |
< |
Thread.sleep(SMALL_DELAY_MS); |
469 |
< |
assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
470 |
< |
t.interrupt(); |
471 |
< |
t.join(); |
472 |
< |
} catch (Exception e){ |
473 |
< |
unexpectedException(); |
474 |
< |
} |
475 |
< |
} |
476 |
< |
|
465 |
> |
long t0 = System.nanoTime(); |
466 |
> |
aboutToWait.countDown(); |
467 |
> |
try { |
468 |
> |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
469 |
> |
shouldThrow(); |
470 |
> |
} catch (InterruptedException success) { |
471 |
> |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
472 |
> |
} |
473 |
> |
}}); |
474 |
> |
|
475 |
> |
aboutToWait.await(); |
476 |
> |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
477 |
> |
t.interrupt(); |
478 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
479 |
> |
checkEmpty(q); |
480 |
> |
} |
481 |
|
|
482 |
|
/** |
483 |
|
* peek returns next element, or null if empty |
485 |
|
public void testPeek() { |
486 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
487 |
|
for (int i = 0; i < SIZE; ++i) { |
488 |
< |
assertEquals(i, ((Integer)q.peek()).intValue()); |
489 |
< |
q.poll(); |
488 |
> |
assertEquals(i, q.peek()); |
489 |
> |
assertEquals(i, q.poll()); |
490 |
|
assertTrue(q.peek() == null || |
491 |
< |
i != ((Integer)q.peek()).intValue()); |
491 |
> |
!q.peek().equals(i)); |
492 |
|
} |
493 |
< |
assertNull(q.peek()); |
493 |
> |
assertNull(q.peek()); |
494 |
|
} |
495 |
|
|
496 |
|
/** |
499 |
|
public void testElement() { |
500 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
501 |
|
for (int i = 0; i < SIZE; ++i) { |
502 |
< |
assertEquals(i, ((Integer)q.element()).intValue()); |
503 |
< |
q.poll(); |
502 |
> |
assertEquals(i, q.element()); |
503 |
> |
assertEquals(i, q.poll()); |
504 |
|
} |
505 |
|
try { |
506 |
|
q.element(); |
507 |
|
shouldThrow(); |
508 |
< |
} |
597 |
< |
catch (NoSuchElementException success) {} |
508 |
> |
} catch (NoSuchElementException success) {} |
509 |
|
} |
510 |
|
|
511 |
|
/** |
514 |
|
public void testRemove() { |
515 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
516 |
|
for (int i = 0; i < SIZE; ++i) { |
517 |
< |
assertEquals(i, ((Integer)q.remove()).intValue()); |
517 |
> |
assertEquals(i, q.remove()); |
518 |
|
} |
519 |
|
try { |
520 |
|
q.remove(); |
521 |
|
shouldThrow(); |
522 |
< |
} catch (NoSuchElementException success){ |
612 |
< |
} |
522 |
> |
} catch (NoSuchElementException success) {} |
523 |
|
} |
524 |
|
|
525 |
|
/** |
536 |
|
} |
537 |
|
assertTrue(q.isEmpty()); |
538 |
|
} |
539 |
< |
|
539 |
> |
|
540 |
|
/** |
541 |
|
* contains(x) reports true when elements added but not yet removed |
542 |
|
*/ |
544 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
545 |
|
for (int i = 0; i < SIZE; ++i) { |
546 |
|
assertTrue(q.contains(new Integer(i))); |
547 |
< |
q.poll(); |
547 |
> |
assertEquals(i, q.poll()); |
548 |
|
assertFalse(q.contains(new Integer(i))); |
549 |
|
} |
550 |
|
} |
615 |
|
} |
616 |
|
|
617 |
|
/** |
618 |
< |
* toArray contains all elements |
618 |
> |
* toArray contains all elements in FIFO order |
619 |
|
*/ |
620 |
|
public void testToArray() { |
621 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
622 |
< |
Object[] o = q.toArray(); |
623 |
< |
try { |
624 |
< |
for(int i = 0; i < o.length; i++) |
715 |
< |
assertEquals(o[i], q.take()); |
716 |
< |
} catch (InterruptedException e){ |
717 |
< |
unexpectedException(); |
718 |
< |
} |
622 |
> |
Object[] o = q.toArray(); |
623 |
> |
for (int i = 0; i < o.length; i++) |
624 |
> |
assertSame(o[i], q.poll()); |
625 |
|
} |
626 |
|
|
627 |
|
/** |
628 |
< |
* toArray(a) contains all elements |
628 |
> |
* toArray(a) contains all elements in FIFO order |
629 |
|
*/ |
630 |
|
public void testToArray2() { |
631 |
< |
ArrayBlockingQueue q = populatedQueue(SIZE); |
632 |
< |
Integer[] ints = new Integer[SIZE]; |
633 |
< |
ints = (Integer[])q.toArray(ints); |
634 |
< |
try { |
635 |
< |
for(int i = 0; i < ints.length; i++) |
636 |
< |
assertEquals(ints[i], q.take()); |
731 |
< |
} catch (InterruptedException e){ |
732 |
< |
unexpectedException(); |
733 |
< |
} |
631 |
> |
ArrayBlockingQueue<Integer> q = populatedQueue(SIZE); |
632 |
> |
Integer[] ints = new Integer[SIZE]; |
633 |
> |
Integer[] array = q.toArray(ints); |
634 |
> |
assertSame(ints, array); |
635 |
> |
for (int i = 0; i < ints.length; i++) |
636 |
> |
assertSame(ints[i], q.poll()); |
637 |
|
} |
638 |
|
|
639 |
|
/** |
640 |
< |
* toArray(null) throws NPE |
640 |
> |
* toArray(null) throws NullPointerException |
641 |
|
*/ |
642 |
< |
public void testToArray_BadArg() { |
643 |
< |
try { |
644 |
< |
ArrayBlockingQueue q = populatedQueue(SIZE); |
645 |
< |
Object o[] = q.toArray(null); |
646 |
< |
shouldThrow(); |
647 |
< |
} catch(NullPointerException success){} |
642 |
> |
public void testToArray_NullArg() { |
643 |
> |
ArrayBlockingQueue q = populatedQueue(SIZE); |
644 |
> |
try { |
645 |
> |
q.toArray(null); |
646 |
> |
shouldThrow(); |
647 |
> |
} catch (NullPointerException success) {} |
648 |
|
} |
649 |
|
|
650 |
|
/** |
651 |
< |
* toArray with incompatible array type throws CCE |
651 |
> |
* toArray(incompatible array type) throws ArrayStoreException |
652 |
|
*/ |
653 |
|
public void testToArray1_BadArg() { |
654 |
< |
try { |
655 |
< |
ArrayBlockingQueue q = populatedQueue(SIZE); |
656 |
< |
Object o[] = q.toArray(new String[10] ); |
657 |
< |
shouldThrow(); |
658 |
< |
} catch(ArrayStoreException success){} |
654 |
> |
ArrayBlockingQueue q = populatedQueue(SIZE); |
655 |
> |
try { |
656 |
> |
q.toArray(new String[10]); |
657 |
> |
shouldThrow(); |
658 |
> |
} catch (ArrayStoreException success) {} |
659 |
|
} |
660 |
|
|
661 |
< |
|
661 |
> |
|
662 |
|
/** |
663 |
|
* iterator iterates through all elements |
664 |
|
*/ |
665 |
< |
public void testIterator() { |
665 |
> |
public void testIterator() throws InterruptedException { |
666 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
667 |
< |
Iterator it = q.iterator(); |
668 |
< |
try { |
669 |
< |
while(it.hasNext()){ |
670 |
< |
assertEquals(it.next(), q.take()); |
768 |
< |
} |
769 |
< |
} catch (InterruptedException e){ |
770 |
< |
unexpectedException(); |
771 |
< |
} |
667 |
> |
Iterator it = q.iterator(); |
668 |
> |
while (it.hasNext()) { |
669 |
> |
assertEquals(it.next(), q.take()); |
670 |
> |
} |
671 |
|
} |
672 |
|
|
673 |
|
/** |
674 |
|
* iterator.remove removes current element |
675 |
|
*/ |
676 |
< |
public void testIteratorRemove () { |
676 |
> |
public void testIteratorRemove() { |
677 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(3); |
678 |
|
q.add(two); |
679 |
|
q.add(one); |
682 |
|
Iterator it = q.iterator(); |
683 |
|
it.next(); |
684 |
|
it.remove(); |
685 |
< |
|
685 |
> |
|
686 |
|
it = q.iterator(); |
687 |
< |
assertEquals(it.next(), one); |
688 |
< |
assertEquals(it.next(), three); |
687 |
> |
assertSame(it.next(), one); |
688 |
> |
assertSame(it.next(), three); |
689 |
|
assertFalse(it.hasNext()); |
690 |
|
} |
691 |
|
|
702 |
|
|
703 |
|
int k = 0; |
704 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
705 |
< |
int i = ((Integer)(it.next())).intValue(); |
807 |
< |
assertEquals(++k, i); |
705 |
> |
assertEquals(++k, it.next()); |
706 |
|
} |
707 |
|
assertEquals(3, k); |
708 |
|
} |
710 |
|
/** |
711 |
|
* Modifications do not cause iterators to fail |
712 |
|
*/ |
713 |
< |
public void testWeaklyConsistentIteration () { |
713 |
> |
public void testWeaklyConsistentIteration() { |
714 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(3); |
715 |
|
q.add(one); |
716 |
|
q.add(two); |
717 |
|
q.add(three); |
718 |
< |
try { |
719 |
< |
for (Iterator it = q.iterator(); it.hasNext();) { |
720 |
< |
q.remove(); |
823 |
< |
it.next(); |
824 |
< |
} |
825 |
< |
} |
826 |
< |
catch (ConcurrentModificationException e) { |
827 |
< |
unexpectedException(); |
718 |
> |
for (Iterator it = q.iterator(); it.hasNext();) { |
719 |
> |
q.remove(); |
720 |
> |
it.next(); |
721 |
|
} |
722 |
|
assertEquals(0, q.size()); |
723 |
|
} |
732 |
|
for (int i = 0; i < SIZE; ++i) { |
733 |
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
734 |
|
} |
735 |
< |
} |
735 |
> |
} |
736 |
|
|
737 |
|
|
738 |
|
/** |
743 |
|
q.add(one); |
744 |
|
q.add(two); |
745 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
746 |
< |
executor.execute(new Runnable() { |
747 |
< |
public void run() { |
748 |
< |
threadAssertFalse(q.offer(three)); |
749 |
< |
try { |
750 |
< |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
751 |
< |
threadAssertEquals(0, q.remainingCapacity()); |
752 |
< |
} |
753 |
< |
catch (InterruptedException e) { |
754 |
< |
threadUnexpectedException(); |
755 |
< |
} |
756 |
< |
} |
757 |
< |
}); |
746 |
> |
executor.execute(new CheckedRunnable() { |
747 |
> |
public void realRun() throws InterruptedException { |
748 |
> |
assertFalse(q.offer(three)); |
749 |
> |
assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
750 |
> |
assertEquals(0, q.remainingCapacity()); |
751 |
> |
}}); |
752 |
> |
|
753 |
> |
executor.execute(new CheckedRunnable() { |
754 |
> |
public void realRun() throws InterruptedException { |
755 |
> |
Thread.sleep(SMALL_DELAY_MS); |
756 |
> |
assertSame(one, q.take()); |
757 |
> |
}}); |
758 |
|
|
866 |
– |
executor.execute(new Runnable() { |
867 |
– |
public void run() { |
868 |
– |
try { |
869 |
– |
Thread.sleep(SMALL_DELAY_MS); |
870 |
– |
threadAssertEquals(one, q.take()); |
871 |
– |
} |
872 |
– |
catch (InterruptedException e) { |
873 |
– |
threadUnexpectedException(); |
874 |
– |
} |
875 |
– |
} |
876 |
– |
}); |
877 |
– |
|
759 |
|
joinPool(executor); |
879 |
– |
|
760 |
|
} |
761 |
|
|
762 |
|
/** |
765 |
|
public void testPollInExecutor() { |
766 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
767 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
768 |
< |
executor.execute(new Runnable() { |
769 |
< |
public void run() { |
770 |
< |
threadAssertNull(q.poll()); |
771 |
< |
try { |
772 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
773 |
< |
threadAssertTrue(q.isEmpty()); |
774 |
< |
} |
775 |
< |
catch (InterruptedException e) { |
776 |
< |
threadUnexpectedException(); |
777 |
< |
} |
778 |
< |
} |
779 |
< |
}); |
768 |
> |
executor.execute(new CheckedRunnable() { |
769 |
> |
public void realRun() throws InterruptedException { |
770 |
> |
assertNull(q.poll()); |
771 |
> |
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
772 |
> |
assertTrue(q.isEmpty()); |
773 |
> |
}}); |
774 |
> |
|
775 |
> |
executor.execute(new CheckedRunnable() { |
776 |
> |
public void realRun() throws InterruptedException { |
777 |
> |
Thread.sleep(SMALL_DELAY_MS); |
778 |
> |
q.put(one); |
779 |
> |
}}); |
780 |
|
|
901 |
– |
executor.execute(new Runnable() { |
902 |
– |
public void run() { |
903 |
– |
try { |
904 |
– |
Thread.sleep(SMALL_DELAY_MS); |
905 |
– |
q.put(one); |
906 |
– |
} |
907 |
– |
catch (InterruptedException e) { |
908 |
– |
threadUnexpectedException(); |
909 |
– |
} |
910 |
– |
} |
911 |
– |
}); |
912 |
– |
|
781 |
|
joinPool(executor); |
782 |
|
} |
783 |
|
|
784 |
|
/** |
785 |
|
* A deserialized serialized queue has same elements in same order |
786 |
|
*/ |
787 |
< |
public void testSerialization() { |
787 |
> |
public void testSerialization() throws Exception { |
788 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
789 |
|
|
790 |
< |
try { |
791 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
792 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
793 |
< |
out.writeObject(q); |
794 |
< |
out.close(); |
795 |
< |
|
796 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
797 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
798 |
< |
ArrayBlockingQueue r = (ArrayBlockingQueue)in.readObject(); |
799 |
< |
assertEquals(q.size(), r.size()); |
800 |
< |
while (!q.isEmpty()) |
933 |
< |
assertEquals(q.remove(), r.remove()); |
934 |
< |
} catch(Exception e){ |
935 |
< |
unexpectedException(); |
936 |
< |
} |
790 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
791 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
792 |
> |
out.writeObject(q); |
793 |
> |
out.close(); |
794 |
> |
|
795 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
796 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
797 |
> |
ArrayBlockingQueue r = (ArrayBlockingQueue)in.readObject(); |
798 |
> |
assertEquals(q.size(), r.size()); |
799 |
> |
while (!q.isEmpty()) |
800 |
> |
assertEquals(q.remove(), r.remove()); |
801 |
|
} |
802 |
|
|
803 |
|
/** |
804 |
|
* drainTo(null) throws NPE |
805 |
< |
*/ |
805 |
> |
*/ |
806 |
|
public void testDrainToNull() { |
807 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
808 |
|
try { |
809 |
|
q.drainTo(null); |
810 |
|
shouldThrow(); |
811 |
< |
} catch(NullPointerException success) { |
948 |
< |
} |
811 |
> |
} catch (NullPointerException success) {} |
812 |
|
} |
813 |
|
|
814 |
|
/** |
815 |
|
* drainTo(this) throws IAE |
816 |
< |
*/ |
816 |
> |
*/ |
817 |
|
public void testDrainToSelf() { |
818 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
819 |
|
try { |
820 |
|
q.drainTo(q); |
821 |
|
shouldThrow(); |
822 |
< |
} catch(IllegalArgumentException success) { |
960 |
< |
} |
822 |
> |
} catch (IllegalArgumentException success) {} |
823 |
|
} |
824 |
|
|
825 |
|
/** |
826 |
|
* drainTo(c) empties queue into another collection c |
827 |
< |
*/ |
827 |
> |
*/ |
828 |
|
public void testDrainTo() { |
829 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
830 |
|
ArrayList l = new ArrayList(); |
831 |
|
q.drainTo(l); |
832 |
|
assertEquals(q.size(), 0); |
833 |
|
assertEquals(l.size(), SIZE); |
834 |
< |
for (int i = 0; i < SIZE; ++i) |
834 |
> |
for (int i = 0; i < SIZE; ++i) |
835 |
|
assertEquals(l.get(i), new Integer(i)); |
836 |
|
q.add(zero); |
837 |
|
q.add(one); |
842 |
|
q.drainTo(l); |
843 |
|
assertEquals(q.size(), 0); |
844 |
|
assertEquals(l.size(), 2); |
845 |
< |
for (int i = 0; i < 2; ++i) |
845 |
> |
for (int i = 0; i < 2; ++i) |
846 |
|
assertEquals(l.get(i), new Integer(i)); |
847 |
|
} |
848 |
|
|
849 |
|
/** |
850 |
|
* drainTo empties full queue, unblocking a waiting put. |
851 |
< |
*/ |
852 |
< |
public void testDrainToWithActivePut() { |
851 |
> |
*/ |
852 |
> |
public void testDrainToWithActivePut() throws InterruptedException { |
853 |
|
final ArrayBlockingQueue q = populatedQueue(SIZE); |
854 |
< |
Thread t = new Thread(new Runnable() { |
855 |
< |
public void run() { |
856 |
< |
try { |
857 |
< |
q.put(new Integer(SIZE+1)); |
858 |
< |
} catch (InterruptedException ie){ |
859 |
< |
threadUnexpectedException(); |
860 |
< |
} |
861 |
< |
} |
862 |
< |
}); |
863 |
< |
try { |
864 |
< |
t.start(); |
865 |
< |
ArrayList l = new ArrayList(); |
866 |
< |
q.drainTo(l); |
1005 |
< |
assertTrue(l.size() >= SIZE); |
1006 |
< |
for (int i = 0; i < SIZE; ++i) |
1007 |
< |
assertEquals(l.get(i), new Integer(i)); |
1008 |
< |
t.join(); |
1009 |
< |
assertTrue(q.size() + l.size() >= SIZE); |
1010 |
< |
} catch(Exception e){ |
1011 |
< |
unexpectedException(); |
1012 |
< |
} |
854 |
> |
Thread t = new Thread(new CheckedRunnable() { |
855 |
> |
public void realRun() throws InterruptedException { |
856 |
> |
q.put(new Integer(SIZE+1)); |
857 |
> |
}}); |
858 |
> |
|
859 |
> |
t.start(); |
860 |
> |
ArrayList l = new ArrayList(); |
861 |
> |
q.drainTo(l); |
862 |
> |
assertTrue(l.size() >= SIZE); |
863 |
> |
for (int i = 0; i < SIZE; ++i) |
864 |
> |
assertEquals(l.get(i), new Integer(i)); |
865 |
> |
t.join(); |
866 |
> |
assertTrue(q.size() + l.size() >= SIZE); |
867 |
|
} |
868 |
|
|
869 |
|
/** |
870 |
|
* drainTo(null, n) throws NPE |
871 |
< |
*/ |
871 |
> |
*/ |
872 |
|
public void testDrainToNullN() { |
873 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
874 |
|
try { |
875 |
|
q.drainTo(null, 0); |
876 |
|
shouldThrow(); |
877 |
< |
} catch(NullPointerException success) { |
1024 |
< |
} |
877 |
> |
} catch (NullPointerException success) {} |
878 |
|
} |
879 |
|
|
880 |
|
/** |
881 |
|
* drainTo(this, n) throws IAE |
882 |
< |
*/ |
882 |
> |
*/ |
883 |
|
public void testDrainToSelfN() { |
884 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
885 |
|
try { |
886 |
|
q.drainTo(q, 0); |
887 |
|
shouldThrow(); |
888 |
< |
} catch(IllegalArgumentException success) { |
1036 |
< |
} |
888 |
> |
} catch (IllegalArgumentException success) {} |
889 |
|
} |
890 |
|
|
891 |
|
/** |
892 |
< |
* drainTo(c, n) empties first max {n, size} elements of queue into c |
893 |
< |
*/ |
892 |
> |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
893 |
> |
*/ |
894 |
|
public void testDrainToN() { |
895 |
|
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE*2); |
896 |
|
for (int i = 0; i < SIZE + 2; ++i) { |
897 |
< |
for(int j = 0; j < SIZE; j++) |
897 |
> |
for (int j = 0; j < SIZE; j++) |
898 |
|
assertTrue(q.offer(new Integer(j))); |
899 |
|
ArrayList l = new ArrayList(); |
900 |
|
q.drainTo(l, i); |
901 |
< |
int k = (i < SIZE)? i : SIZE; |
901 |
> |
int k = (i < SIZE) ? i : SIZE; |
902 |
|
assertEquals(l.size(), k); |
903 |
|
assertEquals(q.size(), SIZE-k); |
904 |
< |
for (int j = 0; j < k; ++j) |
904 |
> |
for (int j = 0; j < k; ++j) |
905 |
|
assertEquals(l.get(j), new Integer(j)); |
906 |
|
while (q.poll() != null) ; |
907 |
|
} |
908 |
|
} |
909 |
|
|
1058 |
– |
|
910 |
|
} |