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 |
|
|
761 |
|
final CountDownLatch aboutToWait = new CountDownLatch(1); |
762 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
763 |
|
public void realRun() throws InterruptedException { |
764 |
+ |
long startTime = System.nanoTime(); |
765 |
|
for (int i = 0; i < SIZE; ++i) { |
755 |
– |
long t0 = System.nanoTime(); |
766 |
|
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
757 |
– |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
767 |
|
} |
759 |
– |
long t0 = System.nanoTime(); |
768 |
|
aboutToWait.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); |
773 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
774 |
|
} |
775 |
|
}}); |
776 |
|
|
777 |
< |
aboutToWait.await(); |
778 |
< |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
777 |
> |
await(aboutToWait); |
778 |
> |
waitForThreadToEnterWaitState(t); |
779 |
|
t.interrupt(); |
780 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
780 |
> |
awaitTermination(t); |
781 |
|
checkEmpty(q); |
782 |
|
} |
783 |
|
|
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 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1084 |
|
}}); |
1085 |
|
|
1086 |
|
await(pleaseInterrupt); |
1325 |
|
* returning timeout status |
1326 |
|
*/ |
1327 |
|
public void testInterruptedTimedPollLast() throws InterruptedException { |
1328 |
+ |
final LinkedBlockingDeque q = populatedDeque(SIZE); |
1329 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
1330 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1331 |
|
public void realRun() throws InterruptedException { |
1332 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1332 |
> |
long startTime = System.nanoTime(); |
1333 |
|
for (int i = 0; i < SIZE; ++i) { |
1334 |
|
assertEquals(SIZE - i - 1, |
1335 |
|
q.pollLast(LONG_DELAY_MS, MILLISECONDS)); |
1348 |
|
shouldThrow(); |
1349 |
|
} catch (InterruptedException success) {} |
1350 |
|
assertFalse(Thread.interrupted()); |
1351 |
+ |
|
1352 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1353 |
|
}}); |
1354 |
|
|
1355 |
|
await(pleaseInterrupt); |
1356 |
|
assertThreadStaysAlive(t); |
1357 |
|
t.interrupt(); |
1358 |
|
awaitTermination(t); |
1359 |
+ |
checkEmpty(q); |
1360 |
|
} |
1361 |
|
|
1362 |
|
/** |
1389 |
|
shouldThrow(); |
1390 |
|
} catch (InterruptedException success) {} |
1391 |
|
assertFalse(Thread.interrupted()); |
1392 |
+ |
|
1393 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1394 |
|
}}); |
1395 |
|
|
1396 |
|
barrier.await(); |
1687 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1688 |
|
q.add(one); |
1689 |
|
q.add(two); |
1674 |
– |
ExecutorService executor = Executors.newFixedThreadPool(2); |
1690 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
1691 |
< |
executor.execute(new CheckedRunnable() { |
1692 |
< |
public void realRun() throws InterruptedException { |
1693 |
< |
assertFalse(q.offer(three)); |
1694 |
< |
threadsStarted.await(); |
1695 |
< |
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
1696 |
< |
assertEquals(0, q.remainingCapacity()); |
1697 |
< |
}}); |
1698 |
< |
|
1699 |
< |
executor.execute(new CheckedRunnable() { |
1700 |
< |
public void realRun() throws InterruptedException { |
1701 |
< |
threadsStarted.await(); |
1702 |
< |
assertSame(one, q.take()); |
1703 |
< |
}}); |
1704 |
< |
|
1705 |
< |
joinPool(executor); |
1691 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
1692 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
1693 |
> |
executor.execute(new CheckedRunnable() { |
1694 |
> |
public void realRun() throws InterruptedException { |
1695 |
> |
assertFalse(q.offer(three)); |
1696 |
> |
threadsStarted.await(); |
1697 |
> |
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
1698 |
> |
assertEquals(0, q.remainingCapacity()); |
1699 |
> |
}}); |
1700 |
> |
|
1701 |
> |
executor.execute(new CheckedRunnable() { |
1702 |
> |
public void realRun() throws InterruptedException { |
1703 |
> |
threadsStarted.await(); |
1704 |
> |
assertSame(one, q.take()); |
1705 |
> |
}}); |
1706 |
> |
} |
1707 |
|
} |
1708 |
|
|
1709 |
|
/** |
1712 |
|
public void testPollInExecutor() { |
1713 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1714 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
1715 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
1716 |
< |
executor.execute(new CheckedRunnable() { |
1717 |
< |
public void realRun() throws InterruptedException { |
1718 |
< |
assertNull(q.poll()); |
1719 |
< |
threadsStarted.await(); |
1720 |
< |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
1721 |
< |
checkEmpty(q); |
1722 |
< |
}}); |
1723 |
< |
|
1724 |
< |
executor.execute(new CheckedRunnable() { |
1725 |
< |
public void realRun() throws InterruptedException { |
1726 |
< |
threadsStarted.await(); |
1727 |
< |
q.put(one); |
1728 |
< |
}}); |
1729 |
< |
|
1730 |
< |
joinPool(executor); |
1715 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
1716 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
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 |
|
} |
1732 |
|
|
1733 |
|
/** |