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()); |
18 |
> |
junit.textui.TestRunner.run (suite()); |
19 |
|
} |
20 |
|
|
21 |
|
public static Test suite() { |
22 |
< |
return new TestSuite(LinkedBlockingQueueTest.class); |
22 |
> |
return new TestSuite(LinkedBlockingQueueTest.class); |
23 |
|
} |
24 |
|
|
25 |
|
|
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))); |
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()); |
37 |
> |
assertEquals(n, q.size()); |
38 |
|
return q; |
39 |
|
} |
40 |
|
|
146 |
|
* offer(null) throws NPE |
147 |
|
*/ |
148 |
|
public void testOfferNull() { |
149 |
< |
try { |
149 |
> |
try { |
150 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
151 |
|
q.offer(null); |
152 |
|
shouldThrow(); |
157 |
|
* add(null) throws NPE |
158 |
|
*/ |
159 |
|
public void testAddNull() { |
160 |
< |
try { |
160 |
> |
try { |
161 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(1); |
162 |
|
q.add(null); |
163 |
|
shouldThrow(); |
177 |
|
* add succeeds if not full; throws ISE if full |
178 |
|
*/ |
179 |
|
public void testAdd() { |
180 |
< |
try { |
180 |
> |
try { |
181 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
182 |
|
for (int i = 0; i < SIZE; ++i) { |
183 |
|
assertTrue(q.add(new Integer(i))); |
185 |
|
assertEquals(0, q.remainingCapacity()); |
186 |
|
q.add(new Integer(SIZE)); |
187 |
|
} catch (IllegalStateException success) { |
188 |
< |
} |
188 |
> |
} |
189 |
|
} |
190 |
|
|
191 |
|
/** |
275 |
|
* put(null) throws NPE |
276 |
|
*/ |
277 |
|
public void testPutNull() { |
278 |
< |
try { |
278 |
> |
try { |
279 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
280 |
|
q.put(null); |
281 |
|
shouldThrow(); |
282 |
|
} |
283 |
|
catch (NullPointerException success) { |
284 |
< |
} |
284 |
> |
} |
285 |
|
catch (InterruptedException ie) { |
286 |
< |
unexpectedException(); |
286 |
> |
unexpectedException(); |
287 |
|
} |
288 |
|
} |
289 |
|
|
301 |
|
assertEquals(0, q.remainingCapacity()); |
302 |
|
} |
303 |
|
catch (InterruptedException ie) { |
304 |
< |
unexpectedException(); |
304 |
> |
unexpectedException(); |
305 |
|
} |
306 |
|
} |
307 |
|
|
331 |
|
t.join(); |
332 |
|
} |
333 |
|
catch (InterruptedException ie) { |
334 |
< |
unexpectedException(); |
334 |
> |
unexpectedException(); |
335 |
|
} |
336 |
|
} |
337 |
|
|
352 |
|
++added; |
353 |
|
q.put(new Object()); |
354 |
|
++added; |
355 |
< |
threadShouldThrow(); |
355 |
> |
threadShouldThrow(); |
356 |
|
} catch (InterruptedException e) { |
357 |
|
threadAssertTrue(added >= 2); |
358 |
|
} |
379 |
|
try { |
380 |
|
q.put(new Object()); |
381 |
|
q.put(new Object()); |
382 |
< |
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
383 |
< |
q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
384 |
< |
threadShouldThrow(); |
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 |
|
}); |
400 |
|
* take retrieves elements in FIFO order |
401 |
|
*/ |
402 |
|
public void testTake() { |
403 |
< |
try { |
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 |
< |
} |
409 |
> |
unexpectedException(); |
410 |
> |
} |
411 |
|
} |
412 |
|
|
413 |
|
/** |
419 |
|
public void run() { |
420 |
|
try { |
421 |
|
q.take(); |
422 |
< |
threadShouldThrow(); |
422 |
> |
threadShouldThrow(); |
423 |
|
} catch (InterruptedException success) { } |
424 |
|
} |
425 |
|
}); |
456 |
|
t.join(); |
457 |
|
} |
458 |
|
catch (InterruptedException ie) { |
459 |
< |
unexpectedException(); |
459 |
> |
unexpectedException(); |
460 |
|
} |
461 |
|
} |
462 |
|
|
469 |
|
for (int i = 0; i < SIZE; ++i) { |
470 |
|
assertEquals(i, ((Integer)q.poll()).intValue()); |
471 |
|
} |
472 |
< |
assertNull(q.poll()); |
472 |
> |
assertNull(q.poll()); |
473 |
|
} |
474 |
|
|
475 |
|
/** |
479 |
|
try { |
480 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
481 |
|
for (int i = 0; i < SIZE; ++i) { |
482 |
< |
assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue()); |
482 |
> |
assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue()); |
483 |
|
} |
484 |
< |
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
484 |
> |
assertNull(q.poll(0, MILLISECONDS)); |
485 |
|
} catch (InterruptedException e) { |
486 |
< |
unexpectedException(); |
487 |
< |
} |
486 |
> |
unexpectedException(); |
487 |
> |
} |
488 |
|
} |
489 |
|
|
490 |
|
/** |
494 |
|
try { |
495 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
496 |
|
for (int i = 0; i < SIZE; ++i) { |
497 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
497 |
> |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
498 |
|
} |
499 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
499 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
500 |
|
} catch (InterruptedException e) { |
501 |
< |
unexpectedException(); |
502 |
< |
} |
501 |
> |
unexpectedException(); |
502 |
> |
} |
503 |
|
} |
504 |
|
|
505 |
|
/** |
512 |
|
try { |
513 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
514 |
|
for (int i = 0; i < SIZE; ++i) { |
515 |
< |
threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
515 |
> |
threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
516 |
|
} |
517 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
517 |
> |
threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
518 |
|
} catch (InterruptedException success) { |
519 |
|
} |
520 |
|
}}); |
525 |
|
t.join(); |
526 |
|
} |
527 |
|
catch (InterruptedException ie) { |
528 |
< |
unexpectedException(); |
528 |
> |
unexpectedException(); |
529 |
|
} |
530 |
|
} |
531 |
|
|
538 |
|
Thread t = new Thread(new Runnable() { |
539 |
|
public void run() { |
540 |
|
try { |
541 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
542 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
543 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
544 |
< |
threadShouldThrow(); |
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, TimeUnit.MILLISECONDS)); |
551 |
> |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
552 |
|
t.interrupt(); |
553 |
|
t.join(); |
554 |
|
} catch (Exception e) { |
567 |
|
assertTrue(q.peek() == null || |
568 |
|
i != ((Integer)q.peek()).intValue()); |
569 |
|
} |
570 |
< |
assertNull(q.peek()); |
570 |
> |
assertNull(q.peek()); |
571 |
|
} |
572 |
|
|
573 |
|
/** |
598 |
|
q.remove(); |
599 |
|
shouldThrow(); |
600 |
|
} catch (NoSuchElementException success) { |
601 |
< |
} |
601 |
> |
} |
602 |
|
} |
603 |
|
|
604 |
|
/** |
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 |
< |
} |
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 |
|
/** |
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 |
< |
} |
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 { |
746 |
> |
try { |
747 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
748 |
< |
Object o[] = q.toArray(null); |
749 |
< |
shouldThrow(); |
750 |
< |
} catch (NullPointerException success) {} |
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 { |
757 |
> |
try { |
758 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
759 |
< |
Object o[] = q.toArray(new String[10] ); |
760 |
< |
shouldThrow(); |
761 |
< |
} catch (ArrayStoreException success) {} |
759 |
> |
Object o[] = q.toArray(new String[10] ); |
760 |
> |
shouldThrow(); |
761 |
> |
} catch (ArrayStoreException success) {} |
762 |
|
} |
763 |
|
|
764 |
|
|
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 |
< |
} |
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 |
|
/** |
859 |
|
public void run() { |
860 |
|
threadAssertFalse(q.offer(three)); |
861 |
|
try { |
862 |
< |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
862 |
> |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
863 |
|
threadAssertEquals(0, q.remainingCapacity()); |
864 |
|
} |
865 |
|
catch (InterruptedException e) { |
893 |
|
public void run() { |
894 |
|
threadAssertNull(q.poll()); |
895 |
|
try { |
896 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
896 |
> |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
897 |
|
threadAssertTrue(q.isEmpty()); |
898 |
|
} |
899 |
|
catch (InterruptedException e) { |