18 |
|
import java.util.NoSuchElementException; |
19 |
|
import java.util.concurrent.*; |
20 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
21 |
+ |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
22 |
|
import junit.framework.Test; |
23 |
|
import junit.framework.TestSuite; |
24 |
|
|
274 |
|
} |
275 |
|
|
276 |
|
/** |
277 |
< |
* take blocks interruptibly when empty |
277 |
< |
*/ |
278 |
< |
public void testTakeFromEmpty() throws InterruptedException { |
279 |
< |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
280 |
< |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
281 |
< |
public void realRun() throws InterruptedException { |
282 |
< |
q.take(); |
283 |
< |
}}); |
284 |
< |
Thread.sleep(SHORT_DELAY_MS); |
285 |
< |
t.interrupt(); |
286 |
< |
t.join(); |
287 |
< |
} |
288 |
< |
|
289 |
< |
/** |
290 |
< |
* Take removes existing elements until empty, then blocks interruptibly |
277 |
> |
* take removes existing elements until empty, then blocks interruptibly |
278 |
|
*/ |
279 |
|
public void testBlockingTake() throws InterruptedException { |
280 |
|
final LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
328 |
|
for (int i = 0; i < SIZE; ++i) { |
329 |
|
long t0 = System.nanoTime(); |
330 |
|
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
331 |
< |
long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024); |
345 |
< |
assertTrue(millisElapsed < SMALL_DELAY_MS); |
331 |
> |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
332 |
|
} |
333 |
|
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
334 |
|
checkEmpty(q); |
345 |
|
for (int i = 0; i < SIZE; ++i) { |
346 |
|
long t0 = System.nanoTime(); |
347 |
|
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
348 |
< |
long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024); |
363 |
< |
assertTrue(millisElapsed < SMALL_DELAY_MS); |
348 |
> |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
349 |
|
} |
350 |
|
try { |
351 |
|
q.poll(LONG_DELAY_MS, MILLISECONDS); |
1038 |
|
|
1039 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1040 |
|
public void realRun() throws InterruptedException { |
1041 |
+ |
long t0 = System.nanoTime(); |
1042 |
|
assertFalse(q.tryTransfer(new Object(), |
1043 |
|
SHORT_DELAY_MS, MILLISECONDS)); |
1044 |
+ |
assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); |
1045 |
|
}}); |
1046 |
|
|
1060 |
– |
Thread.sleep(SMALL_DELAY_MS); |
1047 |
|
checkEmpty(q); |
1048 |
< |
t.join(); |
1048 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
1049 |
> |
checkEmpty(q); |
1050 |
|
} |
1051 |
|
|
1052 |
|
/** |