48 |
|
|
49 |
|
/** |
50 |
|
* Returns a new deque of given size containing consecutive |
51 |
< |
* Integers 0 ... n. |
51 |
> |
* Integers 0 ... n - 1. |
52 |
|
*/ |
53 |
|
private LinkedBlockingDeque<Integer> populatedDeque(int n) { |
54 |
|
LinkedBlockingDeque<Integer> q = |
59 |
|
assertFalse(q.isEmpty()); |
60 |
|
assertEquals(0, q.remainingCapacity()); |
61 |
|
assertEquals(n, q.size()); |
62 |
+ |
assertEquals((Integer) 0, q.peekFirst()); |
63 |
+ |
assertEquals((Integer) (n - 1), q.peekLast()); |
64 |
|
return q; |
65 |
|
} |
66 |
|
|
283 |
|
} |
284 |
|
for (int i = 0; i < SIZE; i += 2) { |
285 |
|
assertTrue(q.removeFirstOccurrence(new Integer(i))); |
286 |
< |
assertFalse(q.removeFirstOccurrence(new Integer(i+1))); |
286 |
> |
assertFalse(q.removeFirstOccurrence(new Integer(i + 1))); |
287 |
|
} |
288 |
|
assertTrue(q.isEmpty()); |
289 |
|
} |
298 |
|
} |
299 |
|
for (int i = 0; i < SIZE; i += 2) { |
300 |
|
assertTrue(q.removeLastOccurrence(new Integer(i))); |
301 |
< |
assertFalse(q.removeLastOccurrence(new Integer(i+1))); |
301 |
> |
assertFalse(q.removeLastOccurrence(new Integer(i + 1))); |
302 |
|
} |
303 |
|
assertTrue(q.isEmpty()); |
304 |
|
} |
753 |
|
final CountDownLatch aboutToWait = new CountDownLatch(1); |
754 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
755 |
|
public void realRun() throws InterruptedException { |
756 |
+ |
long startTime = System.nanoTime(); |
757 |
|
for (int i = 0; i < SIZE; ++i) { |
755 |
– |
long t0 = System.nanoTime(); |
758 |
|
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
757 |
– |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
759 |
|
} |
759 |
– |
long t0 = System.nanoTime(); |
760 |
|
aboutToWait.countDown(); |
761 |
|
try { |
762 |
< |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
762 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
763 |
|
shouldThrow(); |
764 |
|
} catch (InterruptedException success) { |
765 |
< |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
765 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
766 |
|
} |
767 |
|
}}); |
768 |
|
|
769 |
|
aboutToWait.await(); |
770 |
< |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
770 |
> |
waitForThreadToEnterWaitState(t); |
771 |
|
t.interrupt(); |
772 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
772 |
> |
awaitTermination(t); |
773 |
|
checkEmpty(q); |
774 |
|
} |
775 |
|
|
1050 |
|
* returning timeout status |
1051 |
|
*/ |
1052 |
|
public void testInterruptedTimedPollFirst() throws InterruptedException { |
1053 |
+ |
final LinkedBlockingDeque q = populatedDeque(SIZE); |
1054 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
1055 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1056 |
|
public void realRun() throws InterruptedException { |
1057 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1057 |
> |
long startTime = System.nanoTime(); |
1058 |
|
for (int i = 0; i < SIZE; ++i) { |
1059 |
|
assertEquals(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS)); |
1060 |
|
} |
1061 |
|
|
1062 |
|
Thread.currentThread().interrupt(); |
1063 |
|
try { |
1064 |
< |
q.pollFirst(SMALL_DELAY_MS, MILLISECONDS); |
1064 |
> |
q.pollFirst(LONG_DELAY_MS, MILLISECONDS); |
1065 |
|
shouldThrow(); |
1066 |
|
} catch (InterruptedException success) {} |
1067 |
|
assertFalse(Thread.interrupted()); |
1072 |
|
shouldThrow(); |
1073 |
|
} catch (InterruptedException success) {} |
1074 |
|
assertFalse(Thread.interrupted()); |
1075 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1076 |
|
}}); |
1077 |
|
|
1078 |
|
await(pleaseInterrupt); |
1248 |
|
public void testTakeLast() throws InterruptedException { |
1249 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1250 |
|
for (int i = 0; i < SIZE; ++i) { |
1251 |
< |
assertEquals(SIZE - i-1, q.takeLast()); |
1251 |
> |
assertEquals(SIZE - i - 1, q.takeLast()); |
1252 |
|
} |
1253 |
|
} |
1254 |
|
|
1261 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1262 |
|
public void realRun() throws InterruptedException { |
1263 |
|
for (int i = 0; i < SIZE; ++i) { |
1264 |
< |
assertEquals(SIZE - i-1, q.takeLast()); |
1264 |
> |
assertEquals(SIZE - i - 1, q.takeLast()); |
1265 |
|
} |
1266 |
|
|
1267 |
|
Thread.currentThread().interrupt(); |
1291 |
|
public void testTimedPollLast0() throws InterruptedException { |
1292 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1293 |
|
for (int i = 0; i < SIZE; ++i) { |
1294 |
< |
assertEquals(SIZE - i-1, q.pollLast(0, MILLISECONDS)); |
1294 |
> |
assertEquals(SIZE - i - 1, q.pollLast(0, MILLISECONDS)); |
1295 |
|
} |
1296 |
|
assertNull(q.pollLast(0, MILLISECONDS)); |
1297 |
|
} |
1303 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1304 |
|
for (int i = 0; i < SIZE; ++i) { |
1305 |
|
long startTime = System.nanoTime(); |
1306 |
< |
assertEquals(SIZE - i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS)); |
1306 |
> |
assertEquals(SIZE - i - 1, q.pollLast(LONG_DELAY_MS, MILLISECONDS)); |
1307 |
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1308 |
|
} |
1309 |
|
long startTime = System.nanoTime(); |
1317 |
|
* returning timeout status |
1318 |
|
*/ |
1319 |
|
public void testInterruptedTimedPollLast() throws InterruptedException { |
1320 |
+ |
final LinkedBlockingDeque q = populatedDeque(SIZE); |
1321 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
1322 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1323 |
|
public void realRun() throws InterruptedException { |
1324 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1324 |
> |
long startTime = System.nanoTime(); |
1325 |
|
for (int i = 0; i < SIZE; ++i) { |
1326 |
< |
assertEquals(SIZE - i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS)); |
1326 |
> |
assertEquals(SIZE - i - 1, |
1327 |
> |
q.pollLast(LONG_DELAY_MS, MILLISECONDS)); |
1328 |
|
} |
1329 |
|
|
1330 |
|
Thread.currentThread().interrupt(); |
1340 |
|
shouldThrow(); |
1341 |
|
} catch (InterruptedException success) {} |
1342 |
|
assertFalse(Thread.interrupted()); |
1343 |
+ |
|
1344 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1345 |
|
}}); |
1346 |
|
|
1347 |
|
await(pleaseInterrupt); |
1348 |
|
assertThreadStaysAlive(t); |
1349 |
|
t.interrupt(); |
1350 |
|
awaitTermination(t); |
1351 |
+ |
checkEmpty(q); |
1352 |
|
} |
1353 |
|
|
1354 |
|
/** |
1381 |
|
shouldThrow(); |
1382 |
|
} catch (InterruptedException success) {} |
1383 |
|
assertFalse(Thread.interrupted()); |
1384 |
+ |
|
1385 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1386 |
|
}}); |
1387 |
|
|
1388 |
|
barrier.await(); |
1679 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1680 |
|
q.add(one); |
1681 |
|
q.add(two); |
1673 |
– |
ExecutorService executor = Executors.newFixedThreadPool(2); |
1682 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
1683 |
< |
executor.execute(new CheckedRunnable() { |
1684 |
< |
public void realRun() throws InterruptedException { |
1685 |
< |
assertFalse(q.offer(three)); |
1686 |
< |
threadsStarted.await(); |
1687 |
< |
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
1688 |
< |
assertEquals(0, q.remainingCapacity()); |
1689 |
< |
}}); |
1690 |
< |
|
1691 |
< |
executor.execute(new CheckedRunnable() { |
1692 |
< |
public void realRun() throws InterruptedException { |
1693 |
< |
threadsStarted.await(); |
1694 |
< |
assertSame(one, q.take()); |
1695 |
< |
}}); |
1696 |
< |
|
1697 |
< |
joinPool(executor); |
1683 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
1684 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
1685 |
> |
executor.execute(new CheckedRunnable() { |
1686 |
> |
public void realRun() throws InterruptedException { |
1687 |
> |
assertFalse(q.offer(three)); |
1688 |
> |
threadsStarted.await(); |
1689 |
> |
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
1690 |
> |
assertEquals(0, q.remainingCapacity()); |
1691 |
> |
}}); |
1692 |
> |
|
1693 |
> |
executor.execute(new CheckedRunnable() { |
1694 |
> |
public void realRun() throws InterruptedException { |
1695 |
> |
threadsStarted.await(); |
1696 |
> |
assertSame(one, q.take()); |
1697 |
> |
}}); |
1698 |
> |
} |
1699 |
|
} |
1700 |
|
|
1701 |
|
/** |
1704 |
|
public void testPollInExecutor() { |
1705 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1706 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
1707 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
1708 |
< |
executor.execute(new CheckedRunnable() { |
1709 |
< |
public void realRun() throws InterruptedException { |
1710 |
< |
assertNull(q.poll()); |
1711 |
< |
threadsStarted.await(); |
1712 |
< |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
1713 |
< |
checkEmpty(q); |
1714 |
< |
}}); |
1715 |
< |
|
1716 |
< |
executor.execute(new CheckedRunnable() { |
1717 |
< |
public void realRun() throws InterruptedException { |
1718 |
< |
threadsStarted.await(); |
1719 |
< |
q.put(one); |
1720 |
< |
}}); |
1721 |
< |
|
1722 |
< |
joinPool(executor); |
1707 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
1708 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
1709 |
> |
executor.execute(new CheckedRunnable() { |
1710 |
> |
public void realRun() throws InterruptedException { |
1711 |
> |
assertNull(q.poll()); |
1712 |
> |
threadsStarted.await(); |
1713 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
1714 |
> |
checkEmpty(q); |
1715 |
> |
}}); |
1716 |
> |
|
1717 |
> |
executor.execute(new CheckedRunnable() { |
1718 |
> |
public void realRun() throws InterruptedException { |
1719 |
> |
threadsStarted.await(); |
1720 |
> |
q.put(one); |
1721 |
> |
}}); |
1722 |
> |
} |
1723 |
|
} |
1724 |
|
|
1725 |
|
/** |