442 |
|
} |
443 |
|
|
444 |
|
/** |
445 |
< |
* push succeeds if not full; throws ISE if full |
445 |
> |
* push succeeds if not full; throws IllegalStateException if full |
446 |
|
*/ |
447 |
|
public void testPush() { |
448 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
492 |
|
} |
493 |
|
|
494 |
|
/** |
495 |
< |
* add succeeds if not full; throws ISE if full |
495 |
> |
* add succeeds if not full; throws IllegalStateException if full |
496 |
|
*/ |
497 |
|
public void testAdd() { |
498 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
506 |
|
} |
507 |
|
|
508 |
|
/** |
509 |
< |
* addAll(this) throws IAE |
509 |
> |
* addAll(this) throws IllegalArgumentException |
510 |
|
*/ |
511 |
|
public void testAddAllSelf() { |
512 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
604 |
|
}}); |
605 |
|
|
606 |
|
await(pleaseInterrupt); |
607 |
< |
assertThreadBlocks(t, Thread.State.WAITING); |
607 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); |
608 |
|
t.interrupt(); |
609 |
|
awaitTermination(t); |
610 |
|
assertEquals(SIZE, q.size()); |
626 |
|
pleaseTake.countDown(); |
627 |
|
q.put(86); |
628 |
|
|
629 |
+ |
Thread.currentThread().interrupt(); |
630 |
+ |
try { |
631 |
+ |
q.put(99); |
632 |
+ |
shouldThrow(); |
633 |
+ |
} catch (InterruptedException success) {} |
634 |
+ |
assertFalse(Thread.interrupted()); |
635 |
+ |
|
636 |
|
pleaseInterrupt.countDown(); |
637 |
|
try { |
638 |
|
q.put(99); |
646 |
|
assertEquals(0, q.take()); |
647 |
|
|
648 |
|
await(pleaseInterrupt); |
649 |
< |
assertThreadBlocks(t, Thread.State.WAITING); |
649 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); |
650 |
|
t.interrupt(); |
651 |
|
awaitTermination(t); |
652 |
|
assertEquals(0, q.remainingCapacity()); |
655 |
|
/** |
656 |
|
* timed offer times out if full and elements not taken |
657 |
|
*/ |
658 |
< |
public void testTimedOffer() throws InterruptedException { |
658 |
> |
public void testTimedOffer() { |
659 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
660 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
661 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
663 |
|
q.put(new Object()); |
664 |
|
q.put(new Object()); |
665 |
|
long startTime = System.nanoTime(); |
666 |
+ |
|
667 |
|
assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS)); |
668 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
669 |
+ |
|
670 |
+ |
Thread.currentThread().interrupt(); |
671 |
+ |
try { |
672 |
+ |
q.offer(new Object(), randomTimeout(), randomTimeUnit()); |
673 |
+ |
shouldThrow(); |
674 |
+ |
} catch (InterruptedException success) {} |
675 |
+ |
assertFalse(Thread.interrupted()); |
676 |
+ |
|
677 |
|
pleaseInterrupt.countDown(); |
678 |
|
try { |
679 |
< |
q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
679 |
> |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
680 |
|
shouldThrow(); |
681 |
|
} catch (InterruptedException success) {} |
682 |
|
assertFalse(Thread.interrupted()); |
683 |
+ |
|
684 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
685 |
|
}}); |
686 |
|
|
687 |
|
await(pleaseInterrupt); |
688 |
< |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
688 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
689 |
|
t.interrupt(); |
690 |
|
awaitTermination(t); |
691 |
|
} |
726 |
|
}}); |
727 |
|
|
728 |
|
await(pleaseInterrupt); |
729 |
< |
assertThreadBlocks(t, Thread.State.WAITING); |
729 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); |
730 |
|
t.interrupt(); |
731 |
|
awaitTermination(t); |
732 |
|
} |
779 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
780 |
|
public void realRun() throws InterruptedException { |
781 |
|
long startTime = System.nanoTime(); |
782 |
< |
for (int i = 0; i < SIZE; ++i) { |
782 |
> |
for (int i = 0; i < SIZE; i++) |
783 |
|
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
784 |
< |
} |
784 |
> |
|
785 |
> |
Thread.currentThread().interrupt(); |
786 |
> |
try { |
787 |
> |
q.poll(randomTimeout(), randomTimeUnit()); |
788 |
> |
shouldThrow(); |
789 |
> |
} catch (InterruptedException success) {} |
790 |
> |
assertFalse(Thread.interrupted()); |
791 |
|
|
792 |
|
pleaseInterrupt.countDown(); |
793 |
|
try { |
800 |
|
}}); |
801 |
|
|
802 |
|
await(pleaseInterrupt); |
803 |
< |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
803 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
804 |
|
t.interrupt(); |
805 |
|
awaitTermination(t); |
806 |
|
checkEmpty(q); |
859 |
|
}}); |
860 |
|
|
861 |
|
await(pleaseInterrupt); |
862 |
< |
assertThreadBlocks(t, Thread.State.WAITING); |
862 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); |
863 |
|
t.interrupt(); |
864 |
|
awaitTermination(t); |
865 |
|
assertEquals(SIZE, q.size()); |
894 |
|
assertEquals(capacity - 1, q.take()); |
895 |
|
|
896 |
|
await(pleaseInterrupt); |
897 |
< |
assertThreadBlocks(t, Thread.State.WAITING); |
897 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); |
898 |
|
t.interrupt(); |
899 |
|
awaitTermination(t); |
900 |
|
assertEquals(0, q.remainingCapacity()); |
903 |
|
/** |
904 |
|
* timed offerFirst times out if full and elements not taken |
905 |
|
*/ |
906 |
< |
public void testTimedOfferFirst() throws InterruptedException { |
906 |
> |
public void testTimedOfferFirst() { |
907 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
908 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
909 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
911 |
|
q.putFirst(new Object()); |
912 |
|
q.putFirst(new Object()); |
913 |
|
long startTime = System.nanoTime(); |
914 |
+ |
|
915 |
|
assertFalse(q.offerFirst(new Object(), timeoutMillis(), MILLISECONDS)); |
916 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
917 |
+ |
|
918 |
+ |
Thread.currentThread().interrupt(); |
919 |
+ |
try { |
920 |
+ |
q.offerFirst(new Object(), randomTimeout(), randomTimeUnit()); |
921 |
+ |
shouldThrow(); |
922 |
+ |
} catch (InterruptedException success) {} |
923 |
+ |
assertFalse(Thread.interrupted()); |
924 |
+ |
|
925 |
|
pleaseInterrupt.countDown(); |
926 |
|
try { |
927 |
< |
q.offerFirst(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
927 |
> |
q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS); |
928 |
|
shouldThrow(); |
929 |
|
} catch (InterruptedException success) {} |
930 |
|
assertFalse(Thread.interrupted()); |
931 |
+ |
|
932 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
933 |
|
}}); |
934 |
|
|
935 |
|
await(pleaseInterrupt); |
936 |
< |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
936 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
937 |
|
t.interrupt(); |
938 |
|
awaitTermination(t); |
939 |
|
} |
965 |
|
}}); |
966 |
|
|
967 |
|
await(threadStarted); |
968 |
< |
assertThreadBlocks(t, Thread.State.WAITING); |
968 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); |
969 |
|
t.interrupt(); |
970 |
|
awaitTermination(t); |
971 |
|
} |
1006 |
|
}}); |
1007 |
|
|
1008 |
|
await(threadStarted); |
1009 |
< |
assertThreadBlocks(t, Thread.State.WAITING); |
1009 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); |
1010 |
|
t.interrupt(); |
1011 |
|
awaitTermination(t); |
1012 |
|
} |
1056 |
|
}}); |
1057 |
|
|
1058 |
|
await(pleaseInterrupt); |
1059 |
< |
assertThreadBlocks(t, Thread.State.WAITING); |
1059 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); |
1060 |
|
t.interrupt(); |
1061 |
|
awaitTermination(t); |
1062 |
|
} |
1098 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1099 |
|
public void realRun() throws InterruptedException { |
1100 |
|
long startTime = System.nanoTime(); |
1101 |
< |
for (int i = 0; i < SIZE; ++i) { |
1101 |
> |
for (int i = 0; i < SIZE; i++) |
1102 |
|
assertEquals(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS)); |
1068 |
– |
} |
1103 |
|
|
1104 |
|
Thread.currentThread().interrupt(); |
1105 |
|
try { |
1106 |
< |
q.pollFirst(LONG_DELAY_MS, MILLISECONDS); |
1106 |
> |
q.pollFirst(randomTimeout(), randomTimeUnit()); |
1107 |
|
shouldThrow(); |
1108 |
|
} catch (InterruptedException success) {} |
1109 |
|
assertFalse(Thread.interrupted()); |
1119 |
|
}}); |
1120 |
|
|
1121 |
|
await(pleaseInterrupt); |
1122 |
< |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
1122 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
1123 |
|
t.interrupt(); |
1124 |
|
awaitTermination(t); |
1125 |
|
} |
1143 |
|
|
1144 |
|
Thread.currentThread().interrupt(); |
1145 |
|
try { |
1146 |
< |
q.pollFirst(LONG_DELAY_MS, MILLISECONDS); |
1146 |
> |
q.pollFirst(randomTimeout(), randomTimeUnit()); |
1147 |
|
shouldThrow(); |
1148 |
|
} catch (InterruptedException success) {} |
1149 |
|
|
1153 |
|
shouldThrow(); |
1154 |
|
} catch (InterruptedException success) {} |
1155 |
|
assertFalse(Thread.interrupted()); |
1156 |
+ |
|
1157 |
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1158 |
|
}}); |
1159 |
|
|
1162 |
|
assertTrue(q.offerFirst(zero, LONG_DELAY_MS, MILLISECONDS)); |
1163 |
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1164 |
|
barrier.await(); |
1165 |
< |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
1165 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
1166 |
|
t.interrupt(); |
1167 |
|
awaitTermination(t); |
1168 |
|
} |
1220 |
|
}}); |
1221 |
|
|
1222 |
|
await(pleaseInterrupt); |
1223 |
< |
assertThreadBlocks(t, Thread.State.WAITING); |
1223 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); |
1224 |
|
t.interrupt(); |
1225 |
|
awaitTermination(t); |
1226 |
|
assertEquals(SIZE, q.size()); |
1242 |
|
pleaseTake.countDown(); |
1243 |
|
q.putLast(86); |
1244 |
|
|
1245 |
+ |
Thread.currentThread().interrupt(); |
1246 |
+ |
try { |
1247 |
+ |
q.putLast(99); |
1248 |
+ |
shouldThrow(); |
1249 |
+ |
} catch (InterruptedException success) {} |
1250 |
+ |
assertFalse(Thread.interrupted()); |
1251 |
+ |
|
1252 |
|
pleaseInterrupt.countDown(); |
1253 |
|
try { |
1254 |
|
q.putLast(99); |
1262 |
|
assertEquals(0, q.take()); |
1263 |
|
|
1264 |
|
await(pleaseInterrupt); |
1265 |
< |
assertThreadBlocks(t, Thread.State.WAITING); |
1265 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); |
1266 |
|
t.interrupt(); |
1267 |
|
awaitTermination(t); |
1268 |
|
assertEquals(0, q.remainingCapacity()); |
1271 |
|
/** |
1272 |
|
* timed offerLast times out if full and elements not taken |
1273 |
|
*/ |
1274 |
< |
public void testTimedOfferLast() throws InterruptedException { |
1274 |
> |
public void testTimedOfferLast() { |
1275 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1276 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
1277 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1279 |
|
q.putLast(new Object()); |
1280 |
|
q.putLast(new Object()); |
1281 |
|
long startTime = System.nanoTime(); |
1282 |
+ |
|
1283 |
|
assertFalse(q.offerLast(new Object(), timeoutMillis(), MILLISECONDS)); |
1284 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
1285 |
+ |
|
1286 |
+ |
Thread.currentThread().interrupt(); |
1287 |
+ |
try { |
1288 |
+ |
q.offerLast(new Object(), randomTimeout(), randomTimeUnit()); |
1289 |
+ |
shouldThrow(); |
1290 |
+ |
} catch (InterruptedException success) {} |
1291 |
+ |
|
1292 |
|
pleaseInterrupt.countDown(); |
1293 |
|
try { |
1294 |
< |
q.offerLast(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
1294 |
> |
q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS); |
1295 |
|
shouldThrow(); |
1296 |
|
} catch (InterruptedException success) {} |
1297 |
+ |
|
1298 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1299 |
|
}}); |
1300 |
|
|
1301 |
|
await(pleaseInterrupt); |
1302 |
< |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
1302 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
1303 |
|
t.interrupt(); |
1304 |
|
awaitTermination(t); |
1305 |
|
} |
1341 |
|
}}); |
1342 |
|
|
1343 |
|
await(pleaseInterrupt); |
1344 |
< |
assertThreadBlocks(t, Thread.State.WAITING); |
1344 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); |
1345 |
|
t.interrupt(); |
1346 |
|
awaitTermination(t); |
1347 |
|
} |
1383 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1384 |
|
public void realRun() throws InterruptedException { |
1385 |
|
long startTime = System.nanoTime(); |
1386 |
< |
for (int i = 0; i < SIZE; ++i) { |
1386 |
> |
for (int i = 0; i < SIZE; i++) |
1387 |
|
assertEquals(SIZE - i - 1, |
1388 |
|
q.pollLast(LONG_DELAY_MS, MILLISECONDS)); |
1337 |
– |
} |
1389 |
|
|
1390 |
|
Thread.currentThread().interrupt(); |
1391 |
|
try { |
1392 |
< |
q.pollLast(LONG_DELAY_MS, MILLISECONDS); |
1392 |
> |
q.pollLast(randomTimeout(), randomTimeUnit()); |
1393 |
|
shouldThrow(); |
1394 |
|
} catch (InterruptedException success) {} |
1395 |
|
assertFalse(Thread.interrupted()); |
1405 |
|
}}); |
1406 |
|
|
1407 |
|
await(pleaseInterrupt); |
1408 |
< |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
1408 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
1409 |
|
t.interrupt(); |
1410 |
|
awaitTermination(t); |
1411 |
|
checkEmpty(q); |
1430 |
|
|
1431 |
|
Thread.currentThread().interrupt(); |
1432 |
|
try { |
1433 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
1433 |
> |
q.poll(randomTimeout(), randomTimeUnit()); |
1434 |
|
shouldThrow(); |
1435 |
|
} catch (InterruptedException success) {} |
1436 |
|
assertFalse(Thread.interrupted()); |
1451 |
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1452 |
|
|
1453 |
|
barrier.await(); |
1454 |
< |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
1454 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
1455 |
|
t.interrupt(); |
1456 |
|
awaitTermination(t); |
1457 |
|
} |
1553 |
|
*/ |
1554 |
|
public void testToArray() throws InterruptedException { |
1555 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1556 |
< |
Object[] o = q.toArray(); |
1557 |
< |
for (int i = 0; i < o.length; i++) |
1558 |
< |
assertSame(o[i], q.poll()); |
1556 |
> |
Object[] a = q.toArray(); |
1557 |
> |
assertSame(Object[].class, a.getClass()); |
1558 |
> |
for (Object o : a) |
1559 |
> |
assertSame(o, q.poll()); |
1560 |
> |
assertTrue(q.isEmpty()); |
1561 |
|
} |
1562 |
|
|
1563 |
|
/** |
1568 |
|
Integer[] ints = new Integer[SIZE]; |
1569 |
|
Integer[] array = q.toArray(ints); |
1570 |
|
assertSame(ints, array); |
1571 |
< |
for (int i = 0; i < ints.length; i++) |
1572 |
< |
assertSame(ints[i], q.remove()); |
1571 |
> |
for (Integer o : ints) |
1572 |
> |
assertSame(o, q.remove()); |
1573 |
> |
assertTrue(q.isEmpty()); |
1574 |
|
} |
1575 |
|
|
1576 |
|
/** |
1786 |
|
} |
1787 |
|
|
1788 |
|
/** |
1789 |
< |
* A deserialized serialized deque has same elements in same order |
1789 |
> |
* A deserialized/reserialized deque has same elements in same order |
1790 |
|
*/ |
1791 |
|
public void testSerialization() throws Exception { |
1792 |
|
Queue x = populatedDeque(SIZE); |