43 |
|
} |
44 |
|
|
45 |
|
/** |
46 |
+ |
* add(null) throws NPE |
47 |
+ |
*/ |
48 |
+ |
public void testAddNull() { |
49 |
+ |
try { |
50 |
+ |
SynchronousQueue q = new SynchronousQueue(); |
51 |
+ |
q.add(null); |
52 |
+ |
shouldThrow(); |
53 |
+ |
} catch (NullPointerException success) { } |
54 |
+ |
} |
55 |
+ |
|
56 |
+ |
/** |
57 |
|
* offer fails if no active taker |
58 |
|
*/ |
59 |
|
public void testOffer() { |
85 |
|
} |
86 |
|
catch (NullPointerException success) {} |
87 |
|
} |
88 |
+ |
|
89 |
+ |
/** |
90 |
+ |
* addAll(this) throws IAE |
91 |
+ |
*/ |
92 |
+ |
public void testAddAllSelf() { |
93 |
+ |
try { |
94 |
+ |
SynchronousQueue q = new SynchronousQueue(); |
95 |
+ |
q.addAll(q); |
96 |
+ |
shouldThrow(); |
97 |
+ |
} |
98 |
+ |
catch (IllegalArgumentException success) {} |
99 |
+ |
} |
100 |
+ |
|
101 |
|
/** |
102 |
|
* addAll of a collection with null elements throws NPE |
103 |
|
*/ |
445 |
|
} |
446 |
|
|
447 |
|
/** |
448 |
+ |
* toArray(null) throws NPE |
449 |
+ |
*/ |
450 |
+ |
public void testToArray_BadArg() { |
451 |
+ |
try { |
452 |
+ |
SynchronousQueue q = new SynchronousQueue(); |
453 |
+ |
Object o[] = q.toArray(null); |
454 |
+ |
shouldThrow(); |
455 |
+ |
} catch(NullPointerException success){} |
456 |
+ |
} |
457 |
+ |
|
458 |
+ |
|
459 |
+ |
/** |
460 |
|
* iterator does not traverse any elements |
461 |
|
*/ |
462 |
|
public void testIterator() { |
586 |
|
} |
587 |
|
} |
588 |
|
|
589 |
+ |
/** |
590 |
+ |
* drainTo(null) throws NPE |
591 |
+ |
*/ |
592 |
+ |
public void testDrainToNull() { |
593 |
+ |
SynchronousQueue q = new SynchronousQueue(); |
594 |
+ |
try { |
595 |
+ |
q.drainTo(null); |
596 |
+ |
shouldThrow(); |
597 |
+ |
} catch(NullPointerException success) { |
598 |
+ |
} |
599 |
+ |
} |
600 |
+ |
|
601 |
+ |
/** |
602 |
+ |
* drainTo(this) throws IAE |
603 |
+ |
*/ |
604 |
+ |
public void testDrainToSelf() { |
605 |
+ |
SynchronousQueue q = new SynchronousQueue(); |
606 |
+ |
try { |
607 |
+ |
q.drainTo(q); |
608 |
+ |
shouldThrow(); |
609 |
+ |
} catch(IllegalArgumentException success) { |
610 |
+ |
} |
611 |
+ |
} |
612 |
+ |
|
613 |
+ |
/** |
614 |
+ |
* drainTo(c) of empty queue doesn't transfer elements |
615 |
+ |
*/ |
616 |
+ |
public void testDrainTo() { |
617 |
+ |
SynchronousQueue q = new SynchronousQueue(); |
618 |
+ |
ArrayList l = new ArrayList(); |
619 |
+ |
q.drainTo(l); |
620 |
+ |
assertEquals(q.size(), 0); |
621 |
+ |
assertEquals(l.size(), 0); |
622 |
+ |
} |
623 |
+ |
|
624 |
+ |
/** |
625 |
+ |
* drainTo empties queue, unblocking a waiting put. |
626 |
+ |
*/ |
627 |
+ |
public void testDrainToWithActivePut() { |
628 |
+ |
final SynchronousQueue q = new SynchronousQueue(); |
629 |
+ |
Thread t = new Thread(new Runnable() { |
630 |
+ |
public void run() { |
631 |
+ |
try { |
632 |
+ |
q.put(new Integer(1)); |
633 |
+ |
} catch (InterruptedException ie){ |
634 |
+ |
threadUnexpectedException(); |
635 |
+ |
} |
636 |
+ |
} |
637 |
+ |
}); |
638 |
+ |
try { |
639 |
+ |
t.start(); |
640 |
+ |
ArrayList l = new ArrayList(); |
641 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
642 |
+ |
q.drainTo(l); |
643 |
+ |
assertTrue(l.size() <= 1); |
644 |
+ |
if (l.size() > 0) |
645 |
+ |
assertEquals(l.get(0), new Integer(1)); |
646 |
+ |
t.join(); |
647 |
+ |
assertTrue(l.size() <= 1); |
648 |
+ |
} catch(Exception e){ |
649 |
+ |
unexpectedException(); |
650 |
+ |
} |
651 |
+ |
} |
652 |
+ |
|
653 |
+ |
/** |
654 |
+ |
* drainTo(null, n) throws NPE |
655 |
+ |
*/ |
656 |
+ |
public void testDrainToNullN() { |
657 |
+ |
SynchronousQueue q = new SynchronousQueue(); |
658 |
+ |
try { |
659 |
+ |
q.drainTo(null, 0); |
660 |
+ |
shouldThrow(); |
661 |
+ |
} catch(NullPointerException success) { |
662 |
+ |
} |
663 |
+ |
} |
664 |
+ |
|
665 |
+ |
/** |
666 |
+ |
* drainTo(this, n) throws IAE |
667 |
+ |
*/ |
668 |
+ |
public void testDrainToSelfN() { |
669 |
+ |
SynchronousQueue q = new SynchronousQueue(); |
670 |
+ |
try { |
671 |
+ |
q.drainTo(q, 0); |
672 |
+ |
shouldThrow(); |
673 |
+ |
} catch(IllegalArgumentException success) { |
674 |
+ |
} |
675 |
+ |
} |
676 |
+ |
|
677 |
+ |
/** |
678 |
+ |
* drainTo(c, n) empties up to n elements of queue into c |
679 |
+ |
*/ |
680 |
+ |
public void testDrainToN() { |
681 |
+ |
final SynchronousQueue q = new SynchronousQueue(); |
682 |
+ |
Thread t1 = new Thread(new Runnable() { |
683 |
+ |
public void run() { |
684 |
+ |
try { |
685 |
+ |
q.put(one); |
686 |
+ |
} catch (InterruptedException ie){ |
687 |
+ |
threadUnexpectedException(); |
688 |
+ |
} |
689 |
+ |
} |
690 |
+ |
}); |
691 |
+ |
Thread t2 = new Thread(new Runnable() { |
692 |
+ |
public void run() { |
693 |
+ |
try { |
694 |
+ |
q.put(two); |
695 |
+ |
} catch (InterruptedException ie){ |
696 |
+ |
threadUnexpectedException(); |
697 |
+ |
} |
698 |
+ |
} |
699 |
+ |
}); |
700 |
+ |
|
701 |
+ |
try { |
702 |
+ |
t1.start(); |
703 |
+ |
t2.start(); |
704 |
+ |
ArrayList l = new ArrayList(); |
705 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
706 |
+ |
q.drainTo(l, 1); |
707 |
+ |
assertTrue(l.size() == 1); |
708 |
+ |
q.drainTo(l, 1); |
709 |
+ |
assertTrue(l.size() == 2); |
710 |
+ |
assertTrue(l.contains(one)); |
711 |
+ |
assertTrue(l.contains(two)); |
712 |
+ |
t1.join(); |
713 |
+ |
t2.join(); |
714 |
+ |
} catch(Exception e){ |
715 |
+ |
unexpectedException(); |
716 |
+ |
} |
717 |
+ |
} |
718 |
+ |
|
719 |
+ |
|
720 |
|
} |