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 |
import junit.framework.*; |
9 |
import java.util.*; |
10 |
import java.util.concurrent.*; |
11 |
import java.io.*; |
12 |
|
13 |
public class ArrayBlockingQueueTest extends JSR166TestCase { |
14 |
|
15 |
public static void main(String[] args) { |
16 |
junit.textui.TestRunner.run (suite()); |
17 |
} |
18 |
|
19 |
public static Test suite() { |
20 |
return new TestSuite(ArrayBlockingQueueTest.class); |
21 |
} |
22 |
|
23 |
/** |
24 |
* Create a queue of given size containing consecutive |
25 |
* Integers 0 ... n. |
26 |
*/ |
27 |
private ArrayBlockingQueue populatedQueue(int n) { |
28 |
ArrayBlockingQueue q = new ArrayBlockingQueue(n); |
29 |
assertTrue(q.isEmpty()); |
30 |
for(int i = 0; i < n; i++) |
31 |
assertTrue(q.offer(new Integer(i))); |
32 |
assertFalse(q.isEmpty()); |
33 |
assertEquals(0, q.remainingCapacity()); |
34 |
assertEquals(n, q.size()); |
35 |
return q; |
36 |
} |
37 |
|
38 |
public void testConstructor1(){ |
39 |
assertEquals(SIZE, new ArrayBlockingQueue(SIZE).remainingCapacity()); |
40 |
} |
41 |
|
42 |
public void testConstructor2(){ |
43 |
try { |
44 |
ArrayBlockingQueue q = new ArrayBlockingQueue(0); |
45 |
fail("Cannot make zero-sized"); |
46 |
} |
47 |
catch (IllegalArgumentException success) {} |
48 |
} |
49 |
|
50 |
public void testConstructor3(){ |
51 |
|
52 |
try { |
53 |
ArrayBlockingQueue q = new ArrayBlockingQueue(1, true, null); |
54 |
fail("Cannot make from null collection"); |
55 |
} |
56 |
catch (NullPointerException success) {} |
57 |
} |
58 |
|
59 |
public void testConstructor4(){ |
60 |
try { |
61 |
Integer[] ints = new Integer[SIZE]; |
62 |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints)); |
63 |
fail("Cannot make with null elements"); |
64 |
} |
65 |
catch (NullPointerException success) {} |
66 |
} |
67 |
|
68 |
public void testConstructor5(){ |
69 |
try { |
70 |
Integer[] ints = new Integer[SIZE]; |
71 |
for (int i = 0; i < SIZE-1; ++i) |
72 |
ints[i] = new Integer(i); |
73 |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints)); |
74 |
fail("Cannot make with null elements"); |
75 |
} |
76 |
catch (NullPointerException success) {} |
77 |
} |
78 |
|
79 |
public void testConstructor6(){ |
80 |
try { |
81 |
Integer[] ints = new Integer[SIZE]; |
82 |
for (int i = 0; i < SIZE; ++i) |
83 |
ints[i] = new Integer(i); |
84 |
ArrayBlockingQueue q = new ArrayBlockingQueue(1, false, Arrays.asList(ints)); |
85 |
fail("Cannot make with insufficient capacity"); |
86 |
} |
87 |
catch (IllegalArgumentException success) {} |
88 |
} |
89 |
|
90 |
public void testConstructor7(){ |
91 |
try { |
92 |
Integer[] ints = new Integer[SIZE]; |
93 |
for (int i = 0; i < SIZE; ++i) |
94 |
ints[i] = new Integer(i); |
95 |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, true, Arrays.asList(ints)); |
96 |
for (int i = 0; i < SIZE; ++i) |
97 |
assertEquals(ints[i], q.poll()); |
98 |
} |
99 |
finally {} |
100 |
} |
101 |
|
102 |
public void testEmptyFull() { |
103 |
ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
104 |
assertTrue(q.isEmpty()); |
105 |
assertEquals("should have room for 2", 2, q.remainingCapacity()); |
106 |
q.add(one); |
107 |
assertFalse(q.isEmpty()); |
108 |
q.add(two); |
109 |
assertFalse(q.isEmpty()); |
110 |
assertEquals("queue should be full", 0, q.remainingCapacity()); |
111 |
assertFalse("offer should be rejected", q.offer(three)); |
112 |
} |
113 |
|
114 |
public void testRemainingCapacity(){ |
115 |
ArrayBlockingQueue q = populatedQueue(SIZE); |
116 |
for (int i = 0; i < SIZE; ++i) { |
117 |
assertEquals(i, q.remainingCapacity()); |
118 |
assertEquals(SIZE-i, q.size()); |
119 |
q.remove(); |
120 |
} |
121 |
for (int i = 0; i < SIZE; ++i) { |
122 |
assertEquals(SIZE-i, q.remainingCapacity()); |
123 |
assertEquals(i, q.size()); |
124 |
q.add(new Integer(i)); |
125 |
} |
126 |
} |
127 |
|
128 |
public void testOfferNull(){ |
129 |
try { |
130 |
ArrayBlockingQueue q = new ArrayBlockingQueue(1); |
131 |
q.offer(null); |
132 |
fail("should throw NPE"); |
133 |
} catch (NullPointerException success) { } |
134 |
} |
135 |
|
136 |
public void testOffer(){ |
137 |
ArrayBlockingQueue q = new ArrayBlockingQueue(1); |
138 |
assertTrue(q.offer(zero)); |
139 |
assertFalse(q.offer(one)); |
140 |
} |
141 |
|
142 |
public void testAdd(){ |
143 |
try { |
144 |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
145 |
for (int i = 0; i < SIZE; ++i) { |
146 |
assertTrue(q.add(new Integer(i))); |
147 |
} |
148 |
assertEquals(0, q.remainingCapacity()); |
149 |
q.add(new Integer(SIZE)); |
150 |
} catch (IllegalStateException success){ |
151 |
} |
152 |
} |
153 |
|
154 |
public void testAddAll1(){ |
155 |
try { |
156 |
ArrayBlockingQueue q = new ArrayBlockingQueue(1); |
157 |
q.addAll(null); |
158 |
fail("Cannot add null collection"); |
159 |
} |
160 |
catch (NullPointerException success) {} |
161 |
} |
162 |
public void testAddAll2(){ |
163 |
try { |
164 |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
165 |
Integer[] ints = new Integer[SIZE]; |
166 |
q.addAll(Arrays.asList(ints)); |
167 |
fail("Cannot add null elements"); |
168 |
} |
169 |
catch (NullPointerException success) {} |
170 |
} |
171 |
public void testAddAll3(){ |
172 |
try { |
173 |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
174 |
Integer[] ints = new Integer[SIZE]; |
175 |
for (int i = 0; i < SIZE-1; ++i) |
176 |
ints[i] = new Integer(i); |
177 |
q.addAll(Arrays.asList(ints)); |
178 |
fail("Cannot add null elements"); |
179 |
} |
180 |
catch (NullPointerException success) {} |
181 |
} |
182 |
public void testAddAll4(){ |
183 |
try { |
184 |
ArrayBlockingQueue q = new ArrayBlockingQueue(1); |
185 |
Integer[] ints = new Integer[SIZE]; |
186 |
for (int i = 0; i < SIZE; ++i) |
187 |
ints[i] = new Integer(i); |
188 |
q.addAll(Arrays.asList(ints)); |
189 |
fail("Cannot add with insufficient capacity"); |
190 |
} |
191 |
catch (IllegalStateException success) {} |
192 |
} |
193 |
public void testAddAll5(){ |
194 |
try { |
195 |
Integer[] empty = new Integer[0]; |
196 |
Integer[] ints = new Integer[SIZE]; |
197 |
for (int i = 0; i < SIZE; ++i) |
198 |
ints[i] = new Integer(i); |
199 |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
200 |
assertFalse(q.addAll(Arrays.asList(empty))); |
201 |
assertTrue(q.addAll(Arrays.asList(ints))); |
202 |
for (int i = 0; i < SIZE; ++i) |
203 |
assertEquals(ints[i], q.poll()); |
204 |
} |
205 |
finally {} |
206 |
} |
207 |
|
208 |
public void testPutNull() { |
209 |
try { |
210 |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
211 |
q.put(null); |
212 |
fail("put should throw NPE"); |
213 |
} |
214 |
catch (NullPointerException success){ |
215 |
} |
216 |
catch (InterruptedException ie) { |
217 |
fail("Unexpected exception"); |
218 |
} |
219 |
} |
220 |
|
221 |
public void testPut() { |
222 |
try { |
223 |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
224 |
for (int i = 0; i < SIZE; ++i) { |
225 |
Integer I = new Integer(i); |
226 |
q.put(I); |
227 |
assertTrue(q.contains(I)); |
228 |
} |
229 |
assertEquals(0, q.remainingCapacity()); |
230 |
} |
231 |
catch (InterruptedException ie) { |
232 |
fail("Unexpected exception"); |
233 |
} |
234 |
} |
235 |
|
236 |
public void testBlockingPut(){ |
237 |
Thread t = new Thread(new Runnable() { |
238 |
public void run() { |
239 |
int added = 0; |
240 |
try { |
241 |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
242 |
for (int i = 0; i < SIZE; ++i) { |
243 |
q.put(new Integer(i)); |
244 |
++added; |
245 |
} |
246 |
q.put(new Integer(SIZE)); |
247 |
threadFail("put should block"); |
248 |
} catch (InterruptedException ie){ |
249 |
threadAssertEquals(added, SIZE); |
250 |
} |
251 |
}}); |
252 |
try { |
253 |
t.start(); |
254 |
Thread.sleep(SHORT_DELAY_MS); |
255 |
t.interrupt(); |
256 |
t.join(); |
257 |
} |
258 |
catch (InterruptedException ie) { |
259 |
fail("Unexpected exception"); |
260 |
} |
261 |
} |
262 |
|
263 |
public void testPutWithTake() { |
264 |
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
265 |
Thread t = new Thread(new Runnable() { |
266 |
public void run(){ |
267 |
int added = 0; |
268 |
try { |
269 |
q.put(new Object()); |
270 |
++added; |
271 |
q.put(new Object()); |
272 |
++added; |
273 |
q.put(new Object()); |
274 |
++added; |
275 |
q.put(new Object()); |
276 |
++added; |
277 |
threadFail("Should block"); |
278 |
} catch (InterruptedException e){ |
279 |
threadAssertTrue(added >= 2); |
280 |
} |
281 |
} |
282 |
}); |
283 |
try { |
284 |
t.start(); |
285 |
Thread.sleep(SHORT_DELAY_MS); |
286 |
q.take(); |
287 |
t.interrupt(); |
288 |
t.join(); |
289 |
} catch (Exception e){ |
290 |
fail("Unexpected exception"); |
291 |
} |
292 |
} |
293 |
|
294 |
public void testTimedOffer() { |
295 |
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
296 |
Thread t = new Thread(new Runnable() { |
297 |
public void run(){ |
298 |
try { |
299 |
q.put(new Object()); |
300 |
q.put(new Object()); |
301 |
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS)); |
302 |
q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
303 |
threadFail("Should block"); |
304 |
} catch (InterruptedException success){} |
305 |
} |
306 |
}); |
307 |
|
308 |
try { |
309 |
t.start(); |
310 |
Thread.sleep(SHORT_DELAY_MS); |
311 |
t.interrupt(); |
312 |
t.join(); |
313 |
} catch (Exception e){ |
314 |
fail("Unexpected exception"); |
315 |
} |
316 |
} |
317 |
|
318 |
public void testTake(){ |
319 |
try { |
320 |
ArrayBlockingQueue q = populatedQueue(SIZE); |
321 |
for (int i = 0; i < SIZE; ++i) { |
322 |
assertEquals(i, ((Integer)q.take()).intValue()); |
323 |
} |
324 |
} catch (InterruptedException e){ |
325 |
fail("Unexpected exception"); |
326 |
} |
327 |
} |
328 |
|
329 |
public void testTakeFromEmpty() { |
330 |
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
331 |
Thread t = new Thread(new Runnable() { |
332 |
public void run(){ |
333 |
try { |
334 |
q.take(); |
335 |
threadFail("Should block"); |
336 |
} catch (InterruptedException success){ } |
337 |
} |
338 |
}); |
339 |
try { |
340 |
t.start(); |
341 |
Thread.sleep(SHORT_DELAY_MS); |
342 |
t.interrupt(); |
343 |
t.join(); |
344 |
} catch (Exception e){ |
345 |
fail("Unexpected exception"); |
346 |
} |
347 |
} |
348 |
|
349 |
public void testBlockingTake(){ |
350 |
Thread t = new Thread(new Runnable() { |
351 |
public void run() { |
352 |
try { |
353 |
ArrayBlockingQueue q = populatedQueue(SIZE); |
354 |
for (int i = 0; i < SIZE; ++i) { |
355 |
threadAssertEquals(i, ((Integer)q.take()).intValue()); |
356 |
} |
357 |
q.take(); |
358 |
threadFail("take should block"); |
359 |
} catch (InterruptedException success){ |
360 |
} |
361 |
}}); |
362 |
try { |
363 |
t.start(); |
364 |
Thread.sleep(SHORT_DELAY_MS); |
365 |
t.interrupt(); |
366 |
t.join(); |
367 |
} |
368 |
catch (InterruptedException ie) { |
369 |
fail("Unexpected exception"); |
370 |
} |
371 |
} |
372 |
|
373 |
|
374 |
public void testPoll(){ |
375 |
ArrayBlockingQueue q = populatedQueue(SIZE); |
376 |
for (int i = 0; i < SIZE; ++i) { |
377 |
assertEquals(i, ((Integer)q.poll()).intValue()); |
378 |
} |
379 |
assertNull(q.poll()); |
380 |
} |
381 |
|
382 |
public void testTimedPoll0() { |
383 |
try { |
384 |
ArrayBlockingQueue q = populatedQueue(SIZE); |
385 |
for (int i = 0; i < SIZE; ++i) { |
386 |
assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue()); |
387 |
} |
388 |
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
389 |
} catch (InterruptedException e){ |
390 |
fail("Unexpected exception"); |
391 |
} |
392 |
} |
393 |
|
394 |
public void testTimedPoll() { |
395 |
try { |
396 |
ArrayBlockingQueue q = populatedQueue(SIZE); |
397 |
for (int i = 0; i < SIZE; ++i) { |
398 |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
399 |
} |
400 |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
401 |
} catch (InterruptedException e){ |
402 |
fail("Unexpected exception"); |
403 |
} |
404 |
} |
405 |
|
406 |
public void testInterruptedTimedPoll(){ |
407 |
Thread t = new Thread(new Runnable() { |
408 |
public void run() { |
409 |
try { |
410 |
ArrayBlockingQueue q = populatedQueue(SIZE); |
411 |
for (int i = 0; i < SIZE; ++i) { |
412 |
threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
413 |
} |
414 |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
415 |
} catch (InterruptedException success){ |
416 |
} |
417 |
}}); |
418 |
try { |
419 |
t.start(); |
420 |
Thread.sleep(SHORT_DELAY_MS); |
421 |
t.interrupt(); |
422 |
t.join(); |
423 |
} |
424 |
catch (InterruptedException ie) { |
425 |
fail("Unexpected exception"); |
426 |
} |
427 |
} |
428 |
|
429 |
public void testTimedPollWithOffer(){ |
430 |
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
431 |
Thread t = new Thread(new Runnable() { |
432 |
public void run(){ |
433 |
try { |
434 |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
435 |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
436 |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
437 |
threadFail("Should block"); |
438 |
} catch (InterruptedException success) { } |
439 |
} |
440 |
}); |
441 |
try { |
442 |
t.start(); |
443 |
Thread.sleep(SMALL_DELAY_MS); |
444 |
assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
445 |
t.interrupt(); |
446 |
t.join(); |
447 |
} catch (Exception e){ |
448 |
fail("Unexpected exception"); |
449 |
} |
450 |
} |
451 |
|
452 |
|
453 |
public void testPeek(){ |
454 |
ArrayBlockingQueue q = populatedQueue(SIZE); |
455 |
for (int i = 0; i < SIZE; ++i) { |
456 |
assertEquals(i, ((Integer)q.peek()).intValue()); |
457 |
q.poll(); |
458 |
assertTrue(q.peek() == null || |
459 |
i != ((Integer)q.peek()).intValue()); |
460 |
} |
461 |
assertNull(q.peek()); |
462 |
} |
463 |
|
464 |
public void testElement(){ |
465 |
ArrayBlockingQueue q = populatedQueue(SIZE); |
466 |
for (int i = 0; i < SIZE; ++i) { |
467 |
assertEquals(i, ((Integer)q.element()).intValue()); |
468 |
q.poll(); |
469 |
} |
470 |
try { |
471 |
q.element(); |
472 |
fail("no such element"); |
473 |
} |
474 |
catch (NoSuchElementException success) {} |
475 |
} |
476 |
|
477 |
public void testRemove(){ |
478 |
ArrayBlockingQueue q = populatedQueue(SIZE); |
479 |
for (int i = 0; i < SIZE; ++i) { |
480 |
assertEquals(i, ((Integer)q.remove()).intValue()); |
481 |
} |
482 |
try { |
483 |
q.remove(); |
484 |
fail("remove should throw"); |
485 |
} catch (NoSuchElementException success){ |
486 |
} |
487 |
} |
488 |
|
489 |
public void testRemoveElement(){ |
490 |
ArrayBlockingQueue q = populatedQueue(SIZE); |
491 |
for (int i = 1; i < SIZE; i+=2) { |
492 |
assertTrue(q.remove(new Integer(i))); |
493 |
} |
494 |
for (int i = 0; i < SIZE; i+=2) { |
495 |
assertTrue(q.remove(new Integer(i))); |
496 |
assertFalse(q.remove(new Integer(i+1))); |
497 |
} |
498 |
assertTrue(q.isEmpty()); |
499 |
} |
500 |
|
501 |
public void testContains(){ |
502 |
ArrayBlockingQueue q = populatedQueue(SIZE); |
503 |
for (int i = 0; i < SIZE; ++i) { |
504 |
assertTrue(q.contains(new Integer(i))); |
505 |
q.poll(); |
506 |
assertFalse(q.contains(new Integer(i))); |
507 |
} |
508 |
} |
509 |
|
510 |
public void testClear(){ |
511 |
ArrayBlockingQueue q = populatedQueue(SIZE); |
512 |
q.clear(); |
513 |
assertTrue(q.isEmpty()); |
514 |
assertEquals(0, q.size()); |
515 |
assertEquals(SIZE, q.remainingCapacity()); |
516 |
q.add(one); |
517 |
assertFalse(q.isEmpty()); |
518 |
q.clear(); |
519 |
assertTrue(q.isEmpty()); |
520 |
} |
521 |
|
522 |
public void testContainsAll(){ |
523 |
ArrayBlockingQueue q = populatedQueue(SIZE); |
524 |
ArrayBlockingQueue p = new ArrayBlockingQueue(SIZE); |
525 |
for (int i = 0; i < SIZE; ++i) { |
526 |
assertTrue(q.containsAll(p)); |
527 |
assertFalse(p.containsAll(q)); |
528 |
p.add(new Integer(i)); |
529 |
} |
530 |
assertTrue(p.containsAll(q)); |
531 |
} |
532 |
|
533 |
public void testRetainAll(){ |
534 |
ArrayBlockingQueue q = populatedQueue(SIZE); |
535 |
ArrayBlockingQueue p = populatedQueue(SIZE); |
536 |
for (int i = 0; i < SIZE; ++i) { |
537 |
boolean changed = q.retainAll(p); |
538 |
if (i == 0) |
539 |
assertFalse(changed); |
540 |
else |
541 |
assertTrue(changed); |
542 |
|
543 |
assertTrue(q.containsAll(p)); |
544 |
assertEquals(SIZE-i, q.size()); |
545 |
p.remove(); |
546 |
} |
547 |
} |
548 |
|
549 |
public void testRemoveAll(){ |
550 |
for (int i = 1; i < SIZE; ++i) { |
551 |
ArrayBlockingQueue q = populatedQueue(SIZE); |
552 |
ArrayBlockingQueue p = populatedQueue(i); |
553 |
assertTrue(q.removeAll(p)); |
554 |
assertEquals(SIZE-i, q.size()); |
555 |
for (int j = 0; j < i; ++j) { |
556 |
Integer I = (Integer)(p.remove()); |
557 |
assertFalse(q.contains(I)); |
558 |
} |
559 |
} |
560 |
} |
561 |
|
562 |
|
563 |
public void testToArray(){ |
564 |
ArrayBlockingQueue q = populatedQueue(SIZE); |
565 |
Object[] o = q.toArray(); |
566 |
try { |
567 |
for(int i = 0; i < o.length; i++) |
568 |
assertEquals(o[i], q.take()); |
569 |
} catch (InterruptedException e){ |
570 |
fail("Unexpected exception"); |
571 |
} |
572 |
} |
573 |
|
574 |
public void testToArray2(){ |
575 |
ArrayBlockingQueue q = populatedQueue(SIZE); |
576 |
Integer[] ints = new Integer[SIZE]; |
577 |
ints = (Integer[])q.toArray(ints); |
578 |
try { |
579 |
for(int i = 0; i < ints.length; i++) |
580 |
assertEquals(ints[i], q.take()); |
581 |
} catch (InterruptedException e){ |
582 |
fail("Unexpected exception"); |
583 |
} |
584 |
} |
585 |
|
586 |
public void testIterator(){ |
587 |
ArrayBlockingQueue q = populatedQueue(SIZE); |
588 |
Iterator it = q.iterator(); |
589 |
try { |
590 |
while(it.hasNext()){ |
591 |
assertEquals(it.next(), q.take()); |
592 |
} |
593 |
} catch (InterruptedException e){ |
594 |
fail("Unexpected exception"); |
595 |
} |
596 |
} |
597 |
|
598 |
public void testIteratorOrdering() { |
599 |
|
600 |
final ArrayBlockingQueue q = new ArrayBlockingQueue(3); |
601 |
|
602 |
q.add(one); |
603 |
q.add(two); |
604 |
q.add(three); |
605 |
|
606 |
assertEquals("queue should be full", 0, q.remainingCapacity()); |
607 |
|
608 |
int k = 0; |
609 |
for (Iterator it = q.iterator(); it.hasNext();) { |
610 |
int i = ((Integer)(it.next())).intValue(); |
611 |
assertEquals("items should come out in order", ++k, i); |
612 |
} |
613 |
|
614 |
assertEquals("should go through 3 elements", 3, k); |
615 |
} |
616 |
|
617 |
public void testWeaklyConsistentIteration () { |
618 |
|
619 |
final ArrayBlockingQueue q = new ArrayBlockingQueue(3); |
620 |
|
621 |
q.add(one); |
622 |
q.add(two); |
623 |
q.add(three); |
624 |
|
625 |
try { |
626 |
for (Iterator it = q.iterator(); it.hasNext();) { |
627 |
q.remove(); |
628 |
it.next(); |
629 |
} |
630 |
} |
631 |
catch (ConcurrentModificationException e) { |
632 |
fail("weakly consistent iterator; should not get CME"); |
633 |
} |
634 |
|
635 |
assertEquals("queue should be empty again", 0, q.size()); |
636 |
} |
637 |
|
638 |
|
639 |
public void testToString(){ |
640 |
ArrayBlockingQueue q = populatedQueue(SIZE); |
641 |
String s = q.toString(); |
642 |
for (int i = 0; i < SIZE; ++i) { |
643 |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
644 |
} |
645 |
} |
646 |
|
647 |
|
648 |
public void testOfferInExecutor() { |
649 |
|
650 |
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
651 |
|
652 |
q.add(one); |
653 |
q.add(two); |
654 |
|
655 |
ExecutorService executor = Executors.newFixedThreadPool(2); |
656 |
|
657 |
executor.execute(new Runnable() { |
658 |
public void run() { |
659 |
threadAssertFalse(q.offer(three)); |
660 |
try { |
661 |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
662 |
threadAssertEquals(0, q.remainingCapacity()); |
663 |
} |
664 |
catch (InterruptedException e) { |
665 |
threadFail("should not be interrupted"); |
666 |
} |
667 |
} |
668 |
}); |
669 |
|
670 |
executor.execute(new Runnable() { |
671 |
public void run() { |
672 |
try { |
673 |
Thread.sleep(SMALL_DELAY_MS); |
674 |
threadAssertEquals(one, q.take()); |
675 |
} |
676 |
catch (InterruptedException e) { |
677 |
threadFail("should not be interrupted"); |
678 |
} |
679 |
} |
680 |
}); |
681 |
|
682 |
joinPool(executor); |
683 |
|
684 |
} |
685 |
|
686 |
public void testPollInExecutor() { |
687 |
|
688 |
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
689 |
|
690 |
ExecutorService executor = Executors.newFixedThreadPool(2); |
691 |
|
692 |
executor.execute(new Runnable() { |
693 |
public void run() { |
694 |
threadAssertNull(q.poll()); |
695 |
try { |
696 |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
697 |
threadAssertTrue(q.isEmpty()); |
698 |
} |
699 |
catch (InterruptedException e) { |
700 |
threadFail("should not be interrupted"); |
701 |
} |
702 |
} |
703 |
}); |
704 |
|
705 |
executor.execute(new Runnable() { |
706 |
public void run() { |
707 |
try { |
708 |
Thread.sleep(SMALL_DELAY_MS); |
709 |
q.put(one); |
710 |
} |
711 |
catch (InterruptedException e) { |
712 |
threadFail("should not be interrupted"); |
713 |
} |
714 |
} |
715 |
}); |
716 |
|
717 |
joinPool(executor); |
718 |
|
719 |
} |
720 |
|
721 |
public void testSerialization() { |
722 |
ArrayBlockingQueue q = populatedQueue(SIZE); |
723 |
|
724 |
try { |
725 |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
726 |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
727 |
out.writeObject(q); |
728 |
out.close(); |
729 |
|
730 |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
731 |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
732 |
ArrayBlockingQueue r = (ArrayBlockingQueue)in.readObject(); |
733 |
assertEquals(q.size(), r.size()); |
734 |
while (!q.isEmpty()) |
735 |
assertEquals(q.remove(), r.remove()); |
736 |
} catch(Exception e){ |
737 |
fail("unexpected exception"); |
738 |
} |
739 |
} |
740 |
|
741 |
|
742 |
} |