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