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.*; |
38 |
|
* Create a deque of given size containing consecutive |
39 |
|
* Integers 0 ... n. |
40 |
|
*/ |
41 |
< |
private LinkedBlockingDeque populatedDeque(int n) { |
42 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(n); |
41 |
> |
private LinkedBlockingDeque<Integer> populatedDeque(int n) { |
42 |
> |
LinkedBlockingDeque<Integer> q = |
43 |
> |
new LinkedBlockingDeque<Integer>(n); |
44 |
|
assertTrue(q.isEmpty()); |
45 |
|
for (int i = 0; i < n; i++) |
46 |
|
assertTrue(q.offer(new Integer(i))); |
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); |
655 |
|
*/ |
656 |
|
public void testTimedOffer() throws InterruptedException { |
657 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
658 |
< |
Thread t = new Thread(new CheckedRunnable() { |
658 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
659 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
660 |
|
public void realRun() throws InterruptedException { |
661 |
|
q.put(new Object()); |
662 |
|
q.put(new Object()); |
663 |
< |
assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
663 |
> |
long startTime = System.nanoTime(); |
664 |
> |
assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS)); |
665 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
666 |
> |
pleaseInterrupt.countDown(); |
667 |
|
try { |
668 |
< |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
668 |
> |
q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
669 |
|
shouldThrow(); |
670 |
|
} catch (InterruptedException success) {} |
671 |
|
}}); |
672 |
|
|
673 |
< |
t.start(); |
669 |
< |
Thread.sleep(SMALL_DELAY_MS); |
673 |
> |
await(pleaseInterrupt); |
674 |
|
t.interrupt(); |
675 |
< |
t.join(); |
675 |
> |
awaitTermination(t); |
676 |
|
} |
677 |
|
|
678 |
|
/** |
702 |
|
}}); |
703 |
|
|
704 |
|
t.start(); |
705 |
< |
Thread.sleep(SHORT_DELAY_MS); |
705 |
> |
delay(SHORT_DELAY_MS); |
706 |
|
t.interrupt(); |
707 |
|
t.join(); |
708 |
|
} |
746 |
|
* returning timeout status |
747 |
|
*/ |
748 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
749 |
< |
Thread t = new Thread(new CheckedRunnable() { |
749 |
> |
final BlockingQueue<Integer> q = populatedDeque(SIZE); |
750 |
> |
final CountDownLatch aboutToWait = new CountDownLatch(1); |
751 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
752 |
|
public void realRun() throws InterruptedException { |
747 |
– |
LinkedBlockingDeque q = populatedDeque(SIZE); |
753 |
|
for (int i = 0; i < SIZE; ++i) { |
754 |
< |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
754 |
> |
long t0 = System.nanoTime(); |
755 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
756 |
> |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
757 |
|
} |
758 |
+ |
long t0 = System.nanoTime(); |
759 |
+ |
aboutToWait.countDown(); |
760 |
|
try { |
761 |
< |
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
761 |
> |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
762 |
|
shouldThrow(); |
763 |
< |
} catch (InterruptedException success) {} |
763 |
> |
} catch (InterruptedException success) { |
764 |
> |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
765 |
> |
} |
766 |
|
}}); |
767 |
|
|
768 |
< |
t.start(); |
769 |
< |
Thread.sleep(SHORT_DELAY_MS); |
768 |
> |
aboutToWait.await(); |
769 |
> |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
770 |
|
t.interrupt(); |
771 |
< |
t.join(); |
771 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
772 |
> |
checkEmpty(q); |
773 |
|
} |
774 |
|
|
775 |
|
/** |
776 |
|
* putFirst(null) throws NPE |
777 |
|
*/ |
778 |
< |
public void testPutFirstNull() throws InterruptedException { |
778 |
> |
public void testPutFirstNull() throws InterruptedException { |
779 |
|
try { |
780 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
781 |
|
q.putFirst(null); |
782 |
|
shouldThrow(); |
783 |
|
} catch (NullPointerException success) {} |
784 |
< |
} |
784 |
> |
} |
785 |
|
|
786 |
|
/** |
787 |
|
* all elements successfully putFirst are contained |
788 |
|
*/ |
789 |
< |
public void testPutFirst() throws InterruptedException { |
790 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
791 |
< |
for (int i = 0; i < SIZE; ++i) { |
792 |
< |
Integer I = new Integer(i); |
793 |
< |
q.putFirst(I); |
794 |
< |
assertTrue(q.contains(I)); |
795 |
< |
} |
796 |
< |
assertEquals(0, q.remainingCapacity()); |
789 |
> |
public void testPutFirst() throws InterruptedException { |
790 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
791 |
> |
for (int i = 0; i < SIZE; ++i) { |
792 |
> |
Integer I = new Integer(i); |
793 |
> |
q.putFirst(I); |
794 |
> |
assertTrue(q.contains(I)); |
795 |
> |
} |
796 |
> |
assertEquals(0, q.remainingCapacity()); |
797 |
|
} |
798 |
|
|
799 |
|
/** |
814 |
|
}}); |
815 |
|
|
816 |
|
t.start(); |
817 |
< |
Thread.sleep(SHORT_DELAY_MS); |
817 |
> |
delay(SHORT_DELAY_MS); |
818 |
|
t.interrupt(); |
819 |
|
t.join(); |
820 |
|
assertEquals(SIZE, q.size()); |
838 |
|
}}); |
839 |
|
|
840 |
|
t.start(); |
841 |
< |
Thread.sleep(SHORT_DELAY_MS); |
841 |
> |
delay(SHORT_DELAY_MS); |
842 |
|
assertEquals(q.remainingCapacity(), 0); |
843 |
|
assertEquals(capacity - 1, q.take()); |
844 |
< |
Thread.sleep(SHORT_DELAY_MS); |
844 |
> |
delay(SHORT_DELAY_MS); |
845 |
|
t.interrupt(); |
846 |
|
t.join(); |
847 |
|
assertEquals(q.remainingCapacity(), 0); |
852 |
|
*/ |
853 |
|
public void testTimedOfferFirst() throws InterruptedException { |
854 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
855 |
< |
Thread t = new Thread(new CheckedRunnable() { |
855 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
856 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
857 |
|
public void realRun() throws InterruptedException { |
858 |
|
q.putFirst(new Object()); |
859 |
|
q.putFirst(new Object()); |
860 |
< |
assertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
860 |
> |
long startTime = System.nanoTime(); |
861 |
> |
assertFalse(q.offerFirst(new Object(), timeoutMillis(), MILLISECONDS)); |
862 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
863 |
> |
pleaseInterrupt.countDown(); |
864 |
|
try { |
865 |
< |
q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS); |
865 |
> |
q.offerFirst(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
866 |
|
shouldThrow(); |
867 |
|
} catch (InterruptedException success) {} |
868 |
|
}}); |
869 |
|
|
870 |
< |
t.start(); |
855 |
< |
Thread.sleep(SMALL_DELAY_MS); |
870 |
> |
await(pleaseInterrupt); |
871 |
|
t.interrupt(); |
872 |
< |
t.join(); |
872 |
> |
awaitTermination(t); |
873 |
|
} |
874 |
|
|
875 |
|
/** |
893 |
|
}}; |
894 |
|
|
895 |
|
t.start(); |
896 |
< |
Thread.sleep(SHORT_DELAY_MS); |
896 |
> |
delay(SHORT_DELAY_MS); |
897 |
|
t.interrupt(); |
898 |
|
t.join(); |
899 |
|
} |
914 |
|
}}); |
915 |
|
|
916 |
|
t.start(); |
917 |
< |
Thread.sleep(SHORT_DELAY_MS); |
917 |
> |
delay(SHORT_DELAY_MS); |
918 |
|
t.interrupt(); |
919 |
|
t.join(); |
920 |
|
} |
960 |
|
}}); |
961 |
|
|
962 |
|
t.start(); |
963 |
< |
Thread.sleep(SHORT_DELAY_MS); |
963 |
> |
delay(SHORT_DELAY_MS); |
964 |
|
t.interrupt(); |
965 |
|
t.join(); |
966 |
|
} |
982 |
|
}}); |
983 |
|
|
984 |
|
t.start(); |
985 |
< |
Thread.sleep(SMALL_DELAY_MS); |
985 |
> |
delay(SMALL_DELAY_MS); |
986 |
|
assertTrue(q.offerFirst(zero, SHORT_DELAY_MS, MILLISECONDS)); |
987 |
|
t.interrupt(); |
988 |
|
t.join(); |
991 |
|
/** |
992 |
|
* putLast(null) throws NPE |
993 |
|
*/ |
994 |
< |
public void testPutLastNull() throws InterruptedException { |
994 |
> |
public void testPutLastNull() throws InterruptedException { |
995 |
|
try { |
996 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
997 |
|
q.putLast(null); |
998 |
|
shouldThrow(); |
999 |
|
} catch (NullPointerException success) {} |
1000 |
< |
} |
1000 |
> |
} |
1001 |
|
|
1002 |
|
/** |
1003 |
|
* all elements successfully putLast are contained |
1004 |
|
*/ |
1005 |
< |
public void testPutLast() throws InterruptedException { |
1006 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1007 |
< |
for (int i = 0; i < SIZE; ++i) { |
1008 |
< |
Integer I = new Integer(i); |
1009 |
< |
q.putLast(I); |
1010 |
< |
assertTrue(q.contains(I)); |
1011 |
< |
} |
1012 |
< |
assertEquals(0, q.remainingCapacity()); |
1005 |
> |
public void testPutLast() throws InterruptedException { |
1006 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1007 |
> |
for (int i = 0; i < SIZE; ++i) { |
1008 |
> |
Integer I = new Integer(i); |
1009 |
> |
q.putLast(I); |
1010 |
> |
assertTrue(q.contains(I)); |
1011 |
> |
} |
1012 |
> |
assertEquals(0, q.remainingCapacity()); |
1013 |
|
} |
1014 |
|
|
1015 |
|
/** |
1030 |
|
}}); |
1031 |
|
|
1032 |
|
t.start(); |
1033 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1033 |
> |
delay(SHORT_DELAY_MS); |
1034 |
|
t.interrupt(); |
1035 |
|
t.join(); |
1036 |
|
assertEquals(SIZE, q.size()); |
1054 |
|
}}); |
1055 |
|
|
1056 |
|
t.start(); |
1057 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1057 |
> |
delay(SHORT_DELAY_MS); |
1058 |
|
assertEquals(q.remainingCapacity(), 0); |
1059 |
|
assertEquals(0, q.take()); |
1060 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1060 |
> |
delay(SHORT_DELAY_MS); |
1061 |
|
t.interrupt(); |
1062 |
|
t.join(); |
1063 |
|
assertEquals(q.remainingCapacity(), 0); |
1068 |
|
*/ |
1069 |
|
public void testTimedOfferLast() throws InterruptedException { |
1070 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1071 |
< |
Thread t = new Thread(new CheckedRunnable() { |
1071 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
1072 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1073 |
|
public void realRun() throws InterruptedException { |
1074 |
|
q.putLast(new Object()); |
1075 |
|
q.putLast(new Object()); |
1076 |
< |
assertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
1076 |
> |
long startTime = System.nanoTime(); |
1077 |
> |
assertFalse(q.offerLast(new Object(), timeoutMillis(), MILLISECONDS)); |
1078 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
1079 |
> |
pleaseInterrupt.countDown(); |
1080 |
|
try { |
1081 |
< |
q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS); |
1081 |
> |
q.offerLast(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
1082 |
|
shouldThrow(); |
1083 |
|
} catch (InterruptedException success) {} |
1084 |
|
}}); |
1085 |
|
|
1086 |
< |
t.start(); |
1068 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1086 |
> |
await(pleaseInterrupt); |
1087 |
|
t.interrupt(); |
1088 |
< |
t.join(); |
1088 |
> |
awaitTermination(t); |
1089 |
|
} |
1090 |
|
|
1091 |
|
/** |
1109 |
|
}}; |
1110 |
|
|
1111 |
|
t.start(); |
1112 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1112 |
> |
delay(SHORT_DELAY_MS); |
1113 |
|
t.interrupt(); |
1114 |
|
t.join(); |
1115 |
|
} |
1130 |
|
}}); |
1131 |
|
|
1132 |
|
t.start(); |
1133 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1133 |
> |
delay(SHORT_DELAY_MS); |
1134 |
|
t.interrupt(); |
1135 |
|
t.join(); |
1136 |
|
} |
1175 |
|
}}); |
1176 |
|
|
1177 |
|
t.start(); |
1178 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1178 |
> |
delay(SHORT_DELAY_MS); |
1179 |
|
t.interrupt(); |
1180 |
|
t.join(); |
1181 |
|
} |
1197 |
|
}}); |
1198 |
|
|
1199 |
|
t.start(); |
1200 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1200 |
> |
delay(SMALL_DELAY_MS); |
1201 |
|
assertTrue(q.offerLast(zero, SHORT_DELAY_MS, MILLISECONDS)); |
1202 |
|
t.interrupt(); |
1203 |
|
t.join(); |
1225 |
|
public void testRemoveElement() { |
1226 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1227 |
|
for (int i = 1; i < SIZE; i+=2) { |
1228 |
< |
assertTrue(q.remove(new Integer(i))); |
1228 |
> |
assertTrue(q.contains(i)); |
1229 |
> |
assertTrue(q.remove(i)); |
1230 |
> |
assertFalse(q.contains(i)); |
1231 |
> |
assertTrue(q.contains(i-1)); |
1232 |
|
} |
1233 |
|
for (int i = 0; i < SIZE; i+=2) { |
1234 |
< |
assertTrue(q.remove(new Integer(i))); |
1235 |
< |
assertFalse(q.remove(new Integer(i+1))); |
1234 |
> |
assertTrue(q.contains(i)); |
1235 |
> |
assertTrue(q.remove(i)); |
1236 |
> |
assertFalse(q.contains(i)); |
1237 |
> |
assertFalse(q.remove(i+1)); |
1238 |
> |
assertFalse(q.contains(i+1)); |
1239 |
|
} |
1240 |
|
assertTrue(q.isEmpty()); |
1241 |
|
} |
1318 |
|
} |
1319 |
|
|
1320 |
|
/** |
1321 |
< |
* toArray contains all elements |
1321 |
> |
* toArray contains all elements in FIFO order |
1322 |
|
*/ |
1323 |
|
public void testToArray() throws InterruptedException{ |
1324 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1325 |
|
Object[] o = q.toArray(); |
1326 |
|
for (int i = 0; i < o.length; i++) |
1327 |
< |
assertEquals(o[i], q.take()); |
1327 |
> |
assertSame(o[i], q.poll()); |
1328 |
|
} |
1329 |
|
|
1330 |
|
/** |
1331 |
< |
* toArray(a) contains all elements |
1331 |
> |
* toArray(a) contains all elements in FIFO order |
1332 |
|
*/ |
1333 |
< |
public void testToArray2() throws InterruptedException { |
1334 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1333 |
> |
public void testToArray2() { |
1334 |
> |
LinkedBlockingDeque<Integer> q = populatedDeque(SIZE); |
1335 |
|
Integer[] ints = new Integer[SIZE]; |
1336 |
< |
ints = (Integer[])q.toArray(ints); |
1336 |
> |
Integer[] array = q.toArray(ints); |
1337 |
> |
assertSame(ints, array); |
1338 |
|
for (int i = 0; i < ints.length; i++) |
1339 |
< |
assertEquals(ints[i], q.take()); |
1339 |
> |
assertSame(ints[i], q.remove()); |
1340 |
|
} |
1341 |
|
|
1342 |
|
/** |
1517 |
|
|
1518 |
|
executor.execute(new CheckedRunnable() { |
1519 |
|
public void realRun() throws InterruptedException { |
1520 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1520 |
> |
delay(SMALL_DELAY_MS); |
1521 |
|
assertSame(one, q.take()); |
1522 |
|
}}); |
1523 |
|
|
1539 |
|
|
1540 |
|
executor.execute(new CheckedRunnable() { |
1541 |
|
public void realRun() throws InterruptedException { |
1542 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1542 |
> |
delay(SMALL_DELAY_MS); |
1543 |
|
q.put(one); |
1544 |
|
}}); |
1545 |
|
|