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() { |
214 |
|
shouldThrow(); |
215 |
|
} catch (NullPointerException success) {} |
216 |
|
} |
217 |
+ |
|
218 |
|
/** |
219 |
|
* addAll of a collection with any null elements throws NPE after |
220 |
|
* possibly adding some elements |
229 |
|
shouldThrow(); |
230 |
|
} catch (NullPointerException success) {} |
231 |
|
} |
232 |
+ |
|
233 |
|
/** |
234 |
|
* addAll throws ISE if not enough room |
235 |
|
*/ |
243 |
|
shouldThrow(); |
244 |
|
} catch (IllegalStateException success) {} |
245 |
|
} |
246 |
+ |
|
247 |
|
/** |
248 |
|
* Queue contains all elements, in traversal order, of successful addAll |
249 |
|
*/ |
362 |
|
public void testTake() throws InterruptedException { |
363 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
364 |
|
for (int i = 0; i < SIZE; ++i) { |
365 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
365 |
> |
assertEquals(i, q.take()); |
366 |
|
} |
367 |
|
} |
368 |
|
|
390 |
|
Thread t = new Thread(new CheckedRunnable() { |
391 |
|
public void realRun() throws InterruptedException { |
392 |
|
for (int i = 0; i < SIZE; ++i) { |
393 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
393 |
> |
assertEquals(i, q.take()); |
394 |
|
} |
395 |
|
try { |
396 |
|
q.take(); |
410 |
|
public void testPoll() { |
411 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
412 |
|
for (int i = 0; i < SIZE; ++i) { |
413 |
< |
assertEquals(i, ((Integer)q.poll()).intValue()); |
413 |
> |
assertEquals(i, q.poll()); |
414 |
|
} |
415 |
|
assertNull(q.poll()); |
416 |
|
} |
421 |
|
public void testTimedPoll0() throws InterruptedException { |
422 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
423 |
|
for (int i = 0; i < SIZE; ++i) { |
424 |
< |
assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue()); |
424 |
> |
assertEquals(i, q.poll(0, MILLISECONDS)); |
425 |
|
} |
426 |
|
assertNull(q.poll(0, MILLISECONDS)); |
427 |
|
} |
432 |
|
public void testTimedPoll() throws InterruptedException { |
433 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
434 |
|
for (int i = 0; i < SIZE; ++i) { |
435 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
435 |
> |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
436 |
|
} |
437 |
|
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
438 |
|
} |
446 |
|
public void realRun() throws InterruptedException { |
447 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
448 |
|
for (int i = 0; i < SIZE; ++i) { |
449 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
449 |
> |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
450 |
|
} |
451 |
|
try { |
452 |
|
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
489 |
|
public void testPeek() { |
490 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
491 |
|
for (int i = 0; i < SIZE; ++i) { |
492 |
< |
assertEquals(i, ((Integer)q.peek()).intValue()); |
493 |
< |
q.poll(); |
492 |
> |
assertEquals(i, q.peek()); |
493 |
> |
assertEquals(i, q.poll()); |
494 |
|
assertTrue(q.peek() == null || |
495 |
< |
i != ((Integer)q.peek()).intValue()); |
495 |
> |
!q.peek().equals(i)); |
496 |
|
} |
497 |
|
assertNull(q.peek()); |
498 |
|
} |
503 |
|
public void testElement() { |
504 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
505 |
|
for (int i = 0; i < SIZE; ++i) { |
506 |
< |
assertEquals(i, ((Integer)q.element()).intValue()); |
507 |
< |
q.poll(); |
506 |
> |
assertEquals(i, q.element()); |
507 |
> |
assertEquals(i, q.poll()); |
508 |
|
} |
509 |
|
try { |
510 |
|
q.element(); |
518 |
|
public void testRemove() { |
519 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
520 |
|
for (int i = 0; i < SIZE; ++i) { |
521 |
< |
assertEquals(i, ((Integer)q.remove()).intValue()); |
521 |
> |
assertEquals(i, q.remove()); |
522 |
|
} |
523 |
|
try { |
524 |
|
q.remove(); |
656 |
|
* toArray(null) throws NPE |
657 |
|
*/ |
658 |
|
public void testToArray_BadArg() { |
659 |
+ |
LinkedBlockingQueue q = populatedQueue(SIZE); |
660 |
|
try { |
657 |
– |
LinkedBlockingQueue q = populatedQueue(SIZE); |
661 |
|
Object o[] = q.toArray(null); |
662 |
|
shouldThrow(); |
663 |
|
} catch (NullPointerException success) {} |
667 |
|
* toArray with incompatible array type throws CCE |
668 |
|
*/ |
669 |
|
public void testToArray1_BadArg() { |
670 |
+ |
LinkedBlockingQueue q = populatedQueue(SIZE); |
671 |
|
try { |
672 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
669 |
< |
Object o[] = q.toArray(new String[10] ); |
672 |
> |
Object o[] = q.toArray(new String[10]); |
673 |
|
shouldThrow(); |
674 |
|
} catch (ArrayStoreException success) {} |
675 |
|
} |
689 |
|
/** |
690 |
|
* iterator.remove removes current element |
691 |
|
*/ |
692 |
< |
public void testIteratorRemove () { |
692 |
> |
public void testIteratorRemove() { |
693 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(3); |
694 |
|
q.add(two); |
695 |
|
q.add(one); |
700 |
|
it.remove(); |
701 |
|
|
702 |
|
it = q.iterator(); |
703 |
< |
assertEquals(it.next(), one); |
704 |
< |
assertEquals(it.next(), three); |
703 |
> |
assertSame(it.next(), one); |
704 |
> |
assertSame(it.next(), three); |
705 |
|
assertFalse(it.hasNext()); |
706 |
|
} |
707 |
|
|
717 |
|
assertEquals(0, q.remainingCapacity()); |
718 |
|
int k = 0; |
719 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
720 |
< |
int i = ((Integer)(it.next())).intValue(); |
718 |
< |
assertEquals(++k, i); |
720 |
> |
assertEquals(++k, it.next()); |
721 |
|
} |
722 |
|
assertEquals(3, k); |
723 |
|
} |
725 |
|
/** |
726 |
|
* Modifications do not cause iterators to fail |
727 |
|
*/ |
728 |
< |
public void testWeaklyConsistentIteration () { |
728 |
> |
public void testWeaklyConsistentIteration() { |
729 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(3); |
730 |
|
q.add(one); |
731 |
|
q.add(two); |
760 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
761 |
|
executor.execute(new CheckedRunnable() { |
762 |
|
public void realRun() throws InterruptedException { |
763 |
< |
threadAssertFalse(q.offer(three)); |
764 |
< |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
765 |
< |
threadAssertEquals(0, q.remainingCapacity()); |
763 |
> |
assertFalse(q.offer(three)); |
764 |
> |
assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
765 |
> |
assertEquals(0, q.remainingCapacity()); |
766 |
|
}}); |
767 |
|
|
768 |
|
executor.execute(new CheckedRunnable() { |
769 |
|
public void realRun() throws InterruptedException { |
770 |
|
Thread.sleep(SMALL_DELAY_MS); |
771 |
< |
threadAssertEquals(one, q.take()); |
771 |
> |
assertSame(one, q.take()); |
772 |
|
}}); |
773 |
|
|
774 |
|
joinPool(executor); |
782 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
783 |
|
executor.execute(new CheckedRunnable() { |
784 |
|
public void realRun() throws InterruptedException { |
785 |
< |
threadAssertNull(q.poll()); |
786 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
787 |
< |
threadAssertTrue(q.isEmpty()); |
785 |
> |
assertNull(q.poll()); |
786 |
> |
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
787 |
> |
assertTrue(q.isEmpty()); |
788 |
|
}}); |
789 |
|
|
790 |
|
executor.execute(new CheckedRunnable() { |