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()); |
666 |
|
}}); |
667 |
|
|
668 |
|
await(pleaseInterrupt); |
669 |
< |
assertThreadStaysAlive(t); |
669 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
670 |
|
t.interrupt(); |
671 |
|
awaitTermination(t); |
672 |
|
} |
689 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
690 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
691 |
|
public void realRun() throws InterruptedException { |
692 |
< |
for (int i = 0; i < SIZE; ++i) { |
683 |
< |
assertEquals(i, q.take()); |
684 |
< |
} |
692 |
> |
for (int i = 0; i < SIZE; i++) assertEquals(i, q.take()); |
693 |
|
|
694 |
|
Thread.currentThread().interrupt(); |
695 |
|
try { |
707 |
|
}}); |
708 |
|
|
709 |
|
await(pleaseInterrupt); |
710 |
< |
assertThreadStaysAlive(t); |
710 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
711 |
|
t.interrupt(); |
712 |
|
awaitTermination(t); |
713 |
|
} |
759 |
|
final CountDownLatch aboutToWait = new CountDownLatch(1); |
760 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
761 |
|
public void realRun() throws InterruptedException { |
762 |
+ |
long startTime = System.nanoTime(); |
763 |
|
for (int i = 0; i < SIZE; ++i) { |
755 |
– |
long t0 = System.nanoTime(); |
764 |
|
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
757 |
– |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
765 |
|
} |
759 |
– |
long t0 = System.nanoTime(); |
766 |
|
aboutToWait.countDown(); |
767 |
|
try { |
768 |
< |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
768 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
769 |
|
shouldThrow(); |
770 |
|
} catch (InterruptedException success) { |
771 |
< |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
771 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
772 |
|
} |
773 |
|
}}); |
774 |
|
|
775 |
< |
aboutToWait.await(); |
776 |
< |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
775 |
> |
await(aboutToWait); |
776 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
777 |
|
t.interrupt(); |
778 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
778 |
> |
awaitTermination(t); |
779 |
|
checkEmpty(q); |
780 |
|
} |
781 |
|
|
832 |
|
}}); |
833 |
|
|
834 |
|
await(pleaseInterrupt); |
835 |
< |
assertThreadStaysAlive(t); |
835 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
836 |
|
t.interrupt(); |
837 |
|
awaitTermination(t); |
838 |
|
assertEquals(SIZE, q.size()); |
867 |
|
assertEquals(capacity - 1, q.take()); |
868 |
|
|
869 |
|
await(pleaseInterrupt); |
870 |
< |
assertThreadStaysAlive(t); |
870 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
871 |
|
t.interrupt(); |
872 |
|
awaitTermination(t); |
873 |
|
assertEquals(0, q.remainingCapacity()); |
894 |
|
}}); |
895 |
|
|
896 |
|
await(pleaseInterrupt); |
897 |
< |
assertThreadStaysAlive(t); |
897 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
898 |
|
t.interrupt(); |
899 |
|
awaitTermination(t); |
900 |
|
} |
926 |
|
}}); |
927 |
|
|
928 |
|
await(threadStarted); |
929 |
< |
assertThreadStaysAlive(t); |
929 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
930 |
|
t.interrupt(); |
931 |
|
awaitTermination(t); |
932 |
|
} |
967 |
|
}}); |
968 |
|
|
969 |
|
await(threadStarted); |
970 |
< |
assertThreadStaysAlive(t); |
970 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
971 |
|
t.interrupt(); |
972 |
|
awaitTermination(t); |
973 |
|
} |
999 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
1000 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1001 |
|
public void realRun() throws InterruptedException { |
1002 |
< |
for (int i = 0; i < SIZE; ++i) { |
997 |
< |
assertEquals(i, q.takeFirst()); |
998 |
< |
} |
1002 |
> |
for (int i = 0; i < SIZE; i++) assertEquals(i, q.takeFirst()); |
1003 |
|
|
1004 |
|
Thread.currentThread().interrupt(); |
1005 |
|
try { |
1017 |
|
}}); |
1018 |
|
|
1019 |
|
await(pleaseInterrupt); |
1020 |
< |
assertThreadStaysAlive(t); |
1020 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
1021 |
|
t.interrupt(); |
1022 |
|
awaitTermination(t); |
1023 |
|
} |
1054 |
|
* returning timeout status |
1055 |
|
*/ |
1056 |
|
public void testInterruptedTimedPollFirst() throws InterruptedException { |
1057 |
+ |
final LinkedBlockingDeque q = populatedDeque(SIZE); |
1058 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
1059 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1060 |
|
public void realRun() throws InterruptedException { |
1061 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1061 |
> |
long startTime = System.nanoTime(); |
1062 |
|
for (int i = 0; i < SIZE; ++i) { |
1063 |
|
assertEquals(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS)); |
1064 |
|
} |
1065 |
|
|
1066 |
|
Thread.currentThread().interrupt(); |
1067 |
|
try { |
1068 |
< |
q.pollFirst(SMALL_DELAY_MS, MILLISECONDS); |
1068 |
> |
q.pollFirst(LONG_DELAY_MS, MILLISECONDS); |
1069 |
|
shouldThrow(); |
1070 |
|
} catch (InterruptedException success) {} |
1071 |
|
assertFalse(Thread.interrupted()); |
1076 |
|
shouldThrow(); |
1077 |
|
} catch (InterruptedException success) {} |
1078 |
|
assertFalse(Thread.interrupted()); |
1079 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1080 |
|
}}); |
1081 |
|
|
1082 |
|
await(pleaseInterrupt); |
1083 |
< |
assertThreadStaysAlive(t); |
1083 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
1084 |
|
t.interrupt(); |
1085 |
|
awaitTermination(t); |
1086 |
|
} |
1179 |
|
}}); |
1180 |
|
|
1181 |
|
await(pleaseInterrupt); |
1182 |
< |
assertThreadStaysAlive(t); |
1182 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
1183 |
|
t.interrupt(); |
1184 |
|
awaitTermination(t); |
1185 |
|
assertEquals(SIZE, q.size()); |
1214 |
|
assertEquals(0, q.take()); |
1215 |
|
|
1216 |
|
await(pleaseInterrupt); |
1217 |
< |
assertThreadStaysAlive(t); |
1217 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
1218 |
|
t.interrupt(); |
1219 |
|
awaitTermination(t); |
1220 |
|
assertEquals(0, q.remainingCapacity()); |
1241 |
|
}}); |
1242 |
|
|
1243 |
|
await(pleaseInterrupt); |
1244 |
< |
assertThreadStaysAlive(t); |
1244 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
1245 |
|
t.interrupt(); |
1246 |
|
awaitTermination(t); |
1247 |
|
} |
1252 |
|
public void testTakeLast() throws InterruptedException { |
1253 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1254 |
|
for (int i = 0; i < SIZE; ++i) { |
1255 |
< |
assertEquals(SIZE - i-1, q.takeLast()); |
1255 |
> |
assertEquals(SIZE - i - 1, q.takeLast()); |
1256 |
|
} |
1257 |
|
} |
1258 |
|
|
1264 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
1265 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1266 |
|
public void realRun() throws InterruptedException { |
1267 |
< |
for (int i = 0; i < SIZE; ++i) { |
1268 |
< |
assertEquals(SIZE - i-1, q.takeLast()); |
1263 |
< |
} |
1267 |
> |
for (int i = 0; i < SIZE; i++) |
1268 |
> |
assertEquals(SIZE - i - 1, q.takeLast()); |
1269 |
|
|
1270 |
|
Thread.currentThread().interrupt(); |
1271 |
|
try { |
1283 |
|
}}); |
1284 |
|
|
1285 |
|
await(pleaseInterrupt); |
1286 |
< |
assertThreadStaysAlive(t); |
1286 |
> |
assertThreadBlocks(t, Thread.State.WAITING); |
1287 |
|
t.interrupt(); |
1288 |
|
awaitTermination(t); |
1289 |
|
} |
1294 |
|
public void testTimedPollLast0() throws InterruptedException { |
1295 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1296 |
|
for (int i = 0; i < SIZE; ++i) { |
1297 |
< |
assertEquals(SIZE - i-1, q.pollLast(0, MILLISECONDS)); |
1297 |
> |
assertEquals(SIZE - i - 1, q.pollLast(0, MILLISECONDS)); |
1298 |
|
} |
1299 |
|
assertNull(q.pollLast(0, MILLISECONDS)); |
1300 |
|
} |
1306 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1307 |
|
for (int i = 0; i < SIZE; ++i) { |
1308 |
|
long startTime = System.nanoTime(); |
1309 |
< |
assertEquals(SIZE - i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS)); |
1309 |
> |
assertEquals(SIZE - i - 1, q.pollLast(LONG_DELAY_MS, MILLISECONDS)); |
1310 |
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1311 |
|
} |
1312 |
|
long startTime = System.nanoTime(); |
1320 |
|
* returning timeout status |
1321 |
|
*/ |
1322 |
|
public void testInterruptedTimedPollLast() throws InterruptedException { |
1323 |
+ |
final LinkedBlockingDeque q = populatedDeque(SIZE); |
1324 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
1325 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1326 |
|
public void realRun() throws InterruptedException { |
1327 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1327 |
> |
long startTime = System.nanoTime(); |
1328 |
|
for (int i = 0; i < SIZE; ++i) { |
1329 |
< |
assertEquals(SIZE - i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS)); |
1329 |
> |
assertEquals(SIZE - i - 1, |
1330 |
> |
q.pollLast(LONG_DELAY_MS, MILLISECONDS)); |
1331 |
|
} |
1332 |
|
|
1333 |
|
Thread.currentThread().interrupt(); |
1343 |
|
shouldThrow(); |
1344 |
|
} catch (InterruptedException success) {} |
1345 |
|
assertFalse(Thread.interrupted()); |
1346 |
+ |
|
1347 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1348 |
|
}}); |
1349 |
|
|
1350 |
|
await(pleaseInterrupt); |
1351 |
< |
assertThreadStaysAlive(t); |
1351 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
1352 |
|
t.interrupt(); |
1353 |
|
awaitTermination(t); |
1354 |
+ |
checkEmpty(q); |
1355 |
|
} |
1356 |
|
|
1357 |
|
/** |
1384 |
|
shouldThrow(); |
1385 |
|
} catch (InterruptedException success) {} |
1386 |
|
assertFalse(Thread.interrupted()); |
1387 |
+ |
|
1388 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1389 |
|
}}); |
1390 |
|
|
1391 |
|
barrier.await(); |
1394 |
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1395 |
|
|
1396 |
|
barrier.await(); |
1397 |
< |
assertThreadStaysAlive(t); |
1397 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
1398 |
|
t.interrupt(); |
1399 |
|
awaitTermination(t); |
1400 |
|
} |
1682 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1683 |
|
q.add(one); |
1684 |
|
q.add(two); |
1673 |
– |
ExecutorService executor = Executors.newFixedThreadPool(2); |
1685 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
1686 |
< |
executor.execute(new CheckedRunnable() { |
1687 |
< |
public void realRun() throws InterruptedException { |
1688 |
< |
assertFalse(q.offer(three)); |
1689 |
< |
threadsStarted.await(); |
1690 |
< |
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
1691 |
< |
assertEquals(0, q.remainingCapacity()); |
1692 |
< |
}}); |
1693 |
< |
|
1694 |
< |
executor.execute(new CheckedRunnable() { |
1695 |
< |
public void realRun() throws InterruptedException { |
1696 |
< |
threadsStarted.await(); |
1697 |
< |
assertSame(one, q.take()); |
1698 |
< |
}}); |
1699 |
< |
|
1700 |
< |
joinPool(executor); |
1686 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
1687 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
1688 |
> |
executor.execute(new CheckedRunnable() { |
1689 |
> |
public void realRun() throws InterruptedException { |
1690 |
> |
assertFalse(q.offer(three)); |
1691 |
> |
threadsStarted.await(); |
1692 |
> |
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
1693 |
> |
assertEquals(0, q.remainingCapacity()); |
1694 |
> |
}}); |
1695 |
> |
|
1696 |
> |
executor.execute(new CheckedRunnable() { |
1697 |
> |
public void realRun() throws InterruptedException { |
1698 |
> |
threadsStarted.await(); |
1699 |
> |
assertSame(one, q.take()); |
1700 |
> |
}}); |
1701 |
> |
} |
1702 |
|
} |
1703 |
|
|
1704 |
|
/** |
1707 |
|
public void testPollInExecutor() { |
1708 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1709 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
1710 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
1711 |
< |
executor.execute(new CheckedRunnable() { |
1712 |
< |
public void realRun() throws InterruptedException { |
1713 |
< |
assertNull(q.poll()); |
1714 |
< |
threadsStarted.await(); |
1715 |
< |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
1716 |
< |
checkEmpty(q); |
1717 |
< |
}}); |
1718 |
< |
|
1719 |
< |
executor.execute(new CheckedRunnable() { |
1720 |
< |
public void realRun() throws InterruptedException { |
1721 |
< |
threadsStarted.await(); |
1722 |
< |
q.put(one); |
1723 |
< |
}}); |
1724 |
< |
|
1725 |
< |
joinPool(executor); |
1710 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
1711 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
1712 |
> |
executor.execute(new CheckedRunnable() { |
1713 |
> |
public void realRun() throws InterruptedException { |
1714 |
> |
assertNull(q.poll()); |
1715 |
> |
threadsStarted.await(); |
1716 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
1717 |
> |
checkEmpty(q); |
1718 |
> |
}}); |
1719 |
> |
|
1720 |
> |
executor.execute(new CheckedRunnable() { |
1721 |
> |
public void realRun() throws InterruptedException { |
1722 |
> |
threadsStarted.await(); |
1723 |
> |
q.put(one); |
1724 |
> |
}}); |
1725 |
> |
} |
1726 |
|
} |
1727 |
|
|
1728 |
|
/** |