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); |
292 |
|
t.start(); |
293 |
|
Thread.sleep(SHORT_DELAY_MS); |
294 |
|
t.interrupt(); |
295 |
< |
t.join(); |
295 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
296 |
|
checkEmpty(q); |
297 |
|
} |
298 |
|
|
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); |
355 |
|
|
356 |
|
Thread.sleep(SMALL_DELAY_MS); |
357 |
|
t.interrupt(); |
358 |
< |
t.join(); |
358 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
359 |
|
checkEmpty(q); |
360 |
|
} |
361 |
|
|
765 |
|
for (int i = 0; i < SIZE; ++i) { |
766 |
|
assertEquals(l.get(i), i); |
767 |
|
} |
768 |
< |
t.join(); |
768 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
769 |
|
assertTrue(q.size() + l.size() >= SIZE); |
770 |
|
} |
771 |
|
|
835 |
|
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
836 |
|
assertEquals(q.getWaitingConsumerCount(), 0); |
837 |
|
assertFalse(q.hasWaitingConsumer()); |
838 |
< |
t.join(); |
838 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
839 |
|
} |
840 |
|
|
841 |
|
/** |
867 |
|
assertEquals(1, q.size()); |
868 |
|
assertEquals(SIZE, (int) q.poll()); |
869 |
|
assertTrue(q.isEmpty()); |
870 |
< |
t.join(); |
870 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
871 |
|
} |
872 |
|
|
873 |
|
/** |
922 |
|
Thread.sleep(SHORT_DELAY_MS); |
923 |
|
assertTrue(q.offer(three)); |
924 |
|
assertSame(four, q.poll()); |
925 |
< |
t.join(); |
925 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
926 |
|
} |
927 |
|
|
928 |
|
/** |
942 |
|
Thread.sleep(SHORT_DELAY_MS); |
943 |
|
assertEquals(SIZE, (int) q.take()); |
944 |
|
checkEmpty(q); |
945 |
< |
t.join(); |
945 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
946 |
|
} |
947 |
|
|
948 |
|
/** |
987 |
|
|
988 |
|
assertSame(hotPotato, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
989 |
|
checkEmpty(q); |
990 |
< |
t.join(); |
990 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
991 |
|
} |
992 |
|
|
993 |
|
/** |
1010 |
|
|
1011 |
|
assertSame(q.take(), hotPotato); |
1012 |
|
checkEmpty(q); |
1013 |
< |
t.join(); |
1013 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
1014 |
|
} |
1015 |
|
|
1016 |
|
/** |
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 |
|
/** |
1068 |
|
assertSame(four, q.poll()); |
1069 |
|
assertSame(five, q.poll()); |
1070 |
|
checkEmpty(q); |
1071 |
< |
t.join(); |
1071 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
1072 |
|
} |
1073 |
|
|
1074 |
|
/** |