14 |
|
public class LinkedBlockingQueueTest extends JSR166TestCase { |
15 |
|
|
16 |
|
public static void main(String[] args) { |
17 |
< |
junit.textui.TestRunner.run (suite()); |
17 |
> |
junit.textui.TestRunner.run (suite()); |
18 |
|
} |
19 |
|
|
20 |
|
public static Test suite() { |
21 |
< |
return new TestSuite(LinkedBlockingQueueTest.class); |
21 |
> |
return new TestSuite(LinkedBlockingQueueTest.class); |
22 |
|
} |
23 |
|
|
24 |
|
|
29 |
|
private LinkedBlockingQueue populatedQueue(int n) { |
30 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(n); |
31 |
|
assertTrue(q.isEmpty()); |
32 |
< |
for (int i = 0; i < n; i++) |
33 |
< |
assertTrue(q.offer(new Integer(i))); |
32 |
> |
for (int i = 0; i < n; i++) |
33 |
> |
assertTrue(q.offer(new Integer(i))); |
34 |
|
assertFalse(q.isEmpty()); |
35 |
|
assertEquals(0, q.remainingCapacity()); |
36 |
< |
assertEquals(n, q.size()); |
36 |
> |
assertEquals(n, q.size()); |
37 |
|
return q; |
38 |
|
} |
39 |
|
|
145 |
|
* offer(null) throws NPE |
146 |
|
*/ |
147 |
|
public void testOfferNull() { |
148 |
< |
try { |
148 |
> |
try { |
149 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
150 |
|
q.offer(null); |
151 |
|
shouldThrow(); |
156 |
|
* add(null) throws NPE |
157 |
|
*/ |
158 |
|
public void testAddNull() { |
159 |
< |
try { |
159 |
> |
try { |
160 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
161 |
|
q.add(null); |
162 |
|
shouldThrow(); |
176 |
|
* add succeeds if not full; throws ISE if full |
177 |
|
*/ |
178 |
|
public void testAdd() { |
179 |
< |
try { |
179 |
> |
try { |
180 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
181 |
|
for (int i = 0; i < SIZE; ++i) { |
182 |
|
assertTrue(q.add(new Integer(i))); |
183 |
|
} |
184 |
|
assertEquals(0, q.remainingCapacity()); |
185 |
|
q.add(new Integer(SIZE)); |
186 |
< |
} catch (IllegalStateException success){ |
187 |
< |
} |
186 |
> |
} catch (IllegalStateException success) { |
187 |
> |
} |
188 |
|
} |
189 |
|
|
190 |
|
/** |
274 |
|
* put(null) throws NPE |
275 |
|
*/ |
276 |
|
public void testPutNull() { |
277 |
< |
try { |
277 |
> |
try { |
278 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
279 |
|
q.put(null); |
280 |
|
shouldThrow(); |
281 |
|
} |
282 |
< |
catch (NullPointerException success){ |
283 |
< |
} |
282 |
> |
catch (NullPointerException success) { |
283 |
> |
} |
284 |
|
catch (InterruptedException ie) { |
285 |
< |
unexpectedException(); |
285 |
> |
unexpectedException(); |
286 |
|
} |
287 |
|
} |
288 |
|
|
300 |
|
assertEquals(0, q.remainingCapacity()); |
301 |
|
} |
302 |
|
catch (InterruptedException ie) { |
303 |
< |
unexpectedException(); |
303 |
> |
unexpectedException(); |
304 |
|
} |
305 |
|
} |
306 |
|
|
319 |
|
} |
320 |
|
q.put(new Integer(SIZE)); |
321 |
|
threadShouldThrow(); |
322 |
< |
} catch (InterruptedException ie){ |
322 |
> |
} catch (InterruptedException ie) { |
323 |
|
threadAssertEquals(added, SIZE); |
324 |
|
} |
325 |
|
}}); |
330 |
|
t.join(); |
331 |
|
} |
332 |
|
catch (InterruptedException ie) { |
333 |
< |
unexpectedException(); |
333 |
> |
unexpectedException(); |
334 |
|
} |
335 |
|
} |
336 |
|
|
351 |
|
++added; |
352 |
|
q.put(new Object()); |
353 |
|
++added; |
354 |
< |
threadShouldThrow(); |
355 |
< |
} catch (InterruptedException e){ |
354 |
> |
threadShouldThrow(); |
355 |
> |
} catch (InterruptedException e) { |
356 |
|
threadAssertTrue(added >= 2); |
357 |
|
} |
358 |
|
} |
363 |
|
q.take(); |
364 |
|
t.interrupt(); |
365 |
|
t.join(); |
366 |
< |
} catch (Exception e){ |
366 |
> |
} catch (Exception e) { |
367 |
|
unexpectedException(); |
368 |
|
} |
369 |
|
} |
380 |
|
q.put(new Object()); |
381 |
|
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
382 |
|
q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
383 |
< |
threadShouldThrow(); |
384 |
< |
} catch (InterruptedException success){} |
383 |
> |
threadShouldThrow(); |
384 |
> |
} catch (InterruptedException success) {} |
385 |
|
} |
386 |
|
}); |
387 |
|
|
390 |
|
Thread.sleep(SMALL_DELAY_MS); |
391 |
|
t.interrupt(); |
392 |
|
t.join(); |
393 |
< |
} catch (Exception e){ |
393 |
> |
} catch (Exception e) { |
394 |
|
unexpectedException(); |
395 |
|
} |
396 |
|
} |
399 |
|
* take retrieves elements in FIFO order |
400 |
|
*/ |
401 |
|
public void testTake() { |
402 |
< |
try { |
402 |
> |
try { |
403 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
404 |
|
for (int i = 0; i < SIZE; ++i) { |
405 |
|
assertEquals(i, ((Integer)q.take()).intValue()); |
406 |
|
} |
407 |
< |
} catch (InterruptedException e){ |
408 |
< |
unexpectedException(); |
409 |
< |
} |
407 |
> |
} catch (InterruptedException e) { |
408 |
> |
unexpectedException(); |
409 |
> |
} |
410 |
|
} |
411 |
|
|
412 |
|
/** |
418 |
|
public void run() { |
419 |
|
try { |
420 |
|
q.take(); |
421 |
< |
threadShouldThrow(); |
422 |
< |
} catch (InterruptedException success){ } |
421 |
> |
threadShouldThrow(); |
422 |
> |
} catch (InterruptedException success) { } |
423 |
|
} |
424 |
|
}); |
425 |
|
try { |
427 |
|
Thread.sleep(SHORT_DELAY_MS); |
428 |
|
t.interrupt(); |
429 |
|
t.join(); |
430 |
< |
} catch (Exception e){ |
430 |
> |
} catch (Exception e) { |
431 |
|
unexpectedException(); |
432 |
|
} |
433 |
|
} |
445 |
|
} |
446 |
|
q.take(); |
447 |
|
threadShouldThrow(); |
448 |
< |
} catch (InterruptedException success){ |
448 |
> |
} catch (InterruptedException success) { |
449 |
|
} |
450 |
|
}}); |
451 |
|
t.start(); |
455 |
|
t.join(); |
456 |
|
} |
457 |
|
catch (InterruptedException ie) { |
458 |
< |
unexpectedException(); |
458 |
> |
unexpectedException(); |
459 |
|
} |
460 |
|
} |
461 |
|
|
468 |
|
for (int i = 0; i < SIZE; ++i) { |
469 |
|
assertEquals(i, ((Integer)q.poll()).intValue()); |
470 |
|
} |
471 |
< |
assertNull(q.poll()); |
471 |
> |
assertNull(q.poll()); |
472 |
|
} |
473 |
|
|
474 |
|
/** |
481 |
|
assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue()); |
482 |
|
} |
483 |
|
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
484 |
< |
} catch (InterruptedException e){ |
485 |
< |
unexpectedException(); |
486 |
< |
} |
484 |
> |
} catch (InterruptedException e) { |
485 |
> |
unexpectedException(); |
486 |
> |
} |
487 |
|
} |
488 |
|
|
489 |
|
/** |
496 |
|
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
497 |
|
} |
498 |
|
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
499 |
< |
} catch (InterruptedException e){ |
500 |
< |
unexpectedException(); |
501 |
< |
} |
499 |
> |
} catch (InterruptedException e) { |
500 |
> |
unexpectedException(); |
501 |
> |
} |
502 |
|
} |
503 |
|
|
504 |
|
/** |
514 |
|
threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
515 |
|
} |
516 |
|
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
517 |
< |
} catch (InterruptedException success){ |
517 |
> |
} catch (InterruptedException success) { |
518 |
|
} |
519 |
|
}}); |
520 |
|
t.start(); |
524 |
|
t.join(); |
525 |
|
} |
526 |
|
catch (InterruptedException ie) { |
527 |
< |
unexpectedException(); |
527 |
> |
unexpectedException(); |
528 |
|
} |
529 |
|
} |
530 |
|
|
540 |
|
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
541 |
|
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
542 |
|
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
543 |
< |
threadShouldThrow(); |
543 |
> |
threadShouldThrow(); |
544 |
|
} catch (InterruptedException success) { } |
545 |
|
} |
546 |
|
}); |
550 |
|
assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
551 |
|
t.interrupt(); |
552 |
|
t.join(); |
553 |
< |
} catch (Exception e){ |
553 |
> |
} catch (Exception e) { |
554 |
|
unexpectedException(); |
555 |
|
} |
556 |
|
} |
566 |
|
assertTrue(q.peek() == null || |
567 |
|
i != ((Integer)q.peek()).intValue()); |
568 |
|
} |
569 |
< |
assertNull(q.peek()); |
569 |
> |
assertNull(q.peek()); |
570 |
|
} |
571 |
|
|
572 |
|
/** |
596 |
|
try { |
597 |
|
q.remove(); |
598 |
|
shouldThrow(); |
599 |
< |
} catch (NoSuchElementException success){ |
600 |
< |
} |
599 |
> |
} catch (NoSuchElementException success) { |
600 |
> |
} |
601 |
|
} |
602 |
|
|
603 |
|
/** |
627 |
|
assertTrue(q.remove(new Integer(2))); |
628 |
|
assertTrue(q.add(new Integer(3))); |
629 |
|
assertTrue(q.take() != null); |
630 |
< |
} catch (Exception e){ |
630 |
> |
} catch (Exception e) { |
631 |
|
unexpectedException(); |
632 |
|
} |
633 |
|
} |
714 |
|
*/ |
715 |
|
public void testToArray() { |
716 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
717 |
< |
Object[] o = q.toArray(); |
718 |
< |
try { |
719 |
< |
for (int i = 0; i < o.length; i++) |
720 |
< |
assertEquals(o[i], q.take()); |
721 |
< |
} catch (InterruptedException e){ |
722 |
< |
unexpectedException(); |
723 |
< |
} |
717 |
> |
Object[] o = q.toArray(); |
718 |
> |
try { |
719 |
> |
for (int i = 0; i < o.length; i++) |
720 |
> |
assertEquals(o[i], q.take()); |
721 |
> |
} catch (InterruptedException e) { |
722 |
> |
unexpectedException(); |
723 |
> |
} |
724 |
|
} |
725 |
|
|
726 |
|
/** |
728 |
|
*/ |
729 |
|
public void testToArray2() { |
730 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
731 |
< |
Integer[] ints = new Integer[SIZE]; |
732 |
< |
ints = (Integer[])q.toArray(ints); |
733 |
< |
try { |
734 |
< |
for (int i = 0; i < ints.length; i++) |
735 |
< |
assertEquals(ints[i], q.take()); |
736 |
< |
} catch (InterruptedException e){ |
737 |
< |
unexpectedException(); |
738 |
< |
} |
731 |
> |
Integer[] ints = new Integer[SIZE]; |
732 |
> |
ints = (Integer[])q.toArray(ints); |
733 |
> |
try { |
734 |
> |
for (int i = 0; i < ints.length; i++) |
735 |
> |
assertEquals(ints[i], q.take()); |
736 |
> |
} catch (InterruptedException e) { |
737 |
> |
unexpectedException(); |
738 |
> |
} |
739 |
|
} |
740 |
|
|
741 |
|
/** |
742 |
|
* toArray(null) throws NPE |
743 |
|
*/ |
744 |
|
public void testToArray_BadArg() { |
745 |
< |
try { |
745 |
> |
try { |
746 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
747 |
< |
Object o[] = q.toArray(null); |
748 |
< |
shouldThrow(); |
749 |
< |
} catch (NullPointerException success){} |
747 |
> |
Object o[] = q.toArray(null); |
748 |
> |
shouldThrow(); |
749 |
> |
} catch (NullPointerException success) {} |
750 |
|
} |
751 |
|
|
752 |
|
/** |
753 |
|
* toArray with incompatible array type throws CCE |
754 |
|
*/ |
755 |
|
public void testToArray1_BadArg() { |
756 |
< |
try { |
756 |
> |
try { |
757 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
758 |
< |
Object o[] = q.toArray(new String[10] ); |
759 |
< |
shouldThrow(); |
760 |
< |
} catch (ArrayStoreException success){} |
758 |
> |
Object o[] = q.toArray(new String[10] ); |
759 |
> |
shouldThrow(); |
760 |
> |
} catch (ArrayStoreException success) {} |
761 |
|
} |
762 |
|
|
763 |
|
|
766 |
|
*/ |
767 |
|
public void testIterator() { |
768 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
769 |
< |
Iterator it = q.iterator(); |
770 |
< |
try { |
771 |
< |
while (it.hasNext()){ |
772 |
< |
assertEquals(it.next(), q.take()); |
773 |
< |
} |
774 |
< |
} catch (InterruptedException e){ |
775 |
< |
unexpectedException(); |
776 |
< |
} |
769 |
> |
Iterator it = q.iterator(); |
770 |
> |
try { |
771 |
> |
while (it.hasNext()) { |
772 |
> |
assertEquals(it.next(), q.take()); |
773 |
> |
} |
774 |
> |
} catch (InterruptedException e) { |
775 |
> |
unexpectedException(); |
776 |
> |
} |
777 |
|
} |
778 |
|
|
779 |
|
/** |
934 |
|
assertEquals(q.size(), r.size()); |
935 |
|
while (!q.isEmpty()) |
936 |
|
assertEquals(q.remove(), r.remove()); |
937 |
< |
} catch (Exception e){ |
937 |
> |
} catch (Exception e) { |
938 |
|
unexpectedException(); |
939 |
|
} |
940 |
|
} |
996 |
|
public void run() { |
997 |
|
try { |
998 |
|
q.put(new Integer(SIZE+1)); |
999 |
< |
} catch (InterruptedException ie){ |
999 |
> |
} catch (InterruptedException ie) { |
1000 |
|
threadUnexpectedException(); |
1001 |
|
} |
1002 |
|
} |
1010 |
|
assertEquals(l.get(i), new Integer(i)); |
1011 |
|
t.join(); |
1012 |
|
assertTrue(q.size() + l.size() >= SIZE); |
1013 |
< |
} catch (Exception e){ |
1013 |
> |
} catch (Exception e) { |
1014 |
|
unexpectedException(); |
1015 |
|
} |
1016 |
|
} |