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