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 |
* |
39 |
*/ |
40 |
public void testConstructor1() { |
41 |
assertEquals(SIZE, new ArrayBlockingQueue(SIZE).remainingCapacity()); |
42 |
} |
43 |
|
44 |
/** |
45 |
* |
46 |
*/ |
47 |
public void testConstructor2() { |
48 |
try { |
49 |
ArrayBlockingQueue q = new ArrayBlockingQueue(0); |
50 |
shouldThrow(); |
51 |
} |
52 |
catch (IllegalArgumentException success) {} |
53 |
} |
54 |
|
55 |
/** |
56 |
* |
57 |
*/ |
58 |
public void testConstructor3() { |
59 |
|
60 |
try { |
61 |
ArrayBlockingQueue q = new ArrayBlockingQueue(1, true, null); |
62 |
shouldThrow(); |
63 |
} |
64 |
catch (NullPointerException success) {} |
65 |
} |
66 |
|
67 |
/** |
68 |
* |
69 |
*/ |
70 |
public void testConstructor4() { |
71 |
try { |
72 |
Integer[] ints = new Integer[SIZE]; |
73 |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints)); |
74 |
shouldThrow(); |
75 |
} |
76 |
catch (NullPointerException success) {} |
77 |
} |
78 |
|
79 |
/** |
80 |
* |
81 |
*/ |
82 |
public void testConstructor5() { |
83 |
try { |
84 |
Integer[] ints = new Integer[SIZE]; |
85 |
for (int i = 0; i < SIZE-1; ++i) |
86 |
ints[i] = new Integer(i); |
87 |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints)); |
88 |
shouldThrow(); |
89 |
} |
90 |
catch (NullPointerException success) {} |
91 |
} |
92 |
|
93 |
/** |
94 |
* |
95 |
*/ |
96 |
public void testConstructor6() { |
97 |
try { |
98 |
Integer[] ints = new Integer[SIZE]; |
99 |
for (int i = 0; i < SIZE; ++i) |
100 |
ints[i] = new Integer(i); |
101 |
ArrayBlockingQueue q = new ArrayBlockingQueue(1, false, Arrays.asList(ints)); |
102 |
shouldThrow(); |
103 |
} |
104 |
catch (IllegalArgumentException success) {} |
105 |
} |
106 |
|
107 |
/** |
108 |
* |
109 |
*/ |
110 |
public void testConstructor7() { |
111 |
try { |
112 |
Integer[] ints = new Integer[SIZE]; |
113 |
for (int i = 0; i < SIZE; ++i) |
114 |
ints[i] = new Integer(i); |
115 |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, true, Arrays.asList(ints)); |
116 |
for (int i = 0; i < SIZE; ++i) |
117 |
assertEquals(ints[i], q.poll()); |
118 |
} |
119 |
finally {} |
120 |
} |
121 |
|
122 |
/** |
123 |
* |
124 |
*/ |
125 |
public void testEmptyFull() { |
126 |
ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
127 |
assertTrue(q.isEmpty()); |
128 |
assertEquals(2, q.remainingCapacity()); |
129 |
q.add(one); |
130 |
assertFalse(q.isEmpty()); |
131 |
q.add(two); |
132 |
assertFalse(q.isEmpty()); |
133 |
assertEquals(0, q.remainingCapacity()); |
134 |
assertFalse(q.offer(three)); |
135 |
} |
136 |
|
137 |
/** |
138 |
* |
139 |
*/ |
140 |
public void testRemainingCapacity() { |
141 |
ArrayBlockingQueue q = populatedQueue(SIZE); |
142 |
for (int i = 0; i < SIZE; ++i) { |
143 |
assertEquals(i, q.remainingCapacity()); |
144 |
assertEquals(SIZE-i, q.size()); |
145 |
q.remove(); |
146 |
} |
147 |
for (int i = 0; i < SIZE; ++i) { |
148 |
assertEquals(SIZE-i, q.remainingCapacity()); |
149 |
assertEquals(i, q.size()); |
150 |
q.add(new Integer(i)); |
151 |
} |
152 |
} |
153 |
|
154 |
/** |
155 |
* |
156 |
*/ |
157 |
public void testOfferNull() { |
158 |
try { |
159 |
ArrayBlockingQueue q = new ArrayBlockingQueue(1); |
160 |
q.offer(null); |
161 |
shouldThrow(); |
162 |
} catch (NullPointerException success) { } |
163 |
} |
164 |
|
165 |
/** |
166 |
* |
167 |
*/ |
168 |
public void testOffer() { |
169 |
ArrayBlockingQueue q = new ArrayBlockingQueue(1); |
170 |
assertTrue(q.offer(zero)); |
171 |
assertFalse(q.offer(one)); |
172 |
} |
173 |
|
174 |
/** |
175 |
* |
176 |
*/ |
177 |
public void testAdd() { |
178 |
try { |
179 |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
180 |
for (int i = 0; i < SIZE; ++i) { |
181 |
assertTrue(q.add(new Integer(i))); |
182 |
} |
183 |
assertEquals(0, q.remainingCapacity()); |
184 |
q.add(new Integer(SIZE)); |
185 |
} catch (IllegalStateException success){ |
186 |
} |
187 |
} |
188 |
|
189 |
/** |
190 |
* |
191 |
*/ |
192 |
public void testAddAll1() { |
193 |
try { |
194 |
ArrayBlockingQueue q = new ArrayBlockingQueue(1); |
195 |
q.addAll(null); |
196 |
shouldThrow(); |
197 |
} |
198 |
catch (NullPointerException success) {} |
199 |
} |
200 |
/** |
201 |
* |
202 |
*/ |
203 |
public void testAddAll2() { |
204 |
try { |
205 |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
206 |
Integer[] ints = new Integer[SIZE]; |
207 |
q.addAll(Arrays.asList(ints)); |
208 |
shouldThrow(); |
209 |
} |
210 |
catch (NullPointerException success) {} |
211 |
} |
212 |
/** |
213 |
* |
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 |
* |
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 |
* |
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 |
* |
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 |
* |
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 |
* |
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 |
* |
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 |
* |
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 |
* |
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 |
* |
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 |
* |
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 |
* |
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 |
* |
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 |
* |
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 |
* |
491 |
*/ |
492 |
public void testInterruptedTimedPoll() { |
493 |
Thread t = new Thread(new Runnable() { |
494 |
public void run() { |
495 |
try { |
496 |
ArrayBlockingQueue q = populatedQueue(SIZE); |
497 |
for (int i = 0; i < SIZE; ++i) { |
498 |
threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
499 |
} |
500 |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
501 |
} catch (InterruptedException success){ |
502 |
} |
503 |
}}); |
504 |
try { |
505 |
t.start(); |
506 |
Thread.sleep(SHORT_DELAY_MS); |
507 |
t.interrupt(); |
508 |
t.join(); |
509 |
} |
510 |
catch (InterruptedException ie) { |
511 |
unexpectedException(); |
512 |
} |
513 |
} |
514 |
|
515 |
/** |
516 |
* |
517 |
*/ |
518 |
public void testTimedPollWithOffer() { |
519 |
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
520 |
Thread t = new Thread(new Runnable() { |
521 |
public void run() { |
522 |
try { |
523 |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
524 |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
525 |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
526 |
threadShouldThrow(); |
527 |
} catch (InterruptedException success) { } |
528 |
} |
529 |
}); |
530 |
try { |
531 |
t.start(); |
532 |
Thread.sleep(SMALL_DELAY_MS); |
533 |
assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
534 |
t.interrupt(); |
535 |
t.join(); |
536 |
} catch (Exception e){ |
537 |
unexpectedException(); |
538 |
} |
539 |
} |
540 |
|
541 |
|
542 |
/** |
543 |
* |
544 |
*/ |
545 |
public void testPeek() { |
546 |
ArrayBlockingQueue q = populatedQueue(SIZE); |
547 |
for (int i = 0; i < SIZE; ++i) { |
548 |
assertEquals(i, ((Integer)q.peek()).intValue()); |
549 |
q.poll(); |
550 |
assertTrue(q.peek() == null || |
551 |
i != ((Integer)q.peek()).intValue()); |
552 |
} |
553 |
assertNull(q.peek()); |
554 |
} |
555 |
|
556 |
/** |
557 |
* |
558 |
*/ |
559 |
public void testElement() { |
560 |
ArrayBlockingQueue q = populatedQueue(SIZE); |
561 |
for (int i = 0; i < SIZE; ++i) { |
562 |
assertEquals(i, ((Integer)q.element()).intValue()); |
563 |
q.poll(); |
564 |
} |
565 |
try { |
566 |
q.element(); |
567 |
shouldThrow(); |
568 |
} |
569 |
catch (NoSuchElementException success) {} |
570 |
} |
571 |
|
572 |
/** |
573 |
* |
574 |
*/ |
575 |
public void testRemove() { |
576 |
ArrayBlockingQueue q = populatedQueue(SIZE); |
577 |
for (int i = 0; i < SIZE; ++i) { |
578 |
assertEquals(i, ((Integer)q.remove()).intValue()); |
579 |
} |
580 |
try { |
581 |
q.remove(); |
582 |
shouldThrow(); |
583 |
} catch (NoSuchElementException success){ |
584 |
} |
585 |
} |
586 |
|
587 |
/** |
588 |
* |
589 |
*/ |
590 |
public void testRemoveElement() { |
591 |
ArrayBlockingQueue q = populatedQueue(SIZE); |
592 |
for (int i = 1; i < SIZE; i+=2) { |
593 |
assertTrue(q.remove(new Integer(i))); |
594 |
} |
595 |
for (int i = 0; i < SIZE; i+=2) { |
596 |
assertTrue(q.remove(new Integer(i))); |
597 |
assertFalse(q.remove(new Integer(i+1))); |
598 |
} |
599 |
assertTrue(q.isEmpty()); |
600 |
} |
601 |
|
602 |
/** |
603 |
* |
604 |
*/ |
605 |
public void testContains() { |
606 |
ArrayBlockingQueue q = populatedQueue(SIZE); |
607 |
for (int i = 0; i < SIZE; ++i) { |
608 |
assertTrue(q.contains(new Integer(i))); |
609 |
q.poll(); |
610 |
assertFalse(q.contains(new Integer(i))); |
611 |
} |
612 |
} |
613 |
|
614 |
/** |
615 |
* |
616 |
*/ |
617 |
public void testClear() { |
618 |
ArrayBlockingQueue q = populatedQueue(SIZE); |
619 |
q.clear(); |
620 |
assertTrue(q.isEmpty()); |
621 |
assertEquals(0, q.size()); |
622 |
assertEquals(SIZE, q.remainingCapacity()); |
623 |
q.add(one); |
624 |
assertFalse(q.isEmpty()); |
625 |
q.clear(); |
626 |
assertTrue(q.isEmpty()); |
627 |
} |
628 |
|
629 |
/** |
630 |
* |
631 |
*/ |
632 |
public void testContainsAll() { |
633 |
ArrayBlockingQueue q = populatedQueue(SIZE); |
634 |
ArrayBlockingQueue p = new ArrayBlockingQueue(SIZE); |
635 |
for (int i = 0; i < SIZE; ++i) { |
636 |
assertTrue(q.containsAll(p)); |
637 |
assertFalse(p.containsAll(q)); |
638 |
p.add(new Integer(i)); |
639 |
} |
640 |
assertTrue(p.containsAll(q)); |
641 |
} |
642 |
|
643 |
/** |
644 |
* |
645 |
*/ |
646 |
public void testRetainAll() { |
647 |
ArrayBlockingQueue q = populatedQueue(SIZE); |
648 |
ArrayBlockingQueue p = populatedQueue(SIZE); |
649 |
for (int i = 0; i < SIZE; ++i) { |
650 |
boolean changed = q.retainAll(p); |
651 |
if (i == 0) |
652 |
assertFalse(changed); |
653 |
else |
654 |
assertTrue(changed); |
655 |
|
656 |
assertTrue(q.containsAll(p)); |
657 |
assertEquals(SIZE-i, q.size()); |
658 |
p.remove(); |
659 |
} |
660 |
} |
661 |
|
662 |
/** |
663 |
* |
664 |
*/ |
665 |
public void testRemoveAll() { |
666 |
for (int i = 1; i < SIZE; ++i) { |
667 |
ArrayBlockingQueue q = populatedQueue(SIZE); |
668 |
ArrayBlockingQueue p = populatedQueue(i); |
669 |
assertTrue(q.removeAll(p)); |
670 |
assertEquals(SIZE-i, q.size()); |
671 |
for (int j = 0; j < i; ++j) { |
672 |
Integer I = (Integer)(p.remove()); |
673 |
assertFalse(q.contains(I)); |
674 |
} |
675 |
} |
676 |
} |
677 |
|
678 |
|
679 |
/** |
680 |
* |
681 |
*/ |
682 |
public void testToArray() { |
683 |
ArrayBlockingQueue q = populatedQueue(SIZE); |
684 |
Object[] o = q.toArray(); |
685 |
try { |
686 |
for(int i = 0; i < o.length; i++) |
687 |
assertEquals(o[i], q.take()); |
688 |
} catch (InterruptedException e){ |
689 |
unexpectedException(); |
690 |
} |
691 |
} |
692 |
|
693 |
/** |
694 |
* |
695 |
*/ |
696 |
public void testToArray2() { |
697 |
ArrayBlockingQueue q = populatedQueue(SIZE); |
698 |
Integer[] ints = new Integer[SIZE]; |
699 |
ints = (Integer[])q.toArray(ints); |
700 |
try { |
701 |
for(int i = 0; i < ints.length; i++) |
702 |
assertEquals(ints[i], q.take()); |
703 |
} catch (InterruptedException e){ |
704 |
unexpectedException(); |
705 |
} |
706 |
} |
707 |
|
708 |
/** |
709 |
* |
710 |
*/ |
711 |
public void testIterator() { |
712 |
ArrayBlockingQueue q = populatedQueue(SIZE); |
713 |
Iterator it = q.iterator(); |
714 |
try { |
715 |
while(it.hasNext()){ |
716 |
assertEquals(it.next(), q.take()); |
717 |
} |
718 |
} catch (InterruptedException e){ |
719 |
unexpectedException(); |
720 |
} |
721 |
} |
722 |
|
723 |
/** |
724 |
* |
725 |
*/ |
726 |
public void testIteratorOrdering() { |
727 |
final ArrayBlockingQueue q = new ArrayBlockingQueue(3); |
728 |
q.add(one); |
729 |
q.add(two); |
730 |
q.add(three); |
731 |
|
732 |
assertEquals("queue should be full", 0, q.remainingCapacity()); |
733 |
|
734 |
int k = 0; |
735 |
for (Iterator it = q.iterator(); it.hasNext();) { |
736 |
int i = ((Integer)(it.next())).intValue(); |
737 |
assertEquals(++k, i); |
738 |
} |
739 |
assertEquals(3, k); |
740 |
} |
741 |
|
742 |
/** |
743 |
* |
744 |
*/ |
745 |
public void testWeaklyConsistentIteration () { |
746 |
final ArrayBlockingQueue q = new ArrayBlockingQueue(3); |
747 |
q.add(one); |
748 |
q.add(two); |
749 |
q.add(three); |
750 |
try { |
751 |
for (Iterator it = q.iterator(); it.hasNext();) { |
752 |
q.remove(); |
753 |
it.next(); |
754 |
} |
755 |
} |
756 |
catch (ConcurrentModificationException e) { |
757 |
unexpectedException(); |
758 |
} |
759 |
|
760 |
assertEquals(0, q.size()); |
761 |
} |
762 |
|
763 |
|
764 |
/** |
765 |
* |
766 |
*/ |
767 |
public void testToString() { |
768 |
ArrayBlockingQueue q = populatedQueue(SIZE); |
769 |
String s = q.toString(); |
770 |
for (int i = 0; i < SIZE; ++i) { |
771 |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
772 |
} |
773 |
} |
774 |
|
775 |
|
776 |
/** |
777 |
* |
778 |
*/ |
779 |
public void testOfferInExecutor() { |
780 |
|
781 |
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
782 |
|
783 |
q.add(one); |
784 |
q.add(two); |
785 |
|
786 |
ExecutorService executor = Executors.newFixedThreadPool(2); |
787 |
|
788 |
executor.execute(new Runnable() { |
789 |
public void run() { |
790 |
threadAssertFalse(q.offer(three)); |
791 |
try { |
792 |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
793 |
threadAssertEquals(0, q.remainingCapacity()); |
794 |
} |
795 |
catch (InterruptedException e) { |
796 |
threadUnexpectedException(); |
797 |
} |
798 |
} |
799 |
}); |
800 |
|
801 |
executor.execute(new Runnable() { |
802 |
public void run() { |
803 |
try { |
804 |
Thread.sleep(SMALL_DELAY_MS); |
805 |
threadAssertEquals(one, q.take()); |
806 |
} |
807 |
catch (InterruptedException e) { |
808 |
threadUnexpectedException(); |
809 |
} |
810 |
} |
811 |
}); |
812 |
|
813 |
joinPool(executor); |
814 |
|
815 |
} |
816 |
|
817 |
/** |
818 |
* |
819 |
*/ |
820 |
public void testPollInExecutor() { |
821 |
|
822 |
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
823 |
|
824 |
ExecutorService executor = Executors.newFixedThreadPool(2); |
825 |
|
826 |
executor.execute(new Runnable() { |
827 |
public void run() { |
828 |
threadAssertNull(q.poll()); |
829 |
try { |
830 |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
831 |
threadAssertTrue(q.isEmpty()); |
832 |
} |
833 |
catch (InterruptedException e) { |
834 |
threadUnexpectedException(); |
835 |
} |
836 |
} |
837 |
}); |
838 |
|
839 |
executor.execute(new Runnable() { |
840 |
public void run() { |
841 |
try { |
842 |
Thread.sleep(SMALL_DELAY_MS); |
843 |
q.put(one); |
844 |
} |
845 |
catch (InterruptedException e) { |
846 |
threadUnexpectedException(); |
847 |
} |
848 |
} |
849 |
}); |
850 |
|
851 |
joinPool(executor); |
852 |
|
853 |
} |
854 |
|
855 |
/** |
856 |
* |
857 |
*/ |
858 |
public void testSerialization() { |
859 |
ArrayBlockingQueue q = populatedQueue(SIZE); |
860 |
|
861 |
try { |
862 |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
863 |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
864 |
out.writeObject(q); |
865 |
out.close(); |
866 |
|
867 |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
868 |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
869 |
ArrayBlockingQueue r = (ArrayBlockingQueue)in.readObject(); |
870 |
assertEquals(q.size(), r.size()); |
871 |
while (!q.isEmpty()) |
872 |
assertEquals(q.remove(), r.remove()); |
873 |
} catch(Exception e){ |
874 |
unexpectedException(); |
875 |
} |
876 |
} |
877 |
|
878 |
|
879 |
} |