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.*; |
617 |
|
}}); |
618 |
|
|
619 |
|
t.start(); |
620 |
< |
Thread.sleep(SHORT_DELAY_MS); |
620 |
> |
delay(SHORT_DELAY_MS); |
621 |
|
t.interrupt(); |
622 |
|
t.join(); |
623 |
|
assertEquals(SIZE, q.size()); |
641 |
|
}}); |
642 |
|
|
643 |
|
t.start(); |
644 |
< |
Thread.sleep(SHORT_DELAY_MS); |
644 |
> |
delay(SHORT_DELAY_MS); |
645 |
|
assertEquals(q.remainingCapacity(), 0); |
646 |
|
assertEquals(0, q.take()); |
647 |
< |
Thread.sleep(SHORT_DELAY_MS); |
647 |
> |
delay(SHORT_DELAY_MS); |
648 |
|
t.interrupt(); |
649 |
|
t.join(); |
650 |
|
assertEquals(q.remainingCapacity(), 0); |
667 |
|
}}); |
668 |
|
|
669 |
|
t.start(); |
670 |
< |
Thread.sleep(SMALL_DELAY_MS); |
670 |
> |
delay(SMALL_DELAY_MS); |
671 |
|
t.interrupt(); |
672 |
|
t.join(); |
673 |
|
} |
699 |
|
}}); |
700 |
|
|
701 |
|
t.start(); |
702 |
< |
Thread.sleep(SHORT_DELAY_MS); |
702 |
> |
delay(SHORT_DELAY_MS); |
703 |
|
t.interrupt(); |
704 |
|
t.join(); |
705 |
|
} |
743 |
|
* returning timeout status |
744 |
|
*/ |
745 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
746 |
< |
Thread t = new Thread(new CheckedRunnable() { |
746 |
> |
final BlockingQueue<Integer> q = populatedDeque(SIZE); |
747 |
> |
final CountDownLatch aboutToWait = new CountDownLatch(1); |
748 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
749 |
|
public void realRun() throws InterruptedException { |
748 |
– |
LinkedBlockingDeque q = populatedDeque(SIZE); |
750 |
|
for (int i = 0; i < SIZE; ++i) { |
751 |
< |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
751 |
> |
long t0 = System.nanoTime(); |
752 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
753 |
> |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
754 |
|
} |
755 |
+ |
long t0 = System.nanoTime(); |
756 |
+ |
aboutToWait.countDown(); |
757 |
|
try { |
758 |
< |
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
758 |
> |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
759 |
|
shouldThrow(); |
760 |
< |
} catch (InterruptedException success) {} |
760 |
> |
} catch (InterruptedException success) { |
761 |
> |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
762 |
> |
} |
763 |
|
}}); |
764 |
|
|
765 |
< |
t.start(); |
766 |
< |
Thread.sleep(SHORT_DELAY_MS); |
765 |
> |
aboutToWait.await(); |
766 |
> |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
767 |
|
t.interrupt(); |
768 |
< |
t.join(); |
768 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
769 |
> |
checkEmpty(q); |
770 |
|
} |
771 |
|
|
772 |
|
/** |
773 |
|
* putFirst(null) throws NPE |
774 |
|
*/ |
775 |
< |
public void testPutFirstNull() throws InterruptedException { |
775 |
> |
public void testPutFirstNull() throws InterruptedException { |
776 |
|
try { |
777 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
778 |
|
q.putFirst(null); |
779 |
|
shouldThrow(); |
780 |
|
} catch (NullPointerException success) {} |
781 |
< |
} |
781 |
> |
} |
782 |
|
|
783 |
|
/** |
784 |
|
* all elements successfully putFirst are contained |
785 |
|
*/ |
786 |
< |
public void testPutFirst() throws InterruptedException { |
787 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
788 |
< |
for (int i = 0; i < SIZE; ++i) { |
789 |
< |
Integer I = new Integer(i); |
790 |
< |
q.putFirst(I); |
791 |
< |
assertTrue(q.contains(I)); |
792 |
< |
} |
793 |
< |
assertEquals(0, q.remainingCapacity()); |
786 |
> |
public void testPutFirst() throws InterruptedException { |
787 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
788 |
> |
for (int i = 0; i < SIZE; ++i) { |
789 |
> |
Integer I = new Integer(i); |
790 |
> |
q.putFirst(I); |
791 |
> |
assertTrue(q.contains(I)); |
792 |
> |
} |
793 |
> |
assertEquals(0, q.remainingCapacity()); |
794 |
|
} |
795 |
|
|
796 |
|
/** |
811 |
|
}}); |
812 |
|
|
813 |
|
t.start(); |
814 |
< |
Thread.sleep(SHORT_DELAY_MS); |
814 |
> |
delay(SHORT_DELAY_MS); |
815 |
|
t.interrupt(); |
816 |
|
t.join(); |
817 |
|
assertEquals(SIZE, q.size()); |
835 |
|
}}); |
836 |
|
|
837 |
|
t.start(); |
838 |
< |
Thread.sleep(SHORT_DELAY_MS); |
838 |
> |
delay(SHORT_DELAY_MS); |
839 |
|
assertEquals(q.remainingCapacity(), 0); |
840 |
|
assertEquals(capacity - 1, q.take()); |
841 |
< |
Thread.sleep(SHORT_DELAY_MS); |
841 |
> |
delay(SHORT_DELAY_MS); |
842 |
|
t.interrupt(); |
843 |
|
t.join(); |
844 |
|
assertEquals(q.remainingCapacity(), 0); |
861 |
|
}}); |
862 |
|
|
863 |
|
t.start(); |
864 |
< |
Thread.sleep(SMALL_DELAY_MS); |
864 |
> |
delay(SMALL_DELAY_MS); |
865 |
|
t.interrupt(); |
866 |
|
t.join(); |
867 |
|
} |
887 |
|
}}; |
888 |
|
|
889 |
|
t.start(); |
890 |
< |
Thread.sleep(SHORT_DELAY_MS); |
890 |
> |
delay(SHORT_DELAY_MS); |
891 |
|
t.interrupt(); |
892 |
|
t.join(); |
893 |
|
} |
908 |
|
}}); |
909 |
|
|
910 |
|
t.start(); |
911 |
< |
Thread.sleep(SHORT_DELAY_MS); |
911 |
> |
delay(SHORT_DELAY_MS); |
912 |
|
t.interrupt(); |
913 |
|
t.join(); |
914 |
|
} |
954 |
|
}}); |
955 |
|
|
956 |
|
t.start(); |
957 |
< |
Thread.sleep(SHORT_DELAY_MS); |
957 |
> |
delay(SHORT_DELAY_MS); |
958 |
|
t.interrupt(); |
959 |
|
t.join(); |
960 |
|
} |
976 |
|
}}); |
977 |
|
|
978 |
|
t.start(); |
979 |
< |
Thread.sleep(SMALL_DELAY_MS); |
979 |
> |
delay(SMALL_DELAY_MS); |
980 |
|
assertTrue(q.offerFirst(zero, SHORT_DELAY_MS, MILLISECONDS)); |
981 |
|
t.interrupt(); |
982 |
|
t.join(); |
985 |
|
/** |
986 |
|
* putLast(null) throws NPE |
987 |
|
*/ |
988 |
< |
public void testPutLastNull() throws InterruptedException { |
988 |
> |
public void testPutLastNull() throws InterruptedException { |
989 |
|
try { |
990 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
991 |
|
q.putLast(null); |
992 |
|
shouldThrow(); |
993 |
|
} catch (NullPointerException success) {} |
994 |
< |
} |
994 |
> |
} |
995 |
|
|
996 |
|
/** |
997 |
|
* all elements successfully putLast are contained |
998 |
|
*/ |
999 |
< |
public void testPutLast() throws InterruptedException { |
1000 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1001 |
< |
for (int i = 0; i < SIZE; ++i) { |
1002 |
< |
Integer I = new Integer(i); |
1003 |
< |
q.putLast(I); |
1004 |
< |
assertTrue(q.contains(I)); |
1005 |
< |
} |
1006 |
< |
assertEquals(0, q.remainingCapacity()); |
999 |
> |
public void testPutLast() throws InterruptedException { |
1000 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1001 |
> |
for (int i = 0; i < SIZE; ++i) { |
1002 |
> |
Integer I = new Integer(i); |
1003 |
> |
q.putLast(I); |
1004 |
> |
assertTrue(q.contains(I)); |
1005 |
> |
} |
1006 |
> |
assertEquals(0, q.remainingCapacity()); |
1007 |
|
} |
1008 |
|
|
1009 |
|
/** |
1024 |
|
}}); |
1025 |
|
|
1026 |
|
t.start(); |
1027 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1027 |
> |
delay(SHORT_DELAY_MS); |
1028 |
|
t.interrupt(); |
1029 |
|
t.join(); |
1030 |
|
assertEquals(SIZE, q.size()); |
1048 |
|
}}); |
1049 |
|
|
1050 |
|
t.start(); |
1051 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1051 |
> |
delay(SHORT_DELAY_MS); |
1052 |
|
assertEquals(q.remainingCapacity(), 0); |
1053 |
|
assertEquals(0, q.take()); |
1054 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1054 |
> |
delay(SHORT_DELAY_MS); |
1055 |
|
t.interrupt(); |
1056 |
|
t.join(); |
1057 |
|
assertEquals(q.remainingCapacity(), 0); |
1074 |
|
}}); |
1075 |
|
|
1076 |
|
t.start(); |
1077 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1077 |
> |
delay(SMALL_DELAY_MS); |
1078 |
|
t.interrupt(); |
1079 |
|
t.join(); |
1080 |
|
} |
1100 |
|
}}; |
1101 |
|
|
1102 |
|
t.start(); |
1103 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1103 |
> |
delay(SHORT_DELAY_MS); |
1104 |
|
t.interrupt(); |
1105 |
|
t.join(); |
1106 |
|
} |
1121 |
|
}}); |
1122 |
|
|
1123 |
|
t.start(); |
1124 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1124 |
> |
delay(SHORT_DELAY_MS); |
1125 |
|
t.interrupt(); |
1126 |
|
t.join(); |
1127 |
|
} |
1166 |
|
}}); |
1167 |
|
|
1168 |
|
t.start(); |
1169 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1169 |
> |
delay(SHORT_DELAY_MS); |
1170 |
|
t.interrupt(); |
1171 |
|
t.join(); |
1172 |
|
} |
1188 |
|
}}); |
1189 |
|
|
1190 |
|
t.start(); |
1191 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1191 |
> |
delay(SMALL_DELAY_MS); |
1192 |
|
assertTrue(q.offerLast(zero, SHORT_DELAY_MS, MILLISECONDS)); |
1193 |
|
t.interrupt(); |
1194 |
|
t.join(); |
1508 |
|
|
1509 |
|
executor.execute(new CheckedRunnable() { |
1510 |
|
public void realRun() throws InterruptedException { |
1511 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1511 |
> |
delay(SMALL_DELAY_MS); |
1512 |
|
assertSame(one, q.take()); |
1513 |
|
}}); |
1514 |
|
|
1530 |
|
|
1531 |
|
executor.execute(new CheckedRunnable() { |
1532 |
|
public void realRun() throws InterruptedException { |
1533 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1533 |
> |
delay(SMALL_DELAY_MS); |
1534 |
|
q.put(one); |
1535 |
|
}}); |
1536 |
|
|