263 |
|
*/ |
264 |
|
public void testTimedPoll() throws InterruptedException { |
265 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
266 |
– |
for (int i = 0; i < SIZE; ++i) { |
267 |
– |
long startTime = System.nanoTime(); |
268 |
– |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
269 |
– |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
270 |
– |
} |
266 |
|
long startTime = System.nanoTime(); |
267 |
+ |
for (int i = 0; i < SIZE; ++i) |
268 |
+ |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
269 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
270 |
+ |
|
271 |
+ |
startTime = System.nanoTime(); |
272 |
|
assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
273 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
274 |
|
checkEmpty(q); |
284 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
285 |
|
public void realRun() throws InterruptedException { |
286 |
|
long startTime = System.nanoTime(); |
287 |
< |
for (int i = 0; i < SIZE; ++i) { |
287 |
> |
for (int i = 0; i < SIZE; ++i) |
288 |
|
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
289 |
– |
} |
289 |
|
aboutToWait.countDown(); |
290 |
|
try { |
291 |
|
q.poll(LONG_DELAY_MS, MILLISECONDS); |
292 |
|
shouldThrow(); |
293 |
< |
} catch (InterruptedException success) { |
294 |
< |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
296 |
< |
} |
293 |
> |
} catch (InterruptedException success) {} |
294 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
295 |
|
}}); |
296 |
|
|
297 |
|
aboutToWait.await(); |
298 |
< |
waitForThreadToEnterWaitState(t, LONG_DELAY_MS); |
298 |
> |
waitForThreadToEnterWaitState(t); |
299 |
|
t.interrupt(); |
300 |
|
awaitTermination(t); |
301 |
|
checkEmpty(q); |
309 |
|
final BlockingQueue<Integer> q = populatedQueue(SIZE); |
310 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
311 |
|
public void realRun() throws InterruptedException { |
312 |
+ |
long startTime = System.nanoTime(); |
313 |
|
Thread.currentThread().interrupt(); |
314 |
< |
for (int i = 0; i < SIZE; ++i) { |
316 |
< |
long startTime = System.nanoTime(); |
314 |
> |
for (int i = 0; i < SIZE; ++i) |
315 |
|
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
318 |
– |
assertTrue(millisElapsedSince(startTime) < SMALL_DELAY_MS); |
319 |
– |
} |
316 |
|
try { |
317 |
< |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
317 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
318 |
|
shouldThrow(); |
319 |
|
} catch (InterruptedException success) {} |
320 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
321 |
|
}}); |
322 |
|
|
323 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
323 |
> |
awaitTermination(t); |
324 |
|
checkEmpty(q); |
325 |
|
} |
326 |
|
|
597 |
|
executor.execute(new CheckedRunnable() { |
598 |
|
public void realRun() throws InterruptedException { |
599 |
|
threadsStarted.await(); |
600 |
+ |
long startTime = System.nanoTime(); |
601 |
|
assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS)); |
602 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
603 |
|
}}); |
604 |
|
|
605 |
|
executor.execute(new CheckedRunnable() { |
624 |
|
public void realRun() throws InterruptedException { |
625 |
|
assertNull(q.poll()); |
626 |
|
threadsStarted.await(); |
627 |
+ |
long startTime = System.nanoTime(); |
628 |
|
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
629 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
630 |
|
checkEmpty(q); |
631 |
|
}}); |
632 |
|
|
696 |
|
assertTrue(l.size() >= SIZE); |
697 |
|
for (int i = 0; i < SIZE; ++i) |
698 |
|
assertEquals(i, l.get(i)); |
699 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
699 |
> |
awaitTermination(t); |
700 |
|
assertTrue(q.size() + l.size() >= SIZE); |
701 |
|
} |
702 |
|
|
733 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
734 |
|
public void realRun() throws InterruptedException { |
735 |
|
threadStarted.countDown(); |
736 |
+ |
long startTime = System.nanoTime(); |
737 |
|
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
738 |
|
assertEquals(0, q.getWaitingConsumerCount()); |
739 |
|
assertFalse(q.hasWaitingConsumer()); |
740 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
741 |
|
}}); |
742 |
|
|
743 |
|
threadStarted.await(); |
744 |
< |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
744 |
> |
waitForThreadToEnterWaitState(t); |
745 |
|
assertEquals(1, q.getWaitingConsumerCount()); |
746 |
|
assertTrue(q.hasWaitingConsumer()); |
747 |
|
|
749 |
|
assertEquals(0, q.getWaitingConsumerCount()); |
750 |
|
assertFalse(q.hasWaitingConsumer()); |
751 |
|
|
752 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
752 |
> |
awaitTermination(t); |
753 |
|
} |
754 |
|
|
755 |
|
/** |
780 |
|
}}); |
781 |
|
|
782 |
|
threadStarted.await(); |
783 |
< |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
783 |
> |
waitForThreadToEnterWaitState(t); |
784 |
|
assertEquals(1, q.size()); |
785 |
|
assertSame(five, q.poll()); |
786 |
|
checkEmpty(q); |
787 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
787 |
> |
awaitTermination(t); |
788 |
|
} |
789 |
|
|
790 |
|
/** |
840 |
|
assertEquals(1, q.size()); |
841 |
|
assertTrue(q.offer(three)); |
842 |
|
assertSame(four, q.poll()); |
843 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
843 |
> |
awaitTermination(t); |
844 |
|
} |
845 |
|
|
846 |
|
/** |
863 |
|
assertEquals(1, q.size()); |
864 |
|
assertSame(four, q.take()); |
865 |
|
checkEmpty(q); |
866 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
866 |
> |
awaitTermination(t); |
867 |
|
} |
868 |
|
|
869 |
|
/** |
905 |
|
assertTrue(q.tryTransfer(hotPotato)); |
906 |
|
}}); |
907 |
|
|
908 |
< |
assertSame(hotPotato, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
908 |
> |
long startTime = System.nanoTime(); |
909 |
> |
assertSame(hotPotato, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
910 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
911 |
|
checkEmpty(q); |
912 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
912 |
> |
awaitTermination(t); |
913 |
|
} |
914 |
|
|
915 |
|
/** |
931 |
|
|
932 |
|
assertSame(q.take(), hotPotato); |
933 |
|
checkEmpty(q); |
934 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
934 |
> |
awaitTermination(t); |
935 |
|
} |
936 |
|
|
937 |
|
/** |
944 |
|
|
945 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
946 |
|
public void realRun() throws InterruptedException { |
947 |
+ |
long startTime = System.nanoTime(); |
948 |
|
Thread.currentThread().interrupt(); |
949 |
|
try { |
950 |
|
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
958 |
|
shouldThrow(); |
959 |
|
} catch (InterruptedException success) {} |
960 |
|
assertFalse(Thread.interrupted()); |
961 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
962 |
|
}}); |
963 |
|
|
964 |
|
await(pleaseInterrupt); |
997 |
|
|
998 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
999 |
|
public void realRun() throws InterruptedException { |
1000 |
< |
assertTrue(q.tryTransfer(five, MEDIUM_DELAY_MS, MILLISECONDS)); |
1000 |
> |
long startTime = System.nanoTime(); |
1001 |
> |
assertTrue(q.tryTransfer(five, LONG_DELAY_MS, MILLISECONDS)); |
1002 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1003 |
|
checkEmpty(q); |
1004 |
|
}}); |
1005 |
|
|
1009 |
|
assertSame(four, q.poll()); |
1010 |
|
assertSame(five, q.poll()); |
1011 |
|
checkEmpty(q); |
1012 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
1012 |
> |
awaitTermination(t); |
1013 |
|
} |
1014 |
|
|
1015 |
|
/** |