38 |
|
* Create a deque of given size containing consecutive |
39 |
|
* Integers 0 ... n. |
40 |
|
*/ |
41 |
< |
private LinkedBlockingDeque populatedDeque(int n) { |
42 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(n); |
41 |
> |
private LinkedBlockingDeque<Integer> populatedDeque(int n) { |
42 |
> |
LinkedBlockingDeque<Integer> q = |
43 |
> |
new LinkedBlockingDeque<Integer>(n); |
44 |
|
assertTrue(q.isEmpty()); |
45 |
|
for (int i = 0; i < n; i++) |
46 |
|
assertTrue(q.offer(new Integer(i))); |
109 |
|
} |
110 |
|
|
111 |
|
/** |
112 |
< |
* pollFirst succeeds unless empty |
112 |
> |
* pollFirst succeeds unless empty |
113 |
|
*/ |
114 |
|
public void testPollFirst() { |
115 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
120 |
|
} |
121 |
|
|
122 |
|
/** |
123 |
< |
* pollLast succeeds unless empty |
123 |
> |
* pollLast succeeds unless empty |
124 |
|
*/ |
125 |
|
public void testPollLast() { |
126 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
131 |
|
} |
132 |
|
|
133 |
|
/** |
134 |
< |
* peekFirst returns next element, or null if empty |
134 |
> |
* peekFirst returns next element, or null if empty |
135 |
|
*/ |
136 |
|
public void testPeekFirst() { |
137 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
145 |
|
} |
146 |
|
|
147 |
|
/** |
148 |
< |
* peek returns next element, or null if empty |
148 |
> |
* peek returns next element, or null if empty |
149 |
|
*/ |
150 |
|
public void testPeek() { |
151 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
159 |
|
} |
160 |
|
|
161 |
|
/** |
162 |
< |
* peekLast returns next element, or null if empty |
162 |
> |
* peekLast returns next element, or null if empty |
163 |
|
*/ |
164 |
|
public void testPeekLast() { |
165 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
189 |
|
} |
190 |
|
|
191 |
|
/** |
192 |
< |
* getLast() returns last element, or throws NSEE if empty |
192 |
> |
* getLast() returns last element, or throws NSEE if empty |
193 |
|
*/ |
194 |
|
public void testLastElement() { |
195 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
235 |
|
} |
236 |
|
|
237 |
|
/** |
238 |
< |
* remove removes next element, or throws NSEE if empty |
238 |
> |
* remove removes next element, or throws NSEE if empty |
239 |
|
*/ |
240 |
|
public void testRemove() { |
241 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
458 |
|
|
459 |
|
|
460 |
|
/** |
461 |
< |
* pop removes next element, or throws NSEE if empty |
461 |
> |
* pop removes next element, or throws NSEE if empty |
462 |
|
*/ |
463 |
|
public void testPop() { |
464 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
683 |
|
} |
684 |
|
|
685 |
|
/** |
685 |
– |
* take blocks interruptibly when empty |
686 |
– |
*/ |
687 |
– |
public void testTakeFromEmpty() throws InterruptedException { |
688 |
– |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
689 |
– |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
690 |
– |
public void realRun() throws InterruptedException { |
691 |
– |
q.take(); |
692 |
– |
}}; |
693 |
– |
|
694 |
– |
t.start(); |
695 |
– |
Thread.sleep(SHORT_DELAY_MS); |
696 |
– |
t.interrupt(); |
697 |
– |
t.join(); |
698 |
– |
} |
699 |
– |
|
700 |
– |
/** |
686 |
|
* Take removes existing elements until empty, then blocks interruptibly |
687 |
|
*/ |
688 |
|
public void testBlockingTake() throws InterruptedException { |
743 |
|
* returning timeout status |
744 |
|
*/ |
745 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
746 |
< |
Thread t = new Thread(new CheckedRunnable() { |
746 |
> |
final BlockingQueue<Integer> q = populatedDeque(SIZE); |
747 |
> |
final CountDownLatch aboutToWait = new CountDownLatch(1); |
748 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
749 |
|
public void realRun() throws InterruptedException { |
763 |
– |
LinkedBlockingDeque q = populatedDeque(SIZE); |
750 |
|
for (int i = 0; i < SIZE; ++i) { |
751 |
< |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
751 |
> |
long t0 = System.nanoTime(); |
752 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
753 |
> |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
754 |
|
} |
755 |
+ |
long t0 = System.nanoTime(); |
756 |
+ |
aboutToWait.countDown(); |
757 |
|
try { |
758 |
< |
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
758 |
> |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
759 |
|
shouldThrow(); |
760 |
< |
} catch (InterruptedException success) {} |
760 |
> |
} catch (InterruptedException success) { |
761 |
> |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
762 |
> |
} |
763 |
|
}}); |
764 |
|
|
765 |
< |
t.start(); |
766 |
< |
Thread.sleep(SHORT_DELAY_MS); |
765 |
> |
aboutToWait.await(); |
766 |
> |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
767 |
|
t.interrupt(); |
768 |
< |
t.join(); |
768 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
769 |
> |
checkEmpty(q); |
770 |
|
} |
771 |
|
|
772 |
|
/** |
960 |
|
} |
961 |
|
|
962 |
|
/** |
963 |
< |
* timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds; |
964 |
< |
* on interruption throws |
963 |
> |
* timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds; |
964 |
> |
* on interruption throws |
965 |
|
*/ |
966 |
|
public void testTimedPollFirstWithOfferFirst() throws InterruptedException { |
967 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1216 |
|
public void testRemoveElement() { |
1217 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1218 |
|
for (int i = 1; i < SIZE; i+=2) { |
1219 |
< |
assertTrue(q.remove(new Integer(i))); |
1219 |
> |
assertTrue(q.contains(i)); |
1220 |
> |
assertTrue(q.remove(i)); |
1221 |
> |
assertFalse(q.contains(i)); |
1222 |
> |
assertTrue(q.contains(i-1)); |
1223 |
|
} |
1224 |
|
for (int i = 0; i < SIZE; i+=2) { |
1225 |
< |
assertTrue(q.remove(new Integer(i))); |
1226 |
< |
assertFalse(q.remove(new Integer(i+1))); |
1225 |
> |
assertTrue(q.contains(i)); |
1226 |
> |
assertTrue(q.remove(i)); |
1227 |
> |
assertFalse(q.contains(i)); |
1228 |
> |
assertFalse(q.remove(i+1)); |
1229 |
> |
assertFalse(q.contains(i+1)); |
1230 |
|
} |
1231 |
|
assertTrue(q.isEmpty()); |
1232 |
|
} |
1309 |
|
} |
1310 |
|
|
1311 |
|
/** |
1312 |
< |
* toArray contains all elements |
1312 |
> |
* toArray contains all elements in FIFO order |
1313 |
|
*/ |
1314 |
|
public void testToArray() throws InterruptedException{ |
1315 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1316 |
|
Object[] o = q.toArray(); |
1317 |
|
for (int i = 0; i < o.length; i++) |
1318 |
< |
assertEquals(o[i], q.take()); |
1318 |
> |
assertSame(o[i], q.poll()); |
1319 |
|
} |
1320 |
|
|
1321 |
|
/** |
1322 |
< |
* toArray(a) contains all elements |
1322 |
> |
* toArray(a) contains all elements in FIFO order |
1323 |
|
*/ |
1324 |
< |
public void testToArray2() throws InterruptedException { |
1325 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1324 |
> |
public void testToArray2() { |
1325 |
> |
LinkedBlockingDeque<Integer> q = populatedDeque(SIZE); |
1326 |
|
Integer[] ints = new Integer[SIZE]; |
1327 |
< |
ints = (Integer[])q.toArray(ints); |
1327 |
> |
Integer[] array = q.toArray(ints); |
1328 |
> |
assertSame(ints, array); |
1329 |
|
for (int i = 0; i < ints.length; i++) |
1330 |
< |
assertEquals(ints[i], q.take()); |
1330 |
> |
assertSame(ints[i], q.remove()); |
1331 |
|
} |
1332 |
|
|
1333 |
|
/** |
1334 |
< |
* toArray(null) throws NPE |
1334 |
> |
* toArray(null) throws NullPointerException |
1335 |
|
*/ |
1336 |
< |
public void testToArray_BadArg() { |
1336 |
> |
public void testToArray_NullArg() { |
1337 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1338 |
|
try { |
1339 |
< |
Object o[] = q.toArray(null); |
1339 |
> |
q.toArray(null); |
1340 |
|
shouldThrow(); |
1341 |
|
} catch (NullPointerException success) {} |
1342 |
|
} |
1343 |
|
|
1344 |
|
/** |
1345 |
< |
* toArray with incompatible array type throws CCE |
1345 |
> |
* toArray(incompatible array type) throws ArrayStoreException |
1346 |
|
*/ |
1347 |
|
public void testToArray1_BadArg() { |
1348 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1349 |
|
try { |
1350 |
< |
Object o[] = q.toArray(new String[10]); |
1350 |
> |
q.toArray(new String[10]); |
1351 |
|
shouldThrow(); |
1352 |
|
} catch (ArrayStoreException success) {} |
1353 |
|
} |
1417 |
|
|
1418 |
|
|
1419 |
|
/** |
1420 |
< |
* Descending iterator iterates through all elements |
1420 |
> |
* Descending iterator iterates through all elements |
1421 |
|
*/ |
1422 |
|
public void testDescendingIterator() { |
1423 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1436 |
|
} |
1437 |
|
|
1438 |
|
/** |
1439 |
< |
* Descending iterator ordering is reverse FIFO |
1439 |
> |
* Descending iterator ordering is reverse FIFO |
1440 |
|
*/ |
1441 |
|
public void testDescendingIteratorOrdering() { |
1442 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(); |
1645 |
|
} |
1646 |
|
|
1647 |
|
/** |
1648 |
< |
* drainTo(c, n) empties first max {n, size} elements of deque into c |
1648 |
> |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
1649 |
|
*/ |
1650 |
|
public void testDrainToN() { |
1651 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(); |
1654 |
|
assertTrue(q.offer(new Integer(j))); |
1655 |
|
ArrayList l = new ArrayList(); |
1656 |
|
q.drainTo(l, i); |
1657 |
< |
int k = (i < SIZE)? i : SIZE; |
1657 |
> |
int k = (i < SIZE) ? i : SIZE; |
1658 |
|
assertEquals(l.size(), k); |
1659 |
|
assertEquals(q.size(), SIZE-k); |
1660 |
|
for (int j = 0; j < k; ++j) |