14 |
|
|
15 |
|
public class LinkedBlockingQueueTest extends JSR166TestCase { |
16 |
|
|
17 |
+ |
public static class Unbounded extends BlockingQueueTest { |
18 |
+ |
protected BlockingQueue emptyCollection() { |
19 |
+ |
return new LinkedBlockingQueue(); |
20 |
+ |
} |
21 |
+ |
} |
22 |
+ |
|
23 |
+ |
public static class Bounded extends BlockingQueueTest { |
24 |
+ |
protected BlockingQueue emptyCollection() { |
25 |
+ |
return new LinkedBlockingQueue(20); |
26 |
+ |
} |
27 |
+ |
} |
28 |
+ |
|
29 |
|
public static void main(String[] args) { |
30 |
< |
junit.textui.TestRunner.run (suite()); |
30 |
> |
junit.textui.TestRunner.run(suite()); |
31 |
|
} |
32 |
|
|
33 |
|
public static Test suite() { |
34 |
< |
return new TestSuite(LinkedBlockingQueueTest.class); |
34 |
> |
return newTestSuite(LinkedBlockingQueueTest.class, |
35 |
> |
new Unbounded().testSuite(), |
36 |
> |
new Bounded().testSuite()); |
37 |
|
} |
38 |
|
|
39 |
|
|
228 |
|
shouldThrow(); |
229 |
|
} catch (NullPointerException success) {} |
230 |
|
} |
231 |
+ |
|
232 |
|
/** |
233 |
|
* addAll of a collection with any null elements throws NPE after |
234 |
|
* possibly adding some elements |
243 |
|
shouldThrow(); |
244 |
|
} catch (NullPointerException success) {} |
245 |
|
} |
246 |
+ |
|
247 |
|
/** |
248 |
|
* addAll throws ISE if not enough room |
249 |
|
*/ |
257 |
|
shouldThrow(); |
258 |
|
} catch (IllegalStateException success) {} |
259 |
|
} |
260 |
+ |
|
261 |
|
/** |
262 |
|
* Queue contains all elements, in traversal order, of successful addAll |
263 |
|
*/ |
376 |
|
public void testTake() throws InterruptedException { |
377 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
378 |
|
for (int i = 0; i < SIZE; ++i) { |
379 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
379 |
> |
assertEquals(i, q.take()); |
380 |
|
} |
381 |
|
} |
382 |
|
|
404 |
|
Thread t = new Thread(new CheckedRunnable() { |
405 |
|
public void realRun() throws InterruptedException { |
406 |
|
for (int i = 0; i < SIZE; ++i) { |
407 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
407 |
> |
assertEquals(i, q.take()); |
408 |
|
} |
409 |
|
try { |
410 |
|
q.take(); |
424 |
|
public void testPoll() { |
425 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
426 |
|
for (int i = 0; i < SIZE; ++i) { |
427 |
< |
assertEquals(i, ((Integer)q.poll()).intValue()); |
427 |
> |
assertEquals(i, q.poll()); |
428 |
|
} |
429 |
|
assertNull(q.poll()); |
430 |
|
} |
435 |
|
public void testTimedPoll0() throws InterruptedException { |
436 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
437 |
|
for (int i = 0; i < SIZE; ++i) { |
438 |
< |
assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue()); |
438 |
> |
assertEquals(i, q.poll(0, MILLISECONDS)); |
439 |
|
} |
440 |
|
assertNull(q.poll(0, MILLISECONDS)); |
441 |
|
} |
446 |
|
public void testTimedPoll() 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 |
|
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
452 |
|
} |
460 |
|
public void realRun() throws InterruptedException { |
461 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
462 |
|
for (int i = 0; i < SIZE; ++i) { |
463 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
463 |
> |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
464 |
|
} |
465 |
|
try { |
466 |
|
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
475 |
|
} |
476 |
|
|
477 |
|
/** |
461 |
– |
* timed poll before a delayed offer fails; after offer succeeds; |
462 |
– |
* on interruption throws |
463 |
– |
*/ |
464 |
– |
public void testTimedPollWithOffer() throws InterruptedException { |
465 |
– |
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
466 |
– |
Thread t = new Thread(new CheckedRunnable() { |
467 |
– |
public void realRun() throws InterruptedException { |
468 |
– |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
469 |
– |
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
470 |
– |
try { |
471 |
– |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
472 |
– |
shouldThrow(); |
473 |
– |
} catch (InterruptedException success) {} |
474 |
– |
}}); |
475 |
– |
|
476 |
– |
t.start(); |
477 |
– |
Thread.sleep(SMALL_DELAY_MS); |
478 |
– |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
479 |
– |
t.interrupt(); |
480 |
– |
t.join(); |
481 |
– |
} |
482 |
– |
|
483 |
– |
/** |
478 |
|
* peek returns next element, or null if empty |
479 |
|
*/ |
480 |
|
public void testPeek() { |
481 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
482 |
|
for (int i = 0; i < SIZE; ++i) { |
483 |
< |
assertEquals(i, ((Integer)q.peek()).intValue()); |
484 |
< |
q.poll(); |
483 |
> |
assertEquals(i, q.peek()); |
484 |
> |
assertEquals(i, q.poll()); |
485 |
|
assertTrue(q.peek() == null || |
486 |
< |
i != ((Integer)q.peek()).intValue()); |
486 |
> |
!q.peek().equals(i)); |
487 |
|
} |
488 |
|
assertNull(q.peek()); |
489 |
|
} |
494 |
|
public void testElement() { |
495 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
496 |
|
for (int i = 0; i < SIZE; ++i) { |
497 |
< |
assertEquals(i, ((Integer)q.element()).intValue()); |
498 |
< |
q.poll(); |
497 |
> |
assertEquals(i, q.element()); |
498 |
> |
assertEquals(i, q.poll()); |
499 |
|
} |
500 |
|
try { |
501 |
|
q.element(); |
509 |
|
public void testRemove() { |
510 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
511 |
|
for (int i = 0; i < SIZE; ++i) { |
512 |
< |
assertEquals(i, ((Integer)q.remove()).intValue()); |
512 |
> |
assertEquals(i, q.remove()); |
513 |
|
} |
514 |
|
try { |
515 |
|
q.remove(); |
647 |
|
* toArray(null) throws NPE |
648 |
|
*/ |
649 |
|
public void testToArray_BadArg() { |
650 |
+ |
LinkedBlockingQueue q = populatedQueue(SIZE); |
651 |
|
try { |
657 |
– |
LinkedBlockingQueue q = populatedQueue(SIZE); |
652 |
|
Object o[] = q.toArray(null); |
653 |
|
shouldThrow(); |
654 |
|
} catch (NullPointerException success) {} |
658 |
|
* toArray with incompatible array type throws CCE |
659 |
|
*/ |
660 |
|
public void testToArray1_BadArg() { |
661 |
+ |
LinkedBlockingQueue q = populatedQueue(SIZE); |
662 |
|
try { |
663 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
669 |
< |
Object o[] = q.toArray(new String[10] ); |
663 |
> |
Object o[] = q.toArray(new String[10]); |
664 |
|
shouldThrow(); |
665 |
|
} catch (ArrayStoreException success) {} |
666 |
|
} |
680 |
|
/** |
681 |
|
* iterator.remove removes current element |
682 |
|
*/ |
683 |
< |
public void testIteratorRemove () { |
683 |
> |
public void testIteratorRemove() { |
684 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(3); |
685 |
|
q.add(two); |
686 |
|
q.add(one); |
691 |
|
it.remove(); |
692 |
|
|
693 |
|
it = q.iterator(); |
694 |
< |
assertEquals(it.next(), one); |
695 |
< |
assertEquals(it.next(), three); |
694 |
> |
assertSame(it.next(), one); |
695 |
> |
assertSame(it.next(), three); |
696 |
|
assertFalse(it.hasNext()); |
697 |
|
} |
698 |
|
|
708 |
|
assertEquals(0, q.remainingCapacity()); |
709 |
|
int k = 0; |
710 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
711 |
< |
int i = ((Integer)(it.next())).intValue(); |
718 |
< |
assertEquals(++k, i); |
711 |
> |
assertEquals(++k, it.next()); |
712 |
|
} |
713 |
|
assertEquals(3, k); |
714 |
|
} |
716 |
|
/** |
717 |
|
* Modifications do not cause iterators to fail |
718 |
|
*/ |
719 |
< |
public void testWeaklyConsistentIteration () { |
719 |
> |
public void testWeaklyConsistentIteration() { |
720 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(3); |
721 |
|
q.add(one); |
722 |
|
q.add(two); |
751 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
752 |
|
executor.execute(new CheckedRunnable() { |
753 |
|
public void realRun() throws InterruptedException { |
754 |
< |
threadAssertFalse(q.offer(three)); |
755 |
< |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
756 |
< |
threadAssertEquals(0, q.remainingCapacity()); |
754 |
> |
assertFalse(q.offer(three)); |
755 |
> |
assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
756 |
> |
assertEquals(0, q.remainingCapacity()); |
757 |
|
}}); |
758 |
|
|
759 |
|
executor.execute(new CheckedRunnable() { |
760 |
|
public void realRun() throws InterruptedException { |
761 |
|
Thread.sleep(SMALL_DELAY_MS); |
762 |
< |
threadAssertEquals(one, q.take()); |
762 |
> |
assertSame(one, q.take()); |
763 |
|
}}); |
764 |
|
|
765 |
|
joinPool(executor); |
773 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
774 |
|
executor.execute(new CheckedRunnable() { |
775 |
|
public void realRun() throws InterruptedException { |
776 |
< |
threadAssertNull(q.poll()); |
777 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
778 |
< |
threadAssertTrue(q.isEmpty()); |
776 |
> |
assertNull(q.poll()); |
777 |
> |
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
778 |
> |
assertTrue(q.isEmpty()); |
779 |
|
}}); |
780 |
|
|
781 |
|
executor.execute(new CheckedRunnable() { |
895 |
|
} |
896 |
|
|
897 |
|
/** |
898 |
< |
* drainTo(c, n) empties first max {n, size} elements of queue into c |
898 |
> |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
899 |
|
*/ |
900 |
|
public void testDrainToN() { |
901 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(); |
904 |
|
assertTrue(q.offer(new Integer(j))); |
905 |
|
ArrayList l = new ArrayList(); |
906 |
|
q.drainTo(l, i); |
907 |
< |
int k = (i < SIZE)? i : SIZE; |
907 |
> |
int k = (i < SIZE) ? i : SIZE; |
908 |
|
assertEquals(l.size(), k); |
909 |
|
assertEquals(q.size(), SIZE-k); |
910 |
|
for (int j = 0; j < k; ++j) |