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))); |
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 { |
747 |
– |
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 |
|
/** |
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 |
|
} |
1322 |
|
* toArray(a) contains all elements in FIFO order |
1323 |
|
*/ |
1324 |
|
public void testToArray2() { |
1325 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1325 |
> |
LinkedBlockingDeque<Integer> q = populatedDeque(SIZE); |
1326 |
|
Integer[] ints = new Integer[SIZE]; |
1327 |
< |
assertSame(ints, q.toArray(ints)); |
1327 |
> |
Integer[] array = q.toArray(ints); |
1328 |
> |
assertSame(ints, array); |
1329 |
|
for (int i = 0; i < ints.length; i++) |
1330 |
|
assertSame(ints[i], q.remove()); |
1331 |
|
} |