1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
|
import junit.framework.*; |
298 |
|
assertSame(four, q.peekLast()); |
299 |
|
} |
300 |
|
|
301 |
– |
|
301 |
|
/** |
302 |
|
* A new deque has the indicated capacity, or Integer.MAX_VALUE if |
303 |
|
* none given |
455 |
|
assertSame(four, q.peekFirst()); |
456 |
|
} |
457 |
|
|
459 |
– |
|
458 |
|
/** |
459 |
|
* pop removes next element, or throws NSEE if empty |
460 |
|
*/ |
469 |
|
} catch (NoSuchElementException success) {} |
470 |
|
} |
471 |
|
|
474 |
– |
|
472 |
|
/** |
473 |
|
* Offer succeeds if not full; fails if full |
474 |
|
*/ |
571 |
|
assertEquals(ints[i], q.poll()); |
572 |
|
} |
573 |
|
|
577 |
– |
|
574 |
|
/** |
575 |
|
* put(null) throws NPE |
576 |
|
*/ |
600 |
|
*/ |
601 |
|
public void testBlockingPut() throws InterruptedException { |
602 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
603 |
< |
Thread t = new Thread(new CheckedRunnable() { |
603 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
604 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
605 |
|
public void realRun() throws InterruptedException { |
606 |
|
for (int i = 0; i < SIZE; ++i) |
607 |
|
q.put(i); |
608 |
|
assertEquals(SIZE, q.size()); |
609 |
|
assertEquals(0, q.remainingCapacity()); |
610 |
+ |
|
611 |
+ |
Thread.currentThread().interrupt(); |
612 |
+ |
try { |
613 |
+ |
q.put(99); |
614 |
+ |
shouldThrow(); |
615 |
+ |
} catch (InterruptedException success) {} |
616 |
+ |
assertFalse(Thread.interrupted()); |
617 |
+ |
|
618 |
+ |
pleaseInterrupt.countDown(); |
619 |
|
try { |
620 |
|
q.put(99); |
621 |
|
shouldThrow(); |
622 |
|
} catch (InterruptedException success) {} |
623 |
+ |
assertFalse(Thread.interrupted()); |
624 |
|
}}); |
625 |
|
|
626 |
< |
t.start(); |
627 |
< |
Thread.sleep(SHORT_DELAY_MS); |
626 |
> |
await(pleaseInterrupt); |
627 |
> |
assertThreadStaysAlive(t); |
628 |
|
t.interrupt(); |
629 |
< |
t.join(); |
629 |
> |
awaitTermination(t); |
630 |
|
assertEquals(SIZE, q.size()); |
631 |
|
assertEquals(0, q.remainingCapacity()); |
632 |
|
} |
633 |
|
|
634 |
|
/** |
635 |
< |
* put blocks waiting for take when full |
635 |
> |
* put blocks interruptibly waiting for take when full |
636 |
|
*/ |
637 |
|
public void testPutWithTake() throws InterruptedException { |
638 |
|
final int capacity = 2; |
639 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity); |
640 |
< |
Thread t = new Thread(new CheckedRunnable() { |
640 |
> |
final CountDownLatch pleaseTake = new CountDownLatch(1); |
641 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
642 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
643 |
|
public void realRun() throws InterruptedException { |
644 |
< |
for (int i = 0; i < capacity + 1; i++) |
644 |
> |
for (int i = 0; i < capacity; i++) |
645 |
|
q.put(i); |
646 |
+ |
pleaseTake.countDown(); |
647 |
+ |
q.put(86); |
648 |
+ |
|
649 |
+ |
pleaseInterrupt.countDown(); |
650 |
|
try { |
651 |
|
q.put(99); |
652 |
|
shouldThrow(); |
653 |
|
} catch (InterruptedException success) {} |
654 |
+ |
assertFalse(Thread.interrupted()); |
655 |
|
}}); |
656 |
|
|
657 |
< |
t.start(); |
644 |
< |
Thread.sleep(SHORT_DELAY_MS); |
657 |
> |
await(pleaseTake); |
658 |
|
assertEquals(q.remainingCapacity(), 0); |
659 |
|
assertEquals(0, q.take()); |
660 |
< |
Thread.sleep(SHORT_DELAY_MS); |
660 |
> |
|
661 |
> |
await(pleaseInterrupt); |
662 |
> |
assertThreadStaysAlive(t); |
663 |
|
t.interrupt(); |
664 |
< |
t.join(); |
664 |
> |
awaitTermination(t); |
665 |
|
assertEquals(q.remainingCapacity(), 0); |
666 |
|
} |
667 |
|
|
670 |
|
*/ |
671 |
|
public void testTimedOffer() throws InterruptedException { |
672 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
673 |
< |
Thread t = new Thread(new CheckedRunnable() { |
673 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
674 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
675 |
|
public void realRun() throws InterruptedException { |
676 |
|
q.put(new Object()); |
677 |
|
q.put(new Object()); |
678 |
< |
assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
678 |
> |
long startTime = System.nanoTime(); |
679 |
> |
assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS)); |
680 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
681 |
> |
pleaseInterrupt.countDown(); |
682 |
|
try { |
683 |
< |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
683 |
> |
q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
684 |
|
shouldThrow(); |
685 |
|
} catch (InterruptedException success) {} |
686 |
|
}}); |
687 |
|
|
688 |
< |
t.start(); |
689 |
< |
Thread.sleep(SMALL_DELAY_MS); |
688 |
> |
await(pleaseInterrupt); |
689 |
> |
assertThreadStaysAlive(t); |
690 |
|
t.interrupt(); |
691 |
< |
t.join(); |
691 |
> |
awaitTermination(t); |
692 |
|
} |
693 |
|
|
694 |
|
/** |
702 |
|
} |
703 |
|
|
704 |
|
/** |
705 |
< |
* Take removes existing elements until empty, then blocks interruptibly |
705 |
> |
* take removes existing elements until empty, then blocks interruptibly |
706 |
|
*/ |
707 |
|
public void testBlockingTake() throws InterruptedException { |
708 |
|
final LinkedBlockingDeque q = populatedDeque(SIZE); |
709 |
< |
Thread t = new Thread(new CheckedRunnable() { |
709 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
710 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
711 |
|
public void realRun() throws InterruptedException { |
712 |
|
for (int i = 0; i < SIZE; ++i) { |
713 |
|
assertEquals(i, q.take()); |
714 |
|
} |
715 |
+ |
|
716 |
+ |
Thread.currentThread().interrupt(); |
717 |
+ |
try { |
718 |
+ |
q.take(); |
719 |
+ |
shouldThrow(); |
720 |
+ |
} catch (InterruptedException success) {} |
721 |
+ |
assertFalse(Thread.interrupted()); |
722 |
+ |
|
723 |
+ |
pleaseInterrupt.countDown(); |
724 |
|
try { |
725 |
|
q.take(); |
726 |
|
shouldThrow(); |
727 |
|
} catch (InterruptedException success) {} |
728 |
+ |
assertFalse(Thread.interrupted()); |
729 |
|
}}); |
730 |
|
|
731 |
< |
t.start(); |
732 |
< |
Thread.sleep(SHORT_DELAY_MS); |
731 |
> |
await(pleaseInterrupt); |
732 |
> |
assertThreadStaysAlive(t); |
733 |
|
t.interrupt(); |
734 |
< |
t.join(); |
734 |
> |
awaitTermination(t); |
735 |
|
} |
736 |
|
|
707 |
– |
|
737 |
|
/** |
738 |
|
* poll succeeds unless empty |
739 |
|
*/ |
762 |
|
public void testTimedPoll() throws InterruptedException { |
763 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
764 |
|
for (int i = 0; i < SIZE; ++i) { |
765 |
< |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
766 |
< |
} |
767 |
< |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
765 |
> |
long startTime = System.nanoTime(); |
766 |
> |
assertEquals(i, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
767 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
768 |
> |
} |
769 |
> |
long startTime = System.nanoTime(); |
770 |
> |
assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
771 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
772 |
> |
checkEmpty(q); |
773 |
|
} |
774 |
|
|
775 |
|
/** |
777 |
|
* returning timeout status |
778 |
|
*/ |
779 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
780 |
< |
Thread t = new Thread(new CheckedRunnable() { |
780 |
> |
final BlockingQueue<Integer> q = populatedDeque(SIZE); |
781 |
> |
final CountDownLatch aboutToWait = new CountDownLatch(1); |
782 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
783 |
|
public void realRun() throws InterruptedException { |
748 |
– |
LinkedBlockingDeque q = populatedDeque(SIZE); |
784 |
|
for (int i = 0; i < SIZE; ++i) { |
785 |
< |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
785 |
> |
long t0 = System.nanoTime(); |
786 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
787 |
> |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
788 |
|
} |
789 |
+ |
long t0 = System.nanoTime(); |
790 |
+ |
aboutToWait.countDown(); |
791 |
|
try { |
792 |
< |
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
792 |
> |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
793 |
|
shouldThrow(); |
794 |
< |
} catch (InterruptedException success) {} |
794 |
> |
} catch (InterruptedException success) { |
795 |
> |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
796 |
> |
} |
797 |
|
}}); |
798 |
|
|
799 |
< |
t.start(); |
800 |
< |
Thread.sleep(SHORT_DELAY_MS); |
799 |
> |
aboutToWait.await(); |
800 |
> |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
801 |
|
t.interrupt(); |
802 |
< |
t.join(); |
802 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
803 |
> |
checkEmpty(q); |
804 |
|
} |
805 |
|
|
806 |
|
/** |
807 |
|
* putFirst(null) throws NPE |
808 |
|
*/ |
809 |
< |
public void testPutFirstNull() throws InterruptedException { |
809 |
> |
public void testPutFirstNull() throws InterruptedException { |
810 |
|
try { |
811 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
812 |
|
q.putFirst(null); |
813 |
|
shouldThrow(); |
814 |
|
} catch (NullPointerException success) {} |
815 |
< |
} |
815 |
> |
} |
816 |
|
|
817 |
|
/** |
818 |
|
* all elements successfully putFirst are contained |
819 |
|
*/ |
820 |
< |
public void testPutFirst() throws InterruptedException { |
821 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
822 |
< |
for (int i = 0; i < SIZE; ++i) { |
823 |
< |
Integer I = new Integer(i); |
824 |
< |
q.putFirst(I); |
825 |
< |
assertTrue(q.contains(I)); |
826 |
< |
} |
827 |
< |
assertEquals(0, q.remainingCapacity()); |
820 |
> |
public void testPutFirst() throws InterruptedException { |
821 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
822 |
> |
for (int i = 0; i < SIZE; ++i) { |
823 |
> |
Integer I = new Integer(i); |
824 |
> |
q.putFirst(I); |
825 |
> |
assertTrue(q.contains(I)); |
826 |
> |
} |
827 |
> |
assertEquals(0, q.remainingCapacity()); |
828 |
|
} |
829 |
|
|
830 |
|
/** |
832 |
|
*/ |
833 |
|
public void testBlockingPutFirst() throws InterruptedException { |
834 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
835 |
< |
Thread t = new Thread(new CheckedRunnable() { |
835 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
836 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
837 |
|
public void realRun() throws InterruptedException { |
838 |
|
for (int i = 0; i < SIZE; ++i) |
839 |
|
q.putFirst(i); |
840 |
|
assertEquals(SIZE, q.size()); |
841 |
|
assertEquals(0, q.remainingCapacity()); |
842 |
+ |
|
843 |
+ |
Thread.currentThread().interrupt(); |
844 |
|
try { |
845 |
|
q.putFirst(99); |
846 |
|
shouldThrow(); |
847 |
|
} catch (InterruptedException success) {} |
848 |
+ |
assertFalse(Thread.interrupted()); |
849 |
+ |
|
850 |
+ |
pleaseInterrupt.countDown(); |
851 |
+ |
try { |
852 |
+ |
q.putFirst(99); |
853 |
+ |
shouldThrow(); |
854 |
+ |
} catch (InterruptedException success) {} |
855 |
+ |
assertFalse(Thread.interrupted()); |
856 |
|
}}); |
857 |
|
|
858 |
< |
t.start(); |
859 |
< |
Thread.sleep(SHORT_DELAY_MS); |
858 |
> |
await(pleaseInterrupt); |
859 |
> |
assertThreadStaysAlive(t); |
860 |
|
t.interrupt(); |
861 |
< |
t.join(); |
861 |
> |
awaitTermination(t); |
862 |
|
assertEquals(SIZE, q.size()); |
863 |
|
assertEquals(0, q.remainingCapacity()); |
864 |
|
} |
865 |
|
|
866 |
|
/** |
867 |
< |
* putFirst blocks waiting for take when full |
867 |
> |
* putFirst blocks interruptibly waiting for take when full |
868 |
|
*/ |
869 |
|
public void testPutFirstWithTake() throws InterruptedException { |
870 |
|
final int capacity = 2; |
871 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity); |
872 |
< |
Thread t = new Thread(new CheckedRunnable() { |
872 |
> |
final CountDownLatch pleaseTake = new CountDownLatch(1); |
873 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
874 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
875 |
|
public void realRun() throws InterruptedException { |
876 |
< |
for (int i = 0; i < capacity + 1; i++) |
876 |
> |
for (int i = 0; i < capacity; i++) |
877 |
|
q.putFirst(i); |
878 |
+ |
pleaseTake.countDown(); |
879 |
+ |
q.putFirst(86); |
880 |
+ |
|
881 |
+ |
pleaseInterrupt.countDown(); |
882 |
|
try { |
883 |
|
q.putFirst(99); |
884 |
|
shouldThrow(); |
885 |
|
} catch (InterruptedException success) {} |
886 |
+ |
assertFalse(Thread.interrupted()); |
887 |
|
}}); |
888 |
|
|
889 |
< |
t.start(); |
830 |
< |
Thread.sleep(SHORT_DELAY_MS); |
889 |
> |
await(pleaseTake); |
890 |
|
assertEquals(q.remainingCapacity(), 0); |
891 |
|
assertEquals(capacity - 1, q.take()); |
892 |
< |
Thread.sleep(SHORT_DELAY_MS); |
892 |
> |
|
893 |
> |
await(pleaseInterrupt); |
894 |
> |
assertThreadStaysAlive(t); |
895 |
|
t.interrupt(); |
896 |
< |
t.join(); |
896 |
> |
awaitTermination(t); |
897 |
|
assertEquals(q.remainingCapacity(), 0); |
898 |
|
} |
899 |
|
|
902 |
|
*/ |
903 |
|
public void testTimedOfferFirst() throws InterruptedException { |
904 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
905 |
< |
Thread t = new Thread(new CheckedRunnable() { |
905 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
906 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
907 |
|
public void realRun() throws InterruptedException { |
908 |
|
q.putFirst(new Object()); |
909 |
|
q.putFirst(new Object()); |
910 |
< |
assertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
910 |
> |
long startTime = System.nanoTime(); |
911 |
> |
assertFalse(q.offerFirst(new Object(), timeoutMillis(), MILLISECONDS)); |
912 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
913 |
> |
pleaseInterrupt.countDown(); |
914 |
|
try { |
915 |
< |
q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS); |
915 |
> |
q.offerFirst(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
916 |
|
shouldThrow(); |
917 |
|
} catch (InterruptedException success) {} |
918 |
|
}}); |
919 |
|
|
920 |
< |
t.start(); |
921 |
< |
Thread.sleep(SMALL_DELAY_MS); |
920 |
> |
await(pleaseInterrupt); |
921 |
> |
assertThreadStaysAlive(t); |
922 |
|
t.interrupt(); |
923 |
< |
t.join(); |
923 |
> |
awaitTermination(t); |
924 |
|
} |
925 |
|
|
926 |
|
/** |
934 |
|
} |
935 |
|
|
936 |
|
/** |
937 |
< |
* takeFirst blocks interruptibly when empty |
937 |
> |
* takeFirst() blocks interruptibly when empty |
938 |
|
*/ |
939 |
< |
public void testTakeFirstFromEmpty() throws InterruptedException { |
940 |
< |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
941 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
942 |
< |
public void realRun() throws InterruptedException { |
943 |
< |
q.takeFirst(); |
944 |
< |
}}; |
939 |
> |
public void testTakeFirstFromEmptyBlocksInterruptibly() { |
940 |
> |
final BlockingDeque q = new LinkedBlockingDeque(); |
941 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
942 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
943 |
> |
public void realRun() { |
944 |
> |
threadStarted.countDown(); |
945 |
> |
try { |
946 |
> |
q.takeFirst(); |
947 |
> |
shouldThrow(); |
948 |
> |
} catch (InterruptedException success) {} |
949 |
> |
assertFalse(Thread.interrupted()); |
950 |
> |
}}); |
951 |
> |
|
952 |
> |
await(threadStarted); |
953 |
> |
assertThreadStaysAlive(t); |
954 |
> |
t.interrupt(); |
955 |
> |
awaitTermination(t); |
956 |
> |
} |
957 |
> |
|
958 |
> |
/** |
959 |
> |
* takeFirst() throws InterruptedException immediately if interrupted |
960 |
> |
* before waiting |
961 |
> |
*/ |
962 |
> |
public void testTakeFirstFromEmptyAfterInterrupt() { |
963 |
> |
final BlockingDeque q = new LinkedBlockingDeque(); |
964 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
965 |
> |
public void realRun() { |
966 |
> |
Thread.currentThread().interrupt(); |
967 |
> |
try { |
968 |
> |
q.takeFirst(); |
969 |
> |
shouldThrow(); |
970 |
> |
} catch (InterruptedException success) {} |
971 |
> |
assertFalse(Thread.interrupted()); |
972 |
> |
}}); |
973 |
> |
|
974 |
> |
awaitTermination(t); |
975 |
> |
} |
976 |
|
|
977 |
< |
t.start(); |
978 |
< |
Thread.sleep(SHORT_DELAY_MS); |
977 |
> |
/** |
978 |
> |
* takeLast() blocks interruptibly when empty |
979 |
> |
*/ |
980 |
> |
public void testTakeLastFromEmptyBlocksInterruptibly() { |
981 |
> |
final BlockingDeque q = new LinkedBlockingDeque(); |
982 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
983 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
984 |
> |
public void realRun() { |
985 |
> |
threadStarted.countDown(); |
986 |
> |
try { |
987 |
> |
q.takeLast(); |
988 |
> |
shouldThrow(); |
989 |
> |
} catch (InterruptedException success) {} |
990 |
> |
assertFalse(Thread.interrupted()); |
991 |
> |
}}); |
992 |
> |
|
993 |
> |
await(threadStarted); |
994 |
> |
assertThreadStaysAlive(t); |
995 |
|
t.interrupt(); |
996 |
< |
t.join(); |
996 |
> |
awaitTermination(t); |
997 |
|
} |
998 |
|
|
999 |
|
/** |
1000 |
< |
* TakeFirst removes existing elements until empty, then blocks interruptibly |
1000 |
> |
* takeLast() throws InterruptedException immediately if interrupted |
1001 |
> |
* before waiting |
1002 |
> |
*/ |
1003 |
> |
public void testTakeLastFromEmptyAfterInterrupt() { |
1004 |
> |
final BlockingDeque q = new LinkedBlockingDeque(); |
1005 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1006 |
> |
public void realRun() { |
1007 |
> |
Thread.currentThread().interrupt(); |
1008 |
> |
try { |
1009 |
> |
q.takeLast(); |
1010 |
> |
shouldThrow(); |
1011 |
> |
} catch (InterruptedException success) {} |
1012 |
> |
assertFalse(Thread.interrupted()); |
1013 |
> |
}}); |
1014 |
> |
|
1015 |
> |
awaitTermination(t); |
1016 |
> |
} |
1017 |
> |
|
1018 |
> |
/** |
1019 |
> |
* takeFirst removes existing elements until empty, then blocks interruptibly |
1020 |
|
*/ |
1021 |
|
public void testBlockingTakeFirst() throws InterruptedException { |
1022 |
|
final LinkedBlockingDeque q = populatedDeque(SIZE); |
1023 |
< |
Thread t = new Thread(new CheckedRunnable() { |
1023 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
1024 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1025 |
|
public void realRun() throws InterruptedException { |
1026 |
< |
for (int i = 0; i < SIZE; ++i) |
1026 |
> |
for (int i = 0; i < SIZE; ++i) { |
1027 |
|
assertEquals(i, q.takeFirst()); |
1028 |
+ |
} |
1029 |
+ |
|
1030 |
+ |
Thread.currentThread().interrupt(); |
1031 |
+ |
try { |
1032 |
+ |
q.takeFirst(); |
1033 |
+ |
shouldThrow(); |
1034 |
+ |
} catch (InterruptedException success) {} |
1035 |
+ |
assertFalse(Thread.interrupted()); |
1036 |
+ |
|
1037 |
+ |
pleaseInterrupt.countDown(); |
1038 |
|
try { |
1039 |
|
q.takeFirst(); |
1040 |
|
shouldThrow(); |
1041 |
|
} catch (InterruptedException success) {} |
1042 |
+ |
assertFalse(Thread.interrupted()); |
1043 |
|
}}); |
1044 |
|
|
1045 |
< |
t.start(); |
1046 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1045 |
> |
await(pleaseInterrupt); |
1046 |
> |
assertThreadStaysAlive(t); |
1047 |
|
t.interrupt(); |
1048 |
< |
t.join(); |
1048 |
> |
awaitTermination(t); |
1049 |
|
} |
1050 |
|
|
908 |
– |
|
1051 |
|
/** |
1052 |
|
* timed pollFirst with zero timeout succeeds when non-empty, else times out |
1053 |
|
*/ |
1065 |
|
public void testTimedPollFirst() throws InterruptedException { |
1066 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1067 |
|
for (int i = 0; i < SIZE; ++i) { |
1068 |
< |
assertEquals(i, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
1069 |
< |
} |
1070 |
< |
assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
1068 |
> |
long startTime = System.nanoTime(); |
1069 |
> |
assertEquals(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS)); |
1070 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1071 |
> |
} |
1072 |
> |
long startTime = System.nanoTime(); |
1073 |
> |
assertNull(q.pollFirst(timeoutMillis(), MILLISECONDS)); |
1074 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
1075 |
> |
checkEmpty(q); |
1076 |
|
} |
1077 |
|
|
1078 |
|
/** |
1080 |
|
* returning timeout status |
1081 |
|
*/ |
1082 |
|
public void testInterruptedTimedPollFirst() throws InterruptedException { |
1083 |
< |
Thread t = new Thread(new CheckedRunnable() { |
1083 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
1084 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1085 |
|
public void realRun() throws InterruptedException { |
1086 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1087 |
|
for (int i = 0; i < SIZE; ++i) { |
1088 |
< |
assertEquals(i, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
1088 |
> |
assertEquals(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS)); |
1089 |
|
} |
1090 |
+ |
|
1091 |
+ |
Thread.currentThread().interrupt(); |
1092 |
+ |
try { |
1093 |
+ |
q.pollFirst(SMALL_DELAY_MS, MILLISECONDS); |
1094 |
+ |
shouldThrow(); |
1095 |
+ |
} catch (InterruptedException success) {} |
1096 |
+ |
assertFalse(Thread.interrupted()); |
1097 |
+ |
|
1098 |
+ |
pleaseInterrupt.countDown(); |
1099 |
|
try { |
1100 |
|
q.pollFirst(SMALL_DELAY_MS, MILLISECONDS); |
1101 |
|
shouldThrow(); |
1102 |
|
} catch (InterruptedException success) {} |
1103 |
+ |
assertFalse(Thread.interrupted()); |
1104 |
|
}}); |
1105 |
|
|
1106 |
< |
t.start(); |
1107 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1106 |
> |
await(pleaseInterrupt); |
1107 |
> |
assertThreadStaysAlive(t); |
1108 |
|
t.interrupt(); |
1109 |
< |
t.join(); |
1109 |
> |
awaitTermination(t); |
1110 |
|
} |
1111 |
|
|
1112 |
|
/** |
1115 |
|
*/ |
1116 |
|
public void testTimedPollFirstWithOfferFirst() throws InterruptedException { |
1117 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1118 |
< |
Thread t = new Thread(new CheckedRunnable() { |
1118 |
> |
final CheckedBarrier barrier = new CheckedBarrier(2); |
1119 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1120 |
|
public void realRun() throws InterruptedException { |
1121 |
< |
assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
1121 |
> |
long startTime = System.nanoTime(); |
1122 |
> |
assertNull(q.pollFirst(timeoutMillis(), MILLISECONDS)); |
1123 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
1124 |
> |
|
1125 |
> |
barrier.await(); |
1126 |
> |
|
1127 |
|
assertSame(zero, q.pollFirst(LONG_DELAY_MS, MILLISECONDS)); |
1128 |
+ |
|
1129 |
+ |
Thread.currentThread().interrupt(); |
1130 |
|
try { |
1131 |
|
q.pollFirst(LONG_DELAY_MS, MILLISECONDS); |
1132 |
|
shouldThrow(); |
1133 |
|
} catch (InterruptedException success) {} |
1134 |
+ |
|
1135 |
+ |
barrier.await(); |
1136 |
+ |
try { |
1137 |
+ |
q.pollFirst(LONG_DELAY_MS, MILLISECONDS); |
1138 |
+ |
shouldThrow(); |
1139 |
+ |
} catch (InterruptedException success) {} |
1140 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1141 |
|
}}); |
1142 |
|
|
1143 |
< |
t.start(); |
1144 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1145 |
< |
assertTrue(q.offerFirst(zero, SHORT_DELAY_MS, MILLISECONDS)); |
1143 |
> |
barrier.await(); |
1144 |
> |
long startTime = System.nanoTime(); |
1145 |
> |
assertTrue(q.offerFirst(zero, LONG_DELAY_MS, MILLISECONDS)); |
1146 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1147 |
> |
barrier.await(); |
1148 |
> |
assertThreadStaysAlive(t); |
1149 |
|
t.interrupt(); |
1150 |
< |
t.join(); |
1150 |
> |
awaitTermination(t); |
1151 |
|
} |
1152 |
|
|
1153 |
|
/** |
1154 |
|
* putLast(null) throws NPE |
1155 |
|
*/ |
1156 |
< |
public void testPutLastNull() throws InterruptedException { |
1156 |
> |
public void testPutLastNull() throws InterruptedException { |
1157 |
|
try { |
1158 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1159 |
|
q.putLast(null); |
1160 |
|
shouldThrow(); |
1161 |
|
} catch (NullPointerException success) {} |
1162 |
< |
} |
1162 |
> |
} |
1163 |
|
|
1164 |
|
/** |
1165 |
|
* all elements successfully putLast are contained |
1166 |
|
*/ |
1167 |
< |
public void testPutLast() throws InterruptedException { |
1168 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1169 |
< |
for (int i = 0; i < SIZE; ++i) { |
1170 |
< |
Integer I = new Integer(i); |
1171 |
< |
q.putLast(I); |
1172 |
< |
assertTrue(q.contains(I)); |
1173 |
< |
} |
1174 |
< |
assertEquals(0, q.remainingCapacity()); |
1167 |
> |
public void testPutLast() throws InterruptedException { |
1168 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1169 |
> |
for (int i = 0; i < SIZE; ++i) { |
1170 |
> |
Integer I = new Integer(i); |
1171 |
> |
q.putLast(I); |
1172 |
> |
assertTrue(q.contains(I)); |
1173 |
> |
} |
1174 |
> |
assertEquals(0, q.remainingCapacity()); |
1175 |
|
} |
1176 |
|
|
1177 |
|
/** |
1179 |
|
*/ |
1180 |
|
public void testBlockingPutLast() throws InterruptedException { |
1181 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1182 |
< |
Thread t = new Thread(new CheckedRunnable() { |
1182 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
1183 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1184 |
|
public void realRun() throws InterruptedException { |
1185 |
|
for (int i = 0; i < SIZE; ++i) |
1186 |
|
q.putLast(i); |
1187 |
|
assertEquals(SIZE, q.size()); |
1188 |
|
assertEquals(0, q.remainingCapacity()); |
1189 |
+ |
|
1190 |
+ |
Thread.currentThread().interrupt(); |
1191 |
|
try { |
1192 |
|
q.putLast(99); |
1193 |
|
shouldThrow(); |
1194 |
|
} catch (InterruptedException success) {} |
1195 |
+ |
assertFalse(Thread.interrupted()); |
1196 |
+ |
|
1197 |
+ |
pleaseInterrupt.countDown(); |
1198 |
+ |
try { |
1199 |
+ |
q.putLast(99); |
1200 |
+ |
shouldThrow(); |
1201 |
+ |
} catch (InterruptedException success) {} |
1202 |
+ |
assertFalse(Thread.interrupted()); |
1203 |
|
}}); |
1204 |
|
|
1205 |
< |
t.start(); |
1206 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1205 |
> |
await(pleaseInterrupt); |
1206 |
> |
assertThreadStaysAlive(t); |
1207 |
|
t.interrupt(); |
1208 |
< |
t.join(); |
1208 |
> |
awaitTermination(t); |
1209 |
|
assertEquals(SIZE, q.size()); |
1210 |
|
assertEquals(0, q.remainingCapacity()); |
1211 |
|
} |
1212 |
|
|
1213 |
|
/** |
1214 |
< |
* putLast blocks waiting for take when full |
1214 |
> |
* putLast blocks interruptibly waiting for take when full |
1215 |
|
*/ |
1216 |
|
public void testPutLastWithTake() throws InterruptedException { |
1217 |
|
final int capacity = 2; |
1218 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity); |
1219 |
< |
Thread t = new Thread(new CheckedRunnable() { |
1219 |
> |
final CountDownLatch pleaseTake = new CountDownLatch(1); |
1220 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
1221 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1222 |
|
public void realRun() throws InterruptedException { |
1223 |
< |
for (int i = 0; i < capacity + 1; i++) |
1223 |
> |
for (int i = 0; i < capacity; i++) |
1224 |
|
q.putLast(i); |
1225 |
+ |
pleaseTake.countDown(); |
1226 |
+ |
q.putLast(86); |
1227 |
+ |
|
1228 |
+ |
pleaseInterrupt.countDown(); |
1229 |
|
try { |
1230 |
|
q.putLast(99); |
1231 |
|
shouldThrow(); |
1232 |
|
} catch (InterruptedException success) {} |
1233 |
+ |
assertFalse(Thread.interrupted()); |
1234 |
|
}}); |
1235 |
|
|
1236 |
< |
t.start(); |
1043 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1236 |
> |
await(pleaseTake); |
1237 |
|
assertEquals(q.remainingCapacity(), 0); |
1238 |
|
assertEquals(0, q.take()); |
1239 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1239 |
> |
|
1240 |
> |
await(pleaseInterrupt); |
1241 |
> |
assertThreadStaysAlive(t); |
1242 |
|
t.interrupt(); |
1243 |
< |
t.join(); |
1243 |
> |
awaitTermination(t); |
1244 |
|
assertEquals(q.remainingCapacity(), 0); |
1245 |
|
} |
1246 |
|
|
1249 |
|
*/ |
1250 |
|
public void testTimedOfferLast() throws InterruptedException { |
1251 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1252 |
< |
Thread t = new Thread(new CheckedRunnable() { |
1252 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
1253 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1254 |
|
public void realRun() throws InterruptedException { |
1255 |
|
q.putLast(new Object()); |
1256 |
|
q.putLast(new Object()); |
1257 |
< |
assertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
1257 |
> |
long startTime = System.nanoTime(); |
1258 |
> |
assertFalse(q.offerLast(new Object(), timeoutMillis(), MILLISECONDS)); |
1259 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
1260 |
> |
pleaseInterrupt.countDown(); |
1261 |
|
try { |
1262 |
< |
q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS); |
1262 |
> |
q.offerLast(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
1263 |
|
shouldThrow(); |
1264 |
|
} catch (InterruptedException success) {} |
1265 |
|
}}); |
1266 |
|
|
1267 |
< |
t.start(); |
1268 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1267 |
> |
await(pleaseInterrupt); |
1268 |
> |
assertThreadStaysAlive(t); |
1269 |
|
t.interrupt(); |
1270 |
< |
t.join(); |
1270 |
> |
awaitTermination(t); |
1271 |
|
} |
1272 |
|
|
1273 |
|
/** |
1281 |
|
} |
1282 |
|
|
1283 |
|
/** |
1284 |
< |
* takeLast blocks interruptibly when empty |
1086 |
< |
*/ |
1087 |
< |
public void testTakeLastFromEmpty() throws InterruptedException { |
1088 |
< |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1089 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
1090 |
< |
public void realRun() throws InterruptedException { |
1091 |
< |
q.takeLast(); |
1092 |
< |
}}; |
1093 |
< |
|
1094 |
< |
t.start(); |
1095 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1096 |
< |
t.interrupt(); |
1097 |
< |
t.join(); |
1098 |
< |
} |
1099 |
< |
|
1100 |
< |
/** |
1101 |
< |
* TakeLast removes existing elements until empty, then blocks interruptibly |
1284 |
> |
* takeLast removes existing elements until empty, then blocks interruptibly |
1285 |
|
*/ |
1286 |
|
public void testBlockingTakeLast() throws InterruptedException { |
1287 |
|
final LinkedBlockingDeque q = populatedDeque(SIZE); |
1288 |
< |
Thread t = new Thread(new CheckedRunnable() { |
1288 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
1289 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1290 |
|
public void realRun() throws InterruptedException { |
1291 |
< |
for (int i = 0; i < SIZE; ++i) |
1292 |
< |
assertEquals(SIZE - 1 - i, q.takeLast()); |
1291 |
> |
for (int i = 0; i < SIZE; ++i) { |
1292 |
> |
assertEquals(SIZE-i-1, q.takeLast()); |
1293 |
> |
} |
1294 |
> |
|
1295 |
> |
Thread.currentThread().interrupt(); |
1296 |
|
try { |
1297 |
|
q.takeLast(); |
1298 |
|
shouldThrow(); |
1299 |
|
} catch (InterruptedException success) {} |
1300 |
+ |
assertFalse(Thread.interrupted()); |
1301 |
+ |
|
1302 |
+ |
pleaseInterrupt.countDown(); |
1303 |
+ |
try { |
1304 |
+ |
q.takeLast(); |
1305 |
+ |
shouldThrow(); |
1306 |
+ |
} catch (InterruptedException success) {} |
1307 |
+ |
assertFalse(Thread.interrupted()); |
1308 |
|
}}); |
1309 |
|
|
1310 |
< |
t.start(); |
1311 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1310 |
> |
await(pleaseInterrupt); |
1311 |
> |
assertThreadStaysAlive(t); |
1312 |
|
t.interrupt(); |
1313 |
< |
t.join(); |
1313 |
> |
awaitTermination(t); |
1314 |
|
} |
1315 |
|
|
1316 |
|
/** |
1330 |
|
public void testTimedPollLast() throws InterruptedException { |
1331 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1332 |
|
for (int i = 0; i < SIZE; ++i) { |
1333 |
< |
assertEquals(SIZE-i-1, q.pollLast(SHORT_DELAY_MS, MILLISECONDS)); |
1334 |
< |
} |
1335 |
< |
assertNull(q.pollLast(SHORT_DELAY_MS, MILLISECONDS)); |
1333 |
> |
long startTime = System.nanoTime(); |
1334 |
> |
assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS)); |
1335 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1336 |
> |
} |
1337 |
> |
long startTime = System.nanoTime(); |
1338 |
> |
assertNull(q.pollLast(timeoutMillis(), MILLISECONDS)); |
1339 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
1340 |
> |
checkEmpty(q); |
1341 |
|
} |
1342 |
|
|
1343 |
|
/** |
1345 |
|
* returning timeout status |
1346 |
|
*/ |
1347 |
|
public void testInterruptedTimedPollLast() throws InterruptedException { |
1348 |
< |
Thread t = new Thread(new CheckedRunnable() { |
1348 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
1349 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1350 |
|
public void realRun() throws InterruptedException { |
1351 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1352 |
|
for (int i = 0; i < SIZE; ++i) { |
1353 |
< |
assertEquals(SIZE-i-1, q.pollLast(SHORT_DELAY_MS, MILLISECONDS)); |
1353 |
> |
assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS)); |
1354 |
|
} |
1355 |
+ |
|
1356 |
+ |
Thread.currentThread().interrupt(); |
1357 |
|
try { |
1358 |
< |
q.pollLast(SMALL_DELAY_MS, MILLISECONDS); |
1358 |
> |
q.pollLast(LONG_DELAY_MS, MILLISECONDS); |
1359 |
|
shouldThrow(); |
1360 |
|
} catch (InterruptedException success) {} |
1361 |
+ |
assertFalse(Thread.interrupted()); |
1362 |
+ |
|
1363 |
+ |
pleaseInterrupt.countDown(); |
1364 |
+ |
try { |
1365 |
+ |
q.pollLast(LONG_DELAY_MS, MILLISECONDS); |
1366 |
+ |
shouldThrow(); |
1367 |
+ |
} catch (InterruptedException success) {} |
1368 |
+ |
assertFalse(Thread.interrupted()); |
1369 |
|
}}); |
1370 |
|
|
1371 |
< |
t.start(); |
1372 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1371 |
> |
await(pleaseInterrupt); |
1372 |
> |
assertThreadStaysAlive(t); |
1373 |
|
t.interrupt(); |
1374 |
< |
t.join(); |
1374 |
> |
awaitTermination(t); |
1375 |
|
} |
1376 |
|
|
1377 |
|
/** |
1380 |
|
*/ |
1381 |
|
public void testTimedPollWithOfferLast() throws InterruptedException { |
1382 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1383 |
< |
Thread t = new Thread(new CheckedRunnable() { |
1383 |
> |
final CheckedBarrier barrier = new CheckedBarrier(2); |
1384 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1385 |
|
public void realRun() throws InterruptedException { |
1386 |
< |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
1386 |
> |
long startTime = System.nanoTime(); |
1387 |
> |
assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
1388 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
1389 |
> |
|
1390 |
> |
barrier.await(); |
1391 |
> |
|
1392 |
|
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
1393 |
+ |
|
1394 |
+ |
Thread.currentThread().interrupt(); |
1395 |
|
try { |
1396 |
|
q.poll(LONG_DELAY_MS, MILLISECONDS); |
1397 |
|
shouldThrow(); |
1398 |
|
} catch (InterruptedException success) {} |
1399 |
+ |
assertFalse(Thread.interrupted()); |
1400 |
+ |
|
1401 |
+ |
barrier.await(); |
1402 |
+ |
try { |
1403 |
+ |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
1404 |
+ |
shouldThrow(); |
1405 |
+ |
} catch (InterruptedException success) {} |
1406 |
+ |
assertFalse(Thread.interrupted()); |
1407 |
|
}}); |
1408 |
|
|
1409 |
< |
t.start(); |
1410 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1411 |
< |
assertTrue(q.offerLast(zero, SHORT_DELAY_MS, MILLISECONDS)); |
1409 |
> |
barrier.await(); |
1410 |
> |
long startTime = System.nanoTime(); |
1411 |
> |
assertTrue(q.offerLast(zero, LONG_DELAY_MS, MILLISECONDS)); |
1412 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1413 |
> |
|
1414 |
> |
barrier.await(); |
1415 |
> |
assertThreadStaysAlive(t); |
1416 |
|
t.interrupt(); |
1417 |
< |
t.join(); |
1417 |
> |
awaitTermination(t); |
1418 |
|
} |
1419 |
|
|
1189 |
– |
|
1420 |
|
/** |
1421 |
|
* element returns next element, or throws NSEE if empty |
1422 |
|
*/ |
1574 |
|
} catch (ArrayStoreException success) {} |
1575 |
|
} |
1576 |
|
|
1347 |
– |
|
1577 |
|
/** |
1578 |
|
* iterator iterates through all elements |
1579 |
|
*/ |
1604 |
|
assertFalse(it.hasNext()); |
1605 |
|
} |
1606 |
|
|
1378 |
– |
|
1607 |
|
/** |
1608 |
|
* iterator ordering is FIFO |
1609 |
|
*/ |
1635 |
|
assertEquals(0, q.size()); |
1636 |
|
} |
1637 |
|
|
1410 |
– |
|
1638 |
|
/** |
1639 |
|
* Descending iterator iterates through all elements |
1640 |
|
*/ |
1697 |
|
} |
1698 |
|
} |
1699 |
|
|
1473 |
– |
|
1700 |
|
/** |
1701 |
|
* toString contains toStrings of elements |
1702 |
|
*/ |
1704 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1705 |
|
String s = q.toString(); |
1706 |
|
for (int i = 0; i < SIZE; ++i) { |
1707 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
1707 |
> |
assertTrue(s.contains(String.valueOf(i))); |
1708 |
|
} |
1709 |
|
} |
1710 |
|
|
1485 |
– |
|
1711 |
|
/** |
1712 |
|
* offer transfers elements across Executor tasks |
1713 |
|
*/ |
1716 |
|
q.add(one); |
1717 |
|
q.add(two); |
1718 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
1719 |
+ |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
1720 |
|
executor.execute(new CheckedRunnable() { |
1721 |
|
public void realRun() throws InterruptedException { |
1722 |
|
assertFalse(q.offer(three)); |
1723 |
< |
assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
1723 |
> |
threadsStarted.await(); |
1724 |
> |
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
1725 |
|
assertEquals(0, q.remainingCapacity()); |
1726 |
|
}}); |
1727 |
|
|
1728 |
|
executor.execute(new CheckedRunnable() { |
1729 |
|
public void realRun() throws InterruptedException { |
1730 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1730 |
> |
threadsStarted.await(); |
1731 |
|
assertSame(one, q.take()); |
1732 |
|
}}); |
1733 |
|
|
1735 |
|
} |
1736 |
|
|
1737 |
|
/** |
1738 |
< |
* poll retrieves elements across Executor threads |
1738 |
> |
* timed poll retrieves elements across Executor threads |
1739 |
|
*/ |
1740 |
|
public void testPollInExecutor() { |
1741 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1742 |
+ |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
1743 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
1744 |
|
executor.execute(new CheckedRunnable() { |
1745 |
|
public void realRun() throws InterruptedException { |
1746 |
|
assertNull(q.poll()); |
1747 |
< |
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
1748 |
< |
assertTrue(q.isEmpty()); |
1747 |
> |
threadsStarted.await(); |
1748 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
1749 |
> |
checkEmpty(q); |
1750 |
|
}}); |
1751 |
|
|
1752 |
|
executor.execute(new CheckedRunnable() { |
1753 |
|
public void realRun() throws InterruptedException { |
1754 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1754 |
> |
threadsStarted.await(); |
1755 |
|
q.put(one); |
1756 |
|
}}); |
1757 |
|
|