7 |
|
import junit.framework.*; |
8 |
|
import java.util.*; |
9 |
|
import java.util.concurrent.*; |
10 |
+ |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
11 |
|
import java.io.*; |
12 |
|
|
13 |
|
public class LinkedBlockingDequeTest extends JSR166TestCase { |
14 |
|
public static void main(String[] args) { |
15 |
< |
junit.textui.TestRunner.run (suite()); |
15 |
> |
junit.textui.TestRunner.run (suite()); |
16 |
|
} |
17 |
|
|
18 |
|
public static Test suite() { |
19 |
< |
return new TestSuite(LinkedBlockingDequeTest.class); |
19 |
> |
return new TestSuite(LinkedBlockingDequeTest.class); |
20 |
|
} |
21 |
|
|
22 |
|
/** |
26 |
|
private LinkedBlockingDeque populatedDeque(int n) { |
27 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(n); |
28 |
|
assertTrue(q.isEmpty()); |
29 |
< |
for (int i = 0; i < n; i++) |
30 |
< |
assertTrue(q.offer(new Integer(i))); |
29 |
> |
for (int i = 0; i < n; i++) |
30 |
> |
assertTrue(q.offer(new Integer(i))); |
31 |
|
assertFalse(q.isEmpty()); |
32 |
|
assertEquals(0, q.remainingCapacity()); |
33 |
< |
assertEquals(n, q.size()); |
33 |
> |
assertEquals(n, q.size()); |
34 |
|
return q; |
35 |
|
} |
36 |
|
|
67 |
|
* offer(null) throws NPE |
68 |
|
*/ |
69 |
|
public void testOfferFirstNull() { |
70 |
< |
try { |
70 |
> |
try { |
71 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(); |
72 |
|
q.offerFirst(null); |
73 |
|
shouldThrow(); |
101 |
|
for (int i = 0; i < SIZE; ++i) { |
102 |
|
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
103 |
|
} |
104 |
< |
assertNull(q.pollFirst()); |
104 |
> |
assertNull(q.pollFirst()); |
105 |
|
} |
106 |
|
|
107 |
|
/** |
112 |
|
for (int i = SIZE-1; i >= 0; --i) { |
113 |
|
assertEquals(i, ((Integer)q.pollLast()).intValue()); |
114 |
|
} |
115 |
< |
assertNull(q.pollLast()); |
115 |
> |
assertNull(q.pollLast()); |
116 |
|
} |
117 |
|
|
118 |
|
/** |
126 |
|
assertTrue(q.peekFirst() == null || |
127 |
|
i != ((Integer)q.peekFirst()).intValue()); |
128 |
|
} |
129 |
< |
assertNull(q.peekFirst()); |
129 |
> |
assertNull(q.peekFirst()); |
130 |
|
} |
131 |
|
|
132 |
|
/** |
140 |
|
assertTrue(q.peek() == null || |
141 |
|
i != ((Integer)q.peek()).intValue()); |
142 |
|
} |
143 |
< |
assertNull(q.peek()); |
143 |
> |
assertNull(q.peek()); |
144 |
|
} |
145 |
|
|
146 |
|
/** |
154 |
|
assertTrue(q.peekLast() == null || |
155 |
|
i != ((Integer)q.peekLast()).intValue()); |
156 |
|
} |
157 |
< |
assertNull(q.peekLast()); |
157 |
> |
assertNull(q.peekLast()); |
158 |
|
} |
159 |
|
|
160 |
|
/** |
187 |
|
shouldThrow(); |
188 |
|
} |
189 |
|
catch (NoSuchElementException success) {} |
190 |
< |
assertNull(q.peekLast()); |
190 |
> |
assertNull(q.peekLast()); |
191 |
|
} |
192 |
|
|
193 |
|
/** |
202 |
|
q.removeFirst(); |
203 |
|
shouldThrow(); |
204 |
|
} catch (NoSuchElementException success) { |
205 |
< |
} |
205 |
> |
} |
206 |
|
} |
207 |
|
|
208 |
|
/** |
217 |
|
q.remove(); |
218 |
|
shouldThrow(); |
219 |
|
} catch (NoSuchElementException success) { |
220 |
< |
} |
220 |
> |
} |
221 |
|
} |
222 |
|
|
223 |
|
/** |
256 |
|
public void testAddFirst() { |
257 |
|
LinkedBlockingDeque q = populatedDeque(3); |
258 |
|
q.pollLast(); |
259 |
< |
q.addFirst(four); |
260 |
< |
assertEquals(four,q.peekFirst()); |
259 |
> |
q.addFirst(four); |
260 |
> |
assertEquals(four,q.peekFirst()); |
261 |
|
} |
262 |
|
|
263 |
|
/** |
266 |
|
public void testAddLast() { |
267 |
|
LinkedBlockingDeque q = populatedDeque(3); |
268 |
|
q.pollLast(); |
269 |
< |
q.addLast(four); |
270 |
< |
assertEquals(four,q.peekLast()); |
269 |
> |
q.addLast(four); |
270 |
> |
assertEquals(four,q.peekLast()); |
271 |
|
} |
272 |
|
|
273 |
|
|
379 |
|
* offer(null) throws NPE |
380 |
|
*/ |
381 |
|
public void testOfferNull() { |
382 |
< |
try { |
382 |
> |
try { |
383 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(1); |
384 |
|
q.offer(null); |
385 |
|
shouldThrow(); |
390 |
|
* add(null) throws NPE |
391 |
|
*/ |
392 |
|
public void testAddNull() { |
393 |
< |
try { |
393 |
> |
try { |
394 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(1); |
395 |
|
q.add(null); |
396 |
|
shouldThrow(); |
401 |
|
* push(null) throws NPE |
402 |
|
*/ |
403 |
|
public void testPushNull() { |
404 |
< |
try { |
404 |
> |
try { |
405 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(1); |
406 |
|
q.push(null); |
407 |
|
shouldThrow(); |
412 |
|
* push succeeds if not full; throws ISE if full |
413 |
|
*/ |
414 |
|
public void testPush() { |
415 |
< |
try { |
415 |
> |
try { |
416 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
417 |
|
for (int i = 0; i < SIZE; ++i) { |
418 |
|
Integer I = new Integer(i); |
422 |
|
assertEquals(0, q.remainingCapacity()); |
423 |
|
q.push(new Integer(SIZE)); |
424 |
|
} catch (IllegalStateException success) { |
425 |
< |
} |
425 |
> |
} |
426 |
|
} |
427 |
|
|
428 |
|
/** |
431 |
|
public void testPushWithPeek() { |
432 |
|
LinkedBlockingDeque q = populatedDeque(3); |
433 |
|
q.pollLast(); |
434 |
< |
q.push(four); |
435 |
< |
assertEquals(four,q.peekFirst()); |
434 |
> |
q.push(four); |
435 |
> |
assertEquals(four,q.peekFirst()); |
436 |
|
} |
437 |
|
|
438 |
|
|
448 |
|
q.pop(); |
449 |
|
shouldThrow(); |
450 |
|
} catch (NoSuchElementException success) { |
451 |
< |
} |
451 |
> |
} |
452 |
|
} |
453 |
|
|
454 |
|
|
465 |
|
* add succeeds if not full; throws ISE if full |
466 |
|
*/ |
467 |
|
public void testAdd() { |
468 |
< |
try { |
468 |
> |
try { |
469 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
470 |
|
for (int i = 0; i < SIZE; ++i) { |
471 |
|
assertTrue(q.add(new Integer(i))); |
473 |
|
assertEquals(0, q.remainingCapacity()); |
474 |
|
q.add(new Integer(SIZE)); |
475 |
|
} catch (IllegalStateException success) { |
476 |
< |
} |
476 |
> |
} |
477 |
|
} |
478 |
|
|
479 |
|
/** |
564 |
|
* put(null) throws NPE |
565 |
|
*/ |
566 |
|
public void testPutNull() { |
567 |
< |
try { |
567 |
> |
try { |
568 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
569 |
|
q.put(null); |
570 |
|
shouldThrow(); |
571 |
|
} |
572 |
|
catch (NullPointerException success) { |
573 |
< |
} |
573 |
> |
} |
574 |
|
catch (InterruptedException ie) { |
575 |
< |
unexpectedException(); |
575 |
> |
unexpectedException(); |
576 |
|
} |
577 |
|
} |
578 |
|
|
590 |
|
assertEquals(0, q.remainingCapacity()); |
591 |
|
} |
592 |
|
catch (InterruptedException ie) { |
593 |
< |
unexpectedException(); |
593 |
> |
unexpectedException(); |
594 |
|
} |
595 |
|
} |
596 |
|
|
620 |
|
t.join(); |
621 |
|
} |
622 |
|
catch (InterruptedException ie) { |
623 |
< |
unexpectedException(); |
623 |
> |
unexpectedException(); |
624 |
|
} |
625 |
|
} |
626 |
|
|
641 |
|
++added; |
642 |
|
q.put(new Object()); |
643 |
|
++added; |
644 |
< |
threadShouldThrow(); |
644 |
> |
threadShouldThrow(); |
645 |
|
} catch (InterruptedException e) { |
646 |
|
threadAssertTrue(added >= 2); |
647 |
|
} |
668 |
|
try { |
669 |
|
q.put(new Object()); |
670 |
|
q.put(new Object()); |
671 |
< |
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
672 |
< |
q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
673 |
< |
threadShouldThrow(); |
671 |
> |
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
672 |
> |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
673 |
> |
threadShouldThrow(); |
674 |
|
} catch (InterruptedException success) {} |
675 |
|
} |
676 |
|
}); |
689 |
|
* take retrieves elements in FIFO order |
690 |
|
*/ |
691 |
|
public void testTake() { |
692 |
< |
try { |
692 |
> |
try { |
693 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
694 |
|
for (int i = 0; i < SIZE; ++i) { |
695 |
|
assertEquals(i, ((Integer)q.take()).intValue()); |
696 |
|
} |
697 |
|
} catch (InterruptedException e) { |
698 |
< |
unexpectedException(); |
699 |
< |
} |
698 |
> |
unexpectedException(); |
699 |
> |
} |
700 |
|
} |
701 |
|
|
702 |
|
/** |
708 |
|
public void run() { |
709 |
|
try { |
710 |
|
q.take(); |
711 |
< |
threadShouldThrow(); |
711 |
> |
threadShouldThrow(); |
712 |
|
} catch (InterruptedException success) { } |
713 |
|
} |
714 |
|
}); |
745 |
|
t.join(); |
746 |
|
} |
747 |
|
catch (InterruptedException ie) { |
748 |
< |
unexpectedException(); |
748 |
> |
unexpectedException(); |
749 |
|
} |
750 |
|
} |
751 |
|
|
758 |
|
for (int i = 0; i < SIZE; ++i) { |
759 |
|
assertEquals(i, ((Integer)q.poll()).intValue()); |
760 |
|
} |
761 |
< |
assertNull(q.poll()); |
761 |
> |
assertNull(q.poll()); |
762 |
|
} |
763 |
|
|
764 |
|
/** |
768 |
|
try { |
769 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
770 |
|
for (int i = 0; i < SIZE; ++i) { |
771 |
< |
assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue()); |
771 |
> |
assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue()); |
772 |
|
} |
773 |
< |
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
773 |
> |
assertNull(q.poll(0, MILLISECONDS)); |
774 |
|
} catch (InterruptedException e) { |
775 |
< |
unexpectedException(); |
776 |
< |
} |
775 |
> |
unexpectedException(); |
776 |
> |
} |
777 |
|
} |
778 |
|
|
779 |
|
/** |
783 |
|
try { |
784 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
785 |
|
for (int i = 0; i < SIZE; ++i) { |
786 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
786 |
> |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
787 |
|
} |
788 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
788 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
789 |
|
} catch (InterruptedException e) { |
790 |
< |
unexpectedException(); |
791 |
< |
} |
790 |
> |
unexpectedException(); |
791 |
> |
} |
792 |
|
} |
793 |
|
|
794 |
|
/** |
801 |
|
try { |
802 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
803 |
|
for (int i = 0; i < SIZE; ++i) { |
804 |
< |
threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
804 |
> |
threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
805 |
|
} |
806 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
806 |
> |
threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
807 |
|
} catch (InterruptedException success) { |
808 |
|
} |
809 |
|
}}); |
814 |
|
t.join(); |
815 |
|
} |
816 |
|
catch (InterruptedException ie) { |
817 |
< |
unexpectedException(); |
817 |
> |
unexpectedException(); |
818 |
|
} |
819 |
|
} |
820 |
|
|
827 |
|
Thread t = new Thread(new Runnable() { |
828 |
|
public void run() { |
829 |
|
try { |
830 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
831 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
832 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
833 |
< |
threadShouldThrow(); |
830 |
> |
threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
831 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
832 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
833 |
> |
threadShouldThrow(); |
834 |
|
} catch (InterruptedException success) { } |
835 |
|
} |
836 |
|
}); |
837 |
|
try { |
838 |
|
t.start(); |
839 |
|
Thread.sleep(SMALL_DELAY_MS); |
840 |
< |
assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
840 |
> |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
841 |
|
t.interrupt(); |
842 |
|
t.join(); |
843 |
|
} catch (Exception e) { |
850 |
|
* putFirst(null) throws NPE |
851 |
|
*/ |
852 |
|
public void testPutFirstNull() { |
853 |
< |
try { |
853 |
> |
try { |
854 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
855 |
|
q.putFirst(null); |
856 |
|
shouldThrow(); |
857 |
|
} |
858 |
|
catch (NullPointerException success) { |
859 |
< |
} |
859 |
> |
} |
860 |
|
catch (InterruptedException ie) { |
861 |
< |
unexpectedException(); |
861 |
> |
unexpectedException(); |
862 |
|
} |
863 |
|
} |
864 |
|
|
876 |
|
assertEquals(0, q.remainingCapacity()); |
877 |
|
} |
878 |
|
catch (InterruptedException ie) { |
879 |
< |
unexpectedException(); |
879 |
> |
unexpectedException(); |
880 |
|
} |
881 |
|
} |
882 |
|
|
906 |
|
t.join(); |
907 |
|
} |
908 |
|
catch (InterruptedException ie) { |
909 |
< |
unexpectedException(); |
909 |
> |
unexpectedException(); |
910 |
|
} |
911 |
|
} |
912 |
|
|
927 |
|
++added; |
928 |
|
q.putFirst(new Object()); |
929 |
|
++added; |
930 |
< |
threadShouldThrow(); |
930 |
> |
threadShouldThrow(); |
931 |
|
} catch (InterruptedException e) { |
932 |
|
threadAssertTrue(added >= 2); |
933 |
|
} |
954 |
|
try { |
955 |
|
q.putFirst(new Object()); |
956 |
|
q.putFirst(new Object()); |
957 |
< |
threadAssertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
958 |
< |
q.offerFirst(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
959 |
< |
threadShouldThrow(); |
957 |
> |
threadAssertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
958 |
> |
q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS); |
959 |
> |
threadShouldThrow(); |
960 |
|
} catch (InterruptedException success) {} |
961 |
|
} |
962 |
|
}); |
975 |
|
* take retrieves elements in FIFO order |
976 |
|
*/ |
977 |
|
public void testTakeFirst() { |
978 |
< |
try { |
978 |
> |
try { |
979 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
980 |
|
for (int i = 0; i < SIZE; ++i) { |
981 |
|
assertEquals(i, ((Integer)q.takeFirst()).intValue()); |
982 |
|
} |
983 |
|
} catch (InterruptedException e) { |
984 |
< |
unexpectedException(); |
985 |
< |
} |
984 |
> |
unexpectedException(); |
985 |
> |
} |
986 |
|
} |
987 |
|
|
988 |
|
/** |
994 |
|
public void run() { |
995 |
|
try { |
996 |
|
q.takeFirst(); |
997 |
< |
threadShouldThrow(); |
997 |
> |
threadShouldThrow(); |
998 |
|
} catch (InterruptedException success) { } |
999 |
|
} |
1000 |
|
}); |
1031 |
|
t.join(); |
1032 |
|
} |
1033 |
|
catch (InterruptedException ie) { |
1034 |
< |
unexpectedException(); |
1034 |
> |
unexpectedException(); |
1035 |
|
} |
1036 |
|
} |
1037 |
|
|
1043 |
|
try { |
1044 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1045 |
|
for (int i = 0; i < SIZE; ++i) { |
1046 |
< |
assertEquals(i, ((Integer)q.pollFirst(0, TimeUnit.MILLISECONDS)).intValue()); |
1046 |
> |
assertEquals(i, ((Integer)q.pollFirst(0, MILLISECONDS)).intValue()); |
1047 |
|
} |
1048 |
< |
assertNull(q.pollFirst(0, TimeUnit.MILLISECONDS)); |
1048 |
> |
assertNull(q.pollFirst(0, MILLISECONDS)); |
1049 |
|
} catch (InterruptedException e) { |
1050 |
< |
unexpectedException(); |
1051 |
< |
} |
1050 |
> |
unexpectedException(); |
1051 |
> |
} |
1052 |
|
} |
1053 |
|
|
1054 |
|
/** |
1058 |
|
try { |
1059 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1060 |
|
for (int i = 0; i < SIZE; ++i) { |
1061 |
< |
assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
1061 |
> |
assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
1062 |
|
} |
1063 |
< |
assertNull(q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1063 |
> |
assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
1064 |
|
} catch (InterruptedException e) { |
1065 |
< |
unexpectedException(); |
1066 |
< |
} |
1065 |
> |
unexpectedException(); |
1066 |
> |
} |
1067 |
|
} |
1068 |
|
|
1069 |
|
/** |
1076 |
|
try { |
1077 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1078 |
|
for (int i = 0; i < SIZE; ++i) { |
1079 |
< |
threadAssertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
1079 |
> |
threadAssertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
1080 |
|
} |
1081 |
< |
threadAssertNull(q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1081 |
> |
threadAssertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
1082 |
|
} catch (InterruptedException success) { |
1083 |
|
} |
1084 |
|
}}); |
1089 |
|
t.join(); |
1090 |
|
} |
1091 |
|
catch (InterruptedException ie) { |
1092 |
< |
unexpectedException(); |
1092 |
> |
unexpectedException(); |
1093 |
|
} |
1094 |
|
} |
1095 |
|
|
1102 |
|
Thread t = new Thread(new Runnable() { |
1103 |
|
public void run() { |
1104 |
|
try { |
1105 |
< |
threadAssertNull(q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1106 |
< |
q.pollFirst(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
1107 |
< |
q.pollFirst(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
1108 |
< |
threadShouldThrow(); |
1105 |
> |
threadAssertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
1106 |
> |
q.pollFirst(LONG_DELAY_MS, MILLISECONDS); |
1107 |
> |
q.pollFirst(LONG_DELAY_MS, MILLISECONDS); |
1108 |
> |
threadShouldThrow(); |
1109 |
|
} catch (InterruptedException success) { } |
1110 |
|
} |
1111 |
|
}); |
1112 |
|
try { |
1113 |
|
t.start(); |
1114 |
|
Thread.sleep(SMALL_DELAY_MS); |
1115 |
< |
assertTrue(q.offerFirst(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1115 |
> |
assertTrue(q.offerFirst(zero, SHORT_DELAY_MS, MILLISECONDS)); |
1116 |
|
t.interrupt(); |
1117 |
|
t.join(); |
1118 |
|
} catch (Exception e) { |
1124 |
|
* putLast(null) throws NPE |
1125 |
|
*/ |
1126 |
|
public void testPutLastNull() { |
1127 |
< |
try { |
1127 |
> |
try { |
1128 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1129 |
|
q.putLast(null); |
1130 |
|
shouldThrow(); |
1131 |
|
} |
1132 |
|
catch (NullPointerException success) { |
1133 |
< |
} |
1133 |
> |
} |
1134 |
|
catch (InterruptedException ie) { |
1135 |
< |
unexpectedException(); |
1135 |
> |
unexpectedException(); |
1136 |
|
} |
1137 |
|
} |
1138 |
|
|
1150 |
|
assertEquals(0, q.remainingCapacity()); |
1151 |
|
} |
1152 |
|
catch (InterruptedException ie) { |
1153 |
< |
unexpectedException(); |
1153 |
> |
unexpectedException(); |
1154 |
|
} |
1155 |
|
} |
1156 |
|
|
1180 |
|
t.join(); |
1181 |
|
} |
1182 |
|
catch (InterruptedException ie) { |
1183 |
< |
unexpectedException(); |
1183 |
> |
unexpectedException(); |
1184 |
|
} |
1185 |
|
} |
1186 |
|
|
1201 |
|
++added; |
1202 |
|
q.putLast(new Object()); |
1203 |
|
++added; |
1204 |
< |
threadShouldThrow(); |
1204 |
> |
threadShouldThrow(); |
1205 |
|
} catch (InterruptedException e) { |
1206 |
|
threadAssertTrue(added >= 2); |
1207 |
|
} |
1228 |
|
try { |
1229 |
|
q.putLast(new Object()); |
1230 |
|
q.putLast(new Object()); |
1231 |
< |
threadAssertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1232 |
< |
q.offerLast(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
1233 |
< |
threadShouldThrow(); |
1231 |
> |
threadAssertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
1232 |
> |
q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS); |
1233 |
> |
threadShouldThrow(); |
1234 |
|
} catch (InterruptedException success) {} |
1235 |
|
} |
1236 |
|
}); |
1249 |
|
* takeLast retrieves elements in FIFO order |
1250 |
|
*/ |
1251 |
|
public void testTakeLast() { |
1252 |
< |
try { |
1252 |
> |
try { |
1253 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1254 |
|
for (int i = 0; i < SIZE; ++i) { |
1255 |
|
assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue()); |
1256 |
|
} |
1257 |
|
} catch (InterruptedException e) { |
1258 |
< |
unexpectedException(); |
1259 |
< |
} |
1258 |
> |
unexpectedException(); |
1259 |
> |
} |
1260 |
|
} |
1261 |
|
|
1262 |
|
/** |
1268 |
|
public void run() { |
1269 |
|
try { |
1270 |
|
q.takeLast(); |
1271 |
< |
threadShouldThrow(); |
1271 |
> |
threadShouldThrow(); |
1272 |
|
} catch (InterruptedException success) { } |
1273 |
|
} |
1274 |
|
}); |
1305 |
|
t.join(); |
1306 |
|
} |
1307 |
|
catch (InterruptedException ie) { |
1308 |
< |
unexpectedException(); |
1308 |
> |
unexpectedException(); |
1309 |
|
} |
1310 |
|
} |
1311 |
|
|
1317 |
|
try { |
1318 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1319 |
|
for (int i = 0; i < SIZE; ++i) { |
1320 |
< |
assertEquals(SIZE-i-1, ((Integer)q.pollLast(0, TimeUnit.MILLISECONDS)).intValue()); |
1320 |
> |
assertEquals(SIZE-i-1, ((Integer)q.pollLast(0, MILLISECONDS)).intValue()); |
1321 |
|
} |
1322 |
< |
assertNull(q.pollLast(0, TimeUnit.MILLISECONDS)); |
1322 |
> |
assertNull(q.pollLast(0, MILLISECONDS)); |
1323 |
|
} catch (InterruptedException e) { |
1324 |
< |
unexpectedException(); |
1325 |
< |
} |
1324 |
> |
unexpectedException(); |
1325 |
> |
} |
1326 |
|
} |
1327 |
|
|
1328 |
|
/** |
1332 |
|
try { |
1333 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1334 |
|
for (int i = 0; i < SIZE; ++i) { |
1335 |
< |
assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
1335 |
> |
assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
1336 |
|
} |
1337 |
< |
assertNull(q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1337 |
> |
assertNull(q.pollLast(SHORT_DELAY_MS, MILLISECONDS)); |
1338 |
|
} catch (InterruptedException e) { |
1339 |
< |
unexpectedException(); |
1340 |
< |
} |
1339 |
> |
unexpectedException(); |
1340 |
> |
} |
1341 |
|
} |
1342 |
|
|
1343 |
|
/** |
1350 |
|
try { |
1351 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1352 |
|
for (int i = 0; i < SIZE; ++i) { |
1353 |
< |
threadAssertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
1353 |
> |
threadAssertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
1354 |
|
} |
1355 |
< |
threadAssertNull(q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1355 |
> |
threadAssertNull(q.pollLast(SHORT_DELAY_MS, MILLISECONDS)); |
1356 |
|
} catch (InterruptedException success) { |
1357 |
|
} |
1358 |
|
}}); |
1363 |
|
t.join(); |
1364 |
|
} |
1365 |
|
catch (InterruptedException ie) { |
1366 |
< |
unexpectedException(); |
1366 |
> |
unexpectedException(); |
1367 |
|
} |
1368 |
|
} |
1369 |
|
|
1376 |
|
Thread t = new Thread(new Runnable() { |
1377 |
|
public void run() { |
1378 |
|
try { |
1379 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1380 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
1381 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
1382 |
< |
threadShouldThrow(); |
1379 |
> |
threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
1380 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
1381 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
1382 |
> |
threadShouldThrow(); |
1383 |
|
} catch (InterruptedException success) { } |
1384 |
|
} |
1385 |
|
}); |
1386 |
|
try { |
1387 |
|
t.start(); |
1388 |
|
Thread.sleep(SMALL_DELAY_MS); |
1389 |
< |
assertTrue(q.offerLast(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1389 |
> |
assertTrue(q.offerLast(zero, SHORT_DELAY_MS, MILLISECONDS)); |
1390 |
|
t.interrupt(); |
1391 |
|
t.join(); |
1392 |
|
} catch (Exception e) { |
1508 |
|
*/ |
1509 |
|
public void testToArray() { |
1510 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1511 |
< |
Object[] o = q.toArray(); |
1512 |
< |
try { |
1513 |
< |
for (int i = 0; i < o.length; i++) |
1514 |
< |
assertEquals(o[i], q.take()); |
1515 |
< |
} catch (InterruptedException e) { |
1516 |
< |
unexpectedException(); |
1517 |
< |
} |
1511 |
> |
Object[] o = q.toArray(); |
1512 |
> |
try { |
1513 |
> |
for (int i = 0; i < o.length; i++) |
1514 |
> |
assertEquals(o[i], q.take()); |
1515 |
> |
} catch (InterruptedException e) { |
1516 |
> |
unexpectedException(); |
1517 |
> |
} |
1518 |
|
} |
1519 |
|
|
1520 |
|
/** |
1522 |
|
*/ |
1523 |
|
public void testToArray2() { |
1524 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1525 |
< |
Integer[] ints = new Integer[SIZE]; |
1526 |
< |
ints = (Integer[])q.toArray(ints); |
1527 |
< |
try { |
1528 |
< |
for (int i = 0; i < ints.length; i++) |
1529 |
< |
assertEquals(ints[i], q.take()); |
1530 |
< |
} catch (InterruptedException e) { |
1531 |
< |
unexpectedException(); |
1532 |
< |
} |
1525 |
> |
Integer[] ints = new Integer[SIZE]; |
1526 |
> |
ints = (Integer[])q.toArray(ints); |
1527 |
> |
try { |
1528 |
> |
for (int i = 0; i < ints.length; i++) |
1529 |
> |
assertEquals(ints[i], q.take()); |
1530 |
> |
} catch (InterruptedException e) { |
1531 |
> |
unexpectedException(); |
1532 |
> |
} |
1533 |
|
} |
1534 |
|
|
1535 |
|
/** |
1536 |
|
* toArray(null) throws NPE |
1537 |
|
*/ |
1538 |
|
public void testToArray_BadArg() { |
1539 |
< |
try { |
1539 |
> |
try { |
1540 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1541 |
< |
Object o[] = q.toArray(null); |
1542 |
< |
shouldThrow(); |
1543 |
< |
} catch (NullPointerException success) {} |
1541 |
> |
Object o[] = q.toArray(null); |
1542 |
> |
shouldThrow(); |
1543 |
> |
} catch (NullPointerException success) {} |
1544 |
|
} |
1545 |
|
|
1546 |
|
/** |
1547 |
|
* toArray with incompatible array type throws CCE |
1548 |
|
*/ |
1549 |
|
public void testToArray1_BadArg() { |
1550 |
< |
try { |
1550 |
> |
try { |
1551 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1552 |
< |
Object o[] = q.toArray(new String[10] ); |
1553 |
< |
shouldThrow(); |
1554 |
< |
} catch (ArrayStoreException success) {} |
1552 |
> |
Object o[] = q.toArray(new String[10] ); |
1553 |
> |
shouldThrow(); |
1554 |
> |
} catch (ArrayStoreException success) {} |
1555 |
|
} |
1556 |
|
|
1557 |
|
|
1560 |
|
*/ |
1561 |
|
public void testIterator() { |
1562 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1563 |
< |
Iterator it = q.iterator(); |
1564 |
< |
try { |
1565 |
< |
while (it.hasNext()) { |
1566 |
< |
assertEquals(it.next(), q.take()); |
1567 |
< |
} |
1568 |
< |
} catch (InterruptedException e) { |
1569 |
< |
unexpectedException(); |
1570 |
< |
} |
1563 |
> |
Iterator it = q.iterator(); |
1564 |
> |
try { |
1565 |
> |
while (it.hasNext()) { |
1566 |
> |
assertEquals(it.next(), q.take()); |
1567 |
> |
} |
1568 |
> |
} catch (InterruptedException e) { |
1569 |
> |
unexpectedException(); |
1570 |
> |
} |
1571 |
|
} |
1572 |
|
|
1573 |
|
/** |
1634 |
|
public void testDescendingIterator() { |
1635 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1636 |
|
int i = 0; |
1637 |
< |
Iterator it = q.descendingIterator(); |
1637 |
> |
Iterator it = q.descendingIterator(); |
1638 |
|
while (it.hasNext()) { |
1639 |
|
assertTrue(q.contains(it.next())); |
1640 |
|
++i; |
1716 |
|
public void run() { |
1717 |
|
threadAssertFalse(q.offer(three)); |
1718 |
|
try { |
1719 |
< |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
1719 |
> |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
1720 |
|
threadAssertEquals(0, q.remainingCapacity()); |
1721 |
|
} |
1722 |
|
catch (InterruptedException e) { |
1750 |
|
public void run() { |
1751 |
|
threadAssertNull(q.poll()); |
1752 |
|
try { |
1753 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
1753 |
> |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
1754 |
|
threadAssertTrue(q.isEmpty()); |
1755 |
|
} |
1756 |
|
catch (InterruptedException e) { |