41 |
|
} |
42 |
|
|
43 |
|
public static Test suite() { |
44 |
+ |
class Implementation implements CollectionImplementation { |
45 |
+ |
public Class<?> klazz() { return LinkedBlockingDeque.class; } |
46 |
+ |
public Collection emptyCollection() { return new LinkedBlockingDeque(); } |
47 |
+ |
public Object makeElement(int i) { return i; } |
48 |
+ |
public boolean isConcurrent() { return true; } |
49 |
+ |
public boolean permitsNulls() { return false; } |
50 |
+ |
} |
51 |
|
return newTestSuite(LinkedBlockingDequeTest.class, |
52 |
|
new Unbounded().testSuite(), |
53 |
< |
new Bounded().testSuite()); |
53 |
> |
new Bounded().testSuite(), |
54 |
> |
CollectionTest.testSuite(new Implementation())); |
55 |
|
} |
56 |
|
|
57 |
|
/** |
58 |
|
* Returns a new deque of given size containing consecutive |
59 |
< |
* Integers 0 ... n. |
59 |
> |
* Integers 0 ... n - 1. |
60 |
|
*/ |
61 |
< |
private LinkedBlockingDeque<Integer> populatedDeque(int n) { |
61 |
> |
private static LinkedBlockingDeque<Integer> populatedDeque(int n) { |
62 |
|
LinkedBlockingDeque<Integer> q = |
63 |
|
new LinkedBlockingDeque<Integer>(n); |
64 |
|
assertTrue(q.isEmpty()); |
67 |
|
assertFalse(q.isEmpty()); |
68 |
|
assertEquals(0, q.remainingCapacity()); |
69 |
|
assertEquals(n, q.size()); |
70 |
+ |
assertEquals((Integer) 0, q.peekFirst()); |
71 |
+ |
assertEquals((Integer) (n - 1), q.peekLast()); |
72 |
|
return q; |
73 |
|
} |
74 |
|
|
291 |
|
} |
292 |
|
for (int i = 0; i < SIZE; i += 2) { |
293 |
|
assertTrue(q.removeFirstOccurrence(new Integer(i))); |
294 |
< |
assertFalse(q.removeFirstOccurrence(new Integer(i+1))); |
294 |
> |
assertFalse(q.removeFirstOccurrence(new Integer(i + 1))); |
295 |
|
} |
296 |
|
assertTrue(q.isEmpty()); |
297 |
|
} |
306 |
|
} |
307 |
|
for (int i = 0; i < SIZE; i += 2) { |
308 |
|
assertTrue(q.removeLastOccurrence(new Integer(i))); |
309 |
< |
assertFalse(q.removeLastOccurrence(new Integer(i+1))); |
309 |
> |
assertFalse(q.removeLastOccurrence(new Integer(i + 1))); |
310 |
|
} |
311 |
|
assertTrue(q.isEmpty()); |
312 |
|
} |
604 |
|
}}); |
605 |
|
|
606 |
|
await(pleaseInterrupt); |
607 |
< |
assertThreadStaysAlive(t); |
607 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
608 |
|
t.interrupt(); |
609 |
|
awaitTermination(t); |
610 |
|
assertEquals(SIZE, q.size()); |
639 |
|
assertEquals(0, q.take()); |
640 |
|
|
641 |
|
await(pleaseInterrupt); |
642 |
< |
assertThreadStaysAlive(t); |
642 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
643 |
|
t.interrupt(); |
644 |
|
awaitTermination(t); |
645 |
|
assertEquals(0, q.remainingCapacity()); |
663 |
|
q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
664 |
|
shouldThrow(); |
665 |
|
} catch (InterruptedException success) {} |
666 |
+ |
assertFalse(Thread.interrupted()); |
667 |
|
}}); |
668 |
|
|
669 |
|
await(pleaseInterrupt); |
670 |
< |
assertThreadStaysAlive(t); |
670 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
671 |
|
t.interrupt(); |
672 |
|
awaitTermination(t); |
673 |
|
} |
690 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
691 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
692 |
|
public void realRun() throws InterruptedException { |
693 |
< |
for (int i = 0; i < SIZE; ++i) { |
683 |
< |
assertEquals(i, q.take()); |
684 |
< |
} |
693 |
> |
for (int i = 0; i < SIZE; i++) assertEquals(i, q.take()); |
694 |
|
|
695 |
|
Thread.currentThread().interrupt(); |
696 |
|
try { |
708 |
|
}}); |
709 |
|
|
710 |
|
await(pleaseInterrupt); |
711 |
< |
assertThreadStaysAlive(t); |
711 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
712 |
|
t.interrupt(); |
713 |
|
awaitTermination(t); |
714 |
|
} |
757 |
|
*/ |
758 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
759 |
|
final BlockingQueue<Integer> q = populatedDeque(SIZE); |
760 |
< |
final CountDownLatch aboutToWait = new CountDownLatch(1); |
760 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
761 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
762 |
|
public void realRun() throws InterruptedException { |
763 |
+ |
long startTime = System.nanoTime(); |
764 |
|
for (int i = 0; i < SIZE; ++i) { |
755 |
– |
long t0 = System.nanoTime(); |
765 |
|
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
757 |
– |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
766 |
|
} |
767 |
< |
long t0 = System.nanoTime(); |
768 |
< |
aboutToWait.countDown(); |
767 |
> |
|
768 |
> |
pleaseInterrupt.countDown(); |
769 |
|
try { |
770 |
< |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
770 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
771 |
|
shouldThrow(); |
772 |
< |
} catch (InterruptedException success) { |
773 |
< |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
774 |
< |
} |
772 |
> |
} catch (InterruptedException success) {} |
773 |
> |
assertFalse(Thread.interrupted()); |
774 |
> |
|
775 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
776 |
|
}}); |
777 |
|
|
778 |
< |
aboutToWait.await(); |
779 |
< |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
778 |
> |
await(pleaseInterrupt); |
779 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
780 |
|
t.interrupt(); |
781 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
781 |
> |
awaitTermination(t); |
782 |
|
checkEmpty(q); |
783 |
|
} |
784 |
|
|
835 |
|
}}); |
836 |
|
|
837 |
|
await(pleaseInterrupt); |
838 |
< |
assertThreadStaysAlive(t); |
838 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
839 |
|
t.interrupt(); |
840 |
|
awaitTermination(t); |
841 |
|
assertEquals(SIZE, q.size()); |
870 |
|
assertEquals(capacity - 1, q.take()); |
871 |
|
|
872 |
|
await(pleaseInterrupt); |
873 |
< |
assertThreadStaysAlive(t); |
873 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
874 |
|
t.interrupt(); |
875 |
|
awaitTermination(t); |
876 |
|
assertEquals(0, q.remainingCapacity()); |
894 |
|
q.offerFirst(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
895 |
|
shouldThrow(); |
896 |
|
} catch (InterruptedException success) {} |
897 |
+ |
assertFalse(Thread.interrupted()); |
898 |
|
}}); |
899 |
|
|
900 |
|
await(pleaseInterrupt); |
901 |
< |
assertThreadStaysAlive(t); |
901 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
902 |
|
t.interrupt(); |
903 |
|
awaitTermination(t); |
904 |
|
} |
930 |
|
}}); |
931 |
|
|
932 |
|
await(threadStarted); |
933 |
< |
assertThreadStaysAlive(t); |
933 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
934 |
|
t.interrupt(); |
935 |
|
awaitTermination(t); |
936 |
|
} |
971 |
|
}}); |
972 |
|
|
973 |
|
await(threadStarted); |
974 |
< |
assertThreadStaysAlive(t); |
974 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
975 |
|
t.interrupt(); |
976 |
|
awaitTermination(t); |
977 |
|
} |
1003 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
1004 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1005 |
|
public void realRun() throws InterruptedException { |
1006 |
< |
for (int i = 0; i < SIZE; ++i) { |
997 |
< |
assertEquals(i, q.takeFirst()); |
998 |
< |
} |
1006 |
> |
for (int i = 0; i < SIZE; i++) assertEquals(i, q.takeFirst()); |
1007 |
|
|
1008 |
|
Thread.currentThread().interrupt(); |
1009 |
|
try { |
1021 |
|
}}); |
1022 |
|
|
1023 |
|
await(pleaseInterrupt); |
1024 |
< |
assertThreadStaysAlive(t); |
1024 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
1025 |
|
t.interrupt(); |
1026 |
|
awaitTermination(t); |
1027 |
|
} |
1058 |
|
* returning timeout status |
1059 |
|
*/ |
1060 |
|
public void testInterruptedTimedPollFirst() throws InterruptedException { |
1061 |
+ |
final LinkedBlockingDeque q = populatedDeque(SIZE); |
1062 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
1063 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1064 |
|
public void realRun() throws InterruptedException { |
1065 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1065 |
> |
long startTime = System.nanoTime(); |
1066 |
|
for (int i = 0; i < SIZE; ++i) { |
1067 |
|
assertEquals(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS)); |
1068 |
|
} |
1069 |
|
|
1070 |
|
Thread.currentThread().interrupt(); |
1071 |
|
try { |
1072 |
< |
q.pollFirst(SMALL_DELAY_MS, MILLISECONDS); |
1072 |
> |
q.pollFirst(LONG_DELAY_MS, MILLISECONDS); |
1073 |
|
shouldThrow(); |
1074 |
|
} catch (InterruptedException success) {} |
1075 |
|
assertFalse(Thread.interrupted()); |
1080 |
|
shouldThrow(); |
1081 |
|
} catch (InterruptedException success) {} |
1082 |
|
assertFalse(Thread.interrupted()); |
1083 |
+ |
|
1084 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1085 |
|
}}); |
1086 |
|
|
1087 |
|
await(pleaseInterrupt); |
1088 |
< |
assertThreadStaysAlive(t); |
1088 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
1089 |
|
t.interrupt(); |
1090 |
|
awaitTermination(t); |
1091 |
|
} |
1118 |
|
q.pollFirst(LONG_DELAY_MS, MILLISECONDS); |
1119 |
|
shouldThrow(); |
1120 |
|
} catch (InterruptedException success) {} |
1121 |
+ |
assertFalse(Thread.interrupted()); |
1122 |
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1123 |
|
}}); |
1124 |
|
|
1127 |
|
assertTrue(q.offerFirst(zero, LONG_DELAY_MS, MILLISECONDS)); |
1128 |
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1129 |
|
barrier.await(); |
1130 |
< |
assertThreadStaysAlive(t); |
1130 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
1131 |
|
t.interrupt(); |
1132 |
|
awaitTermination(t); |
1133 |
|
} |
1185 |
|
}}); |
1186 |
|
|
1187 |
|
await(pleaseInterrupt); |
1188 |
< |
assertThreadStaysAlive(t); |
1188 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
1189 |
|
t.interrupt(); |
1190 |
|
awaitTermination(t); |
1191 |
|
assertEquals(SIZE, q.size()); |
1220 |
|
assertEquals(0, q.take()); |
1221 |
|
|
1222 |
|
await(pleaseInterrupt); |
1223 |
< |
assertThreadStaysAlive(t); |
1223 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
1224 |
|
t.interrupt(); |
1225 |
|
awaitTermination(t); |
1226 |
|
assertEquals(0, q.remainingCapacity()); |
1247 |
|
}}); |
1248 |
|
|
1249 |
|
await(pleaseInterrupt); |
1250 |
< |
assertThreadStaysAlive(t); |
1250 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
1251 |
|
t.interrupt(); |
1252 |
|
awaitTermination(t); |
1253 |
|
} |
1258 |
|
public void testTakeLast() throws InterruptedException { |
1259 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1260 |
|
for (int i = 0; i < SIZE; ++i) { |
1261 |
< |
assertEquals(SIZE - i-1, q.takeLast()); |
1261 |
> |
assertEquals(SIZE - i - 1, q.takeLast()); |
1262 |
|
} |
1263 |
|
} |
1264 |
|
|
1270 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
1271 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1272 |
|
public void realRun() throws InterruptedException { |
1273 |
< |
for (int i = 0; i < SIZE; ++i) { |
1274 |
< |
assertEquals(SIZE - i-1, q.takeLast()); |
1263 |
< |
} |
1273 |
> |
for (int i = 0; i < SIZE; i++) |
1274 |
> |
assertEquals(SIZE - i - 1, q.takeLast()); |
1275 |
|
|
1276 |
|
Thread.currentThread().interrupt(); |
1277 |
|
try { |
1289 |
|
}}); |
1290 |
|
|
1291 |
|
await(pleaseInterrupt); |
1292 |
< |
assertThreadStaysAlive(t); |
1292 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
1293 |
|
t.interrupt(); |
1294 |
|
awaitTermination(t); |
1295 |
|
} |
1300 |
|
public void testTimedPollLast0() throws InterruptedException { |
1301 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1302 |
|
for (int i = 0; i < SIZE; ++i) { |
1303 |
< |
assertEquals(SIZE - i-1, q.pollLast(0, MILLISECONDS)); |
1303 |
> |
assertEquals(SIZE - i - 1, q.pollLast(0, MILLISECONDS)); |
1304 |
|
} |
1305 |
|
assertNull(q.pollLast(0, MILLISECONDS)); |
1306 |
|
} |
1312 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1313 |
|
for (int i = 0; i < SIZE; ++i) { |
1314 |
|
long startTime = System.nanoTime(); |
1315 |
< |
assertEquals(SIZE - i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS)); |
1315 |
> |
assertEquals(SIZE - i - 1, q.pollLast(LONG_DELAY_MS, MILLISECONDS)); |
1316 |
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1317 |
|
} |
1318 |
|
long startTime = System.nanoTime(); |
1326 |
|
* returning timeout status |
1327 |
|
*/ |
1328 |
|
public void testInterruptedTimedPollLast() throws InterruptedException { |
1329 |
+ |
final LinkedBlockingDeque q = populatedDeque(SIZE); |
1330 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
1331 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1332 |
|
public void realRun() throws InterruptedException { |
1333 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1333 |
> |
long startTime = System.nanoTime(); |
1334 |
|
for (int i = 0; i < SIZE; ++i) { |
1335 |
< |
assertEquals(SIZE - i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS)); |
1335 |
> |
assertEquals(SIZE - i - 1, |
1336 |
> |
q.pollLast(LONG_DELAY_MS, MILLISECONDS)); |
1337 |
|
} |
1338 |
|
|
1339 |
|
Thread.currentThread().interrupt(); |
1349 |
|
shouldThrow(); |
1350 |
|
} catch (InterruptedException success) {} |
1351 |
|
assertFalse(Thread.interrupted()); |
1352 |
+ |
|
1353 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1354 |
|
}}); |
1355 |
|
|
1356 |
|
await(pleaseInterrupt); |
1357 |
< |
assertThreadStaysAlive(t); |
1357 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
1358 |
|
t.interrupt(); |
1359 |
|
awaitTermination(t); |
1360 |
+ |
checkEmpty(q); |
1361 |
|
} |
1362 |
|
|
1363 |
|
/** |
1390 |
|
shouldThrow(); |
1391 |
|
} catch (InterruptedException success) {} |
1392 |
|
assertFalse(Thread.interrupted()); |
1393 |
+ |
|
1394 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1395 |
|
}}); |
1396 |
|
|
1397 |
|
barrier.await(); |
1400 |
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1401 |
|
|
1402 |
|
barrier.await(); |
1403 |
< |
assertThreadStaysAlive(t); |
1403 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
1404 |
|
t.interrupt(); |
1405 |
|
awaitTermination(t); |
1406 |
|
} |
1688 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1689 |
|
q.add(one); |
1690 |
|
q.add(two); |
1673 |
– |
ExecutorService executor = Executors.newFixedThreadPool(2); |
1691 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
1692 |
< |
executor.execute(new CheckedRunnable() { |
1693 |
< |
public void realRun() throws InterruptedException { |
1694 |
< |
assertFalse(q.offer(three)); |
1695 |
< |
threadsStarted.await(); |
1696 |
< |
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
1697 |
< |
assertEquals(0, q.remainingCapacity()); |
1698 |
< |
}}); |
1699 |
< |
|
1700 |
< |
executor.execute(new CheckedRunnable() { |
1701 |
< |
public void realRun() throws InterruptedException { |
1702 |
< |
threadsStarted.await(); |
1703 |
< |
assertSame(one, q.take()); |
1704 |
< |
}}); |
1705 |
< |
|
1706 |
< |
joinPool(executor); |
1692 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
1693 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
1694 |
> |
executor.execute(new CheckedRunnable() { |
1695 |
> |
public void realRun() throws InterruptedException { |
1696 |
> |
assertFalse(q.offer(three)); |
1697 |
> |
threadsStarted.await(); |
1698 |
> |
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
1699 |
> |
assertEquals(0, q.remainingCapacity()); |
1700 |
> |
}}); |
1701 |
> |
|
1702 |
> |
executor.execute(new CheckedRunnable() { |
1703 |
> |
public void realRun() throws InterruptedException { |
1704 |
> |
threadsStarted.await(); |
1705 |
> |
assertSame(one, q.take()); |
1706 |
> |
}}); |
1707 |
> |
} |
1708 |
|
} |
1709 |
|
|
1710 |
|
/** |
1713 |
|
public void testPollInExecutor() { |
1714 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1715 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
1716 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
1717 |
< |
executor.execute(new CheckedRunnable() { |
1718 |
< |
public void realRun() throws InterruptedException { |
1719 |
< |
assertNull(q.poll()); |
1720 |
< |
threadsStarted.await(); |
1721 |
< |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
1722 |
< |
checkEmpty(q); |
1723 |
< |
}}); |
1724 |
< |
|
1725 |
< |
executor.execute(new CheckedRunnable() { |
1726 |
< |
public void realRun() throws InterruptedException { |
1727 |
< |
threadsStarted.await(); |
1728 |
< |
q.put(one); |
1729 |
< |
}}); |
1730 |
< |
|
1731 |
< |
joinPool(executor); |
1716 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
1717 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
1718 |
> |
executor.execute(new CheckedRunnable() { |
1719 |
> |
public void realRun() throws InterruptedException { |
1720 |
> |
assertNull(q.poll()); |
1721 |
> |
threadsStarted.await(); |
1722 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
1723 |
> |
checkEmpty(q); |
1724 |
> |
}}); |
1725 |
> |
|
1726 |
> |
executor.execute(new CheckedRunnable() { |
1727 |
> |
public void realRun() throws InterruptedException { |
1728 |
> |
threadsStarted.await(); |
1729 |
> |
q.put(one); |
1730 |
> |
}}); |
1731 |
> |
} |
1732 |
|
} |
1733 |
|
|
1734 |
|
/** |