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