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