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 |
|
|
25 |
|
@SuppressWarnings({"unchecked", "rawtypes"}) |
26 |
|
public class LinkedTransferQueueTest extends JSR166TestCase { |
27 |
|
|
28 |
+ |
public static class Generic extends BlockingQueueTest { |
29 |
+ |
protected BlockingQueue emptyCollection() { |
30 |
+ |
return new LinkedTransferQueue(); |
31 |
+ |
} |
32 |
+ |
} |
33 |
+ |
|
34 |
|
public static void main(String[] args) { |
35 |
|
junit.textui.TestRunner.run(suite()); |
36 |
|
} |
37 |
|
|
38 |
|
public static Test suite() { |
39 |
< |
return new TestSuite(LinkedTransferQueueTest.class); |
39 |
> |
return newTestSuite(LinkedTransferQueueTest.class, |
40 |
> |
new Generic().testSuite()); |
41 |
|
} |
42 |
|
|
43 |
|
void checkEmpty(LinkedTransferQueue q) throws InterruptedException { |
274 |
|
} |
275 |
|
|
276 |
|
/** |
277 |
< |
* take blocks interruptibly when empty |
270 |
< |
*/ |
271 |
< |
public void testTakeFromEmpty() throws InterruptedException { |
272 |
< |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
273 |
< |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
274 |
< |
public void realRun() throws InterruptedException { |
275 |
< |
q.take(); |
276 |
< |
}}); |
277 |
< |
Thread.sleep(SHORT_DELAY_MS); |
278 |
< |
t.interrupt(); |
279 |
< |
t.join(); |
280 |
< |
} |
281 |
< |
|
282 |
< |
/** |
283 |
< |
* 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 |
|
|
309 |
|
} |
310 |
|
|
311 |
|
/** |
312 |
< |
* timed pool with zero timeout succeeds when non-empty, else times out |
312 |
> |
* timed poll with zero timeout succeeds when non-empty, else times out |
313 |
|
*/ |
314 |
|
public void testTimedPoll0() throws InterruptedException { |
315 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
321 |
|
} |
322 |
|
|
323 |
|
/** |
324 |
< |
* timed pool with nonzero timeout succeeds when non-empty, else times out |
324 |
> |
* timed poll with nonzero timeout succeeds when non-empty, else times out |
325 |
|
*/ |
326 |
|
public void testTimedPoll() throws InterruptedException { |
327 |
|
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); |
338 |
< |
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); |
356 |
< |
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 |
|
|
362 |
|
/** |
371 |
– |
* timed poll before a delayed offer fails; after offer succeeds; |
372 |
– |
* on interruption throws |
373 |
– |
*/ |
374 |
– |
public void testTimedPollWithOffer() throws InterruptedException { |
375 |
– |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
376 |
– |
Thread t = new Thread(new CheckedRunnable() { |
377 |
– |
public void realRun() throws InterruptedException { |
378 |
– |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
379 |
– |
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
380 |
– |
try { |
381 |
– |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
382 |
– |
shouldThrow(); |
383 |
– |
} catch (InterruptedException success) {} |
384 |
– |
}}); |
385 |
– |
|
386 |
– |
t.start(); |
387 |
– |
Thread.sleep(SMALL_DELAY_MS); |
388 |
– |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
389 |
– |
t.interrupt(); |
390 |
– |
t.join(); |
391 |
– |
} |
392 |
– |
|
393 |
– |
/** |
363 |
|
* peek returns next element, or null if empty |
364 |
|
*/ |
365 |
|
public void testPeek() throws InterruptedException { |
583 |
|
it.remove(); |
584 |
|
|
585 |
|
it = q.iterator(); |
586 |
< |
assertEquals(it.next(), one); |
587 |
< |
assertEquals(it.next(), three); |
586 |
> |
assertSame(it.next(), one); |
587 |
> |
assertSame(it.next(), three); |
588 |
|
assertFalse(it.hasNext()); |
589 |
|
} |
590 |
|
|
649 |
|
executor.execute(new CheckedRunnable() { |
650 |
|
public void realRun() throws InterruptedException { |
651 |
|
Thread.sleep(SMALL_DELAY_MS); |
652 |
< |
assertEquals(one, q.take()); |
652 |
> |
assertSame(one, q.take()); |
653 |
|
}}); |
654 |
|
|
655 |
|
joinPool(executor); |
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 |
|
|
792 |
|
} |
793 |
|
|
794 |
|
/** |
795 |
< |
* drainTo(c, n) empties first max {n, size} elements of queue into c |
795 |
> |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
796 |
|
*/ |
797 |
|
public void testDrainToN() { |
798 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
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 |
|
/** |
860 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
861 |
|
public void realRun() throws InterruptedException { |
862 |
|
q.transfer(SIZE); |
863 |
< |
threadAssertTrue(q.isEmpty()); |
863 |
> |
assertTrue(q.isEmpty()); |
864 |
|
}}); |
865 |
|
|
866 |
|
Thread.sleep(SHORT_DELAY_MS); |
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 |
|
/** |
881 |
|
public void realRun() throws InterruptedException { |
882 |
|
Integer i = SIZE + 1; |
883 |
|
q.transfer(i); |
884 |
< |
threadAssertTrue(!q.contains(i)); |
885 |
< |
threadAssertEquals(1, q.size()); |
884 |
> |
assertTrue(!q.contains(i)); |
885 |
> |
assertEquals(1, q.size()); |
886 |
|
}}); |
887 |
|
|
888 |
|
Thread interruptedThread = newStartedThread( |
915 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
916 |
|
public void realRun() throws InterruptedException { |
917 |
|
q.transfer(four); |
918 |
< |
threadAssertFalse(q.contains(four)); |
919 |
< |
threadAssertEquals(three, q.poll()); |
918 |
> |
assertFalse(q.contains(four)); |
919 |
> |
assertSame(three, q.poll()); |
920 |
|
}}); |
921 |
|
|
922 |
|
Thread.sleep(SHORT_DELAY_MS); |
923 |
|
assertTrue(q.offer(three)); |
924 |
< |
assertEquals(four, q.poll()); |
925 |
< |
t.join(); |
924 |
> |
assertSame(four, q.poll()); |
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 |
|
/** |
979 |
|
public void realRun() { |
980 |
|
while (! q.hasWaitingConsumer()) |
981 |
|
Thread.yield(); |
982 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
983 |
< |
threadAssertTrue(q.isEmpty()); |
984 |
< |
threadAssertTrue(q.size() == 0); |
985 |
< |
threadAssertTrue(q.tryTransfer(hotPotato)); |
982 |
> |
assertTrue(q.hasWaitingConsumer()); |
983 |
> |
assertTrue(q.isEmpty()); |
984 |
> |
assertEquals(q.size(), 0); |
985 |
> |
assertTrue(q.tryTransfer(hotPotato)); |
986 |
|
}}); |
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 |
|
/** |
1002 |
|
public void realRun() { |
1003 |
|
while (! q.hasWaitingConsumer()) |
1004 |
|
Thread.yield(); |
1005 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
1006 |
< |
threadAssertTrue(q.isEmpty()); |
1007 |
< |
threadAssertTrue(q.size() == 0); |
1008 |
< |
threadAssertTrue(q.tryTransfer(hotPotato)); |
1005 |
> |
assertTrue(q.hasWaitingConsumer()); |
1006 |
> |
assertTrue(q.isEmpty()); |
1007 |
> |
assertEquals(q.size(), 0); |
1008 |
> |
assertTrue(q.tryTransfer(hotPotato)); |
1009 |
|
}}); |
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 |
|
|
1076 |
– |
Thread.sleep(SMALL_DELAY_MS); |
1047 |
|
checkEmpty(q); |
1048 |
< |
t.join(); |
1048 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
1049 |
> |
checkEmpty(q); |
1050 |
|
} |
1051 |
|
|
1052 |
|
/** |
1065 |
|
|
1066 |
|
Thread.sleep(SHORT_DELAY_MS); |
1067 |
|
assertEquals(2, q.size()); |
1068 |
< |
assertEquals(four, q.poll()); |
1069 |
< |
assertEquals(five, q.poll()); |
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 |
|
/** |
1081 |
|
assertEquals(1, q.size()); |
1082 |
|
assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS)); |
1083 |
|
assertEquals(1, q.size()); |
1084 |
< |
assertEquals(four, q.poll()); |
1084 |
> |
assertSame(four, q.poll()); |
1085 |
|
assertNull(q.poll()); |
1086 |
|
checkEmpty(q); |
1087 |
|
} |