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