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 { |
279 |
|
public void testTakeFromEmpty() throws InterruptedException { |
280 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
281 |
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
282 |
< |
void realRun() throws InterruptedException { |
282 |
> |
public void realRun() throws InterruptedException { |
283 |
|
q.take(); |
284 |
|
}}); |
285 |
|
Thread.sleep(SHORT_DELAY_MS); |
356 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
357 |
|
final LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
358 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
359 |
< |
void realRun() throws InterruptedException { |
359 |
> |
public void realRun() throws InterruptedException { |
360 |
|
for (int i = 0; i < SIZE; ++i) { |
361 |
|
long t0 = System.nanoTime(); |
362 |
< |
threadAssertEquals(i, (int) q.poll(LONG_DELAY_MS, |
355 |
< |
MILLISECONDS)); |
362 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
363 |
|
long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024); |
364 |
|
assertTrue(millisElapsed < SMALL_DELAY_MS); |
365 |
|
} |
376 |
|
} |
377 |
|
|
378 |
|
/** |
372 |
– |
* timed poll before a delayed offer fails; after offer succeeds; |
373 |
– |
* on interruption throws |
374 |
– |
*/ |
375 |
– |
public void testTimedPollWithOffer() throws InterruptedException { |
376 |
– |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
377 |
– |
Thread t = new Thread(new CheckedRunnable() { |
378 |
– |
void realRun() throws InterruptedException { |
379 |
– |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
380 |
– |
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
381 |
– |
try { |
382 |
– |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
383 |
– |
shouldThrow(); |
384 |
– |
} catch (InterruptedException success) {} |
385 |
– |
}}); |
386 |
– |
|
387 |
– |
t.start(); |
388 |
– |
Thread.sleep(SMALL_DELAY_MS); |
389 |
– |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
390 |
– |
t.interrupt(); |
391 |
– |
t.join(); |
392 |
– |
} |
393 |
– |
|
394 |
– |
/** |
379 |
|
* peek returns next element, or null if empty |
380 |
|
*/ |
381 |
|
public void testPeek() throws InterruptedException { |
446 |
|
assertTrue(q.remove(one)); |
447 |
|
assertTrue(q.remove(two)); |
448 |
|
assertTrue(q.add(three)); |
449 |
< |
assertTrue(q.take() == three); |
449 |
> |
assertSame(q.take(), three); |
450 |
|
} |
451 |
|
|
452 |
|
/** |
599 |
|
it.remove(); |
600 |
|
|
601 |
|
it = q.iterator(); |
602 |
< |
assertEquals(it.next(), one); |
603 |
< |
assertEquals(it.next(), three); |
602 |
> |
assertSame(it.next(), one); |
603 |
> |
assertSame(it.next(), three); |
604 |
|
assertFalse(it.hasNext()); |
605 |
|
} |
606 |
|
|
658 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
659 |
|
|
660 |
|
executor.execute(new CheckedRunnable() { |
661 |
< |
void realRun() { |
662 |
< |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, |
679 |
< |
MILLISECONDS)); |
661 |
> |
public void realRun() { |
662 |
> |
assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
663 |
|
}}); |
664 |
|
|
665 |
|
executor.execute(new CheckedRunnable() { |
666 |
< |
void realRun() throws InterruptedException { |
666 |
> |
public void realRun() throws InterruptedException { |
667 |
|
Thread.sleep(SMALL_DELAY_MS); |
668 |
< |
threadAssertEquals(one, q.take()); |
668 |
> |
assertSame(one, q.take()); |
669 |
|
}}); |
670 |
|
|
671 |
|
joinPool(executor); |
679 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
680 |
|
|
681 |
|
executor.execute(new CheckedRunnable() { |
682 |
< |
void realRun() throws InterruptedException { |
683 |
< |
threadAssertNull(q.poll()); |
684 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, |
685 |
< |
MILLISECONDS)); |
703 |
< |
threadAssertTrue(q.isEmpty()); |
682 |
> |
public void realRun() throws InterruptedException { |
683 |
> |
assertNull(q.poll()); |
684 |
> |
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
685 |
> |
assertTrue(q.isEmpty()); |
686 |
|
}}); |
687 |
|
|
688 |
|
executor.execute(new CheckedRunnable() { |
689 |
< |
void realRun() throws InterruptedException { |
689 |
> |
public void realRun() throws InterruptedException { |
690 |
|
Thread.sleep(SMALL_DELAY_MS); |
691 |
|
q.put(one); |
692 |
|
}}); |
772 |
|
public void testDrainToWithActivePut() throws InterruptedException { |
773 |
|
final LinkedTransferQueue q = populatedQueue(SIZE); |
774 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
775 |
< |
void realRun() { |
775 |
> |
public void realRun() { |
776 |
|
q.put(SIZE + 1); |
777 |
|
}}); |
778 |
|
ArrayList l = new ArrayList(); |
808 |
|
} |
809 |
|
|
810 |
|
/** |
811 |
< |
* drainTo(c, n) empties first max {n, size} elements of queue into c |
811 |
> |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
812 |
|
*/ |
813 |
|
public void testDrainToN() { |
814 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
839 |
|
assertFalse(q.hasWaitingConsumer()); |
840 |
|
|
841 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
842 |
< |
void realRun() throws InterruptedException { |
842 |
> |
public void realRun() throws InterruptedException { |
843 |
|
Thread.sleep(SMALL_DELAY_MS); |
844 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
845 |
< |
threadAssertEquals(q.getWaitingConsumerCount(), 1); |
846 |
< |
threadAssertTrue(q.offer(new Object())); |
847 |
< |
threadAssertFalse(q.hasWaitingConsumer()); |
848 |
< |
threadAssertEquals(q.getWaitingConsumerCount(), 0); |
844 |
> |
assertTrue(q.hasWaitingConsumer()); |
845 |
> |
assertEquals(q.getWaitingConsumerCount(), 1); |
846 |
> |
assertTrue(q.offer(one)); |
847 |
> |
assertFalse(q.hasWaitingConsumer()); |
848 |
> |
assertEquals(q.getWaitingConsumerCount(), 0); |
849 |
|
}}); |
850 |
|
|
851 |
< |
assertTrue(q.poll(LONG_DELAY_MS, MILLISECONDS) != null); |
851 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
852 |
|
assertEquals(q.getWaitingConsumerCount(), 0); |
853 |
|
assertFalse(q.hasWaitingConsumer()); |
854 |
|
t.join(); |
874 |
|
= new LinkedTransferQueue<Integer>(); |
875 |
|
|
876 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
877 |
< |
void realRun() throws InterruptedException { |
877 |
> |
public void realRun() throws InterruptedException { |
878 |
|
q.transfer(SIZE); |
879 |
< |
threadAssertTrue(q.isEmpty()); |
879 |
> |
assertTrue(q.isEmpty()); |
880 |
|
}}); |
881 |
|
|
882 |
|
Thread.sleep(SHORT_DELAY_MS); |
894 |
|
= new LinkedTransferQueue<Integer>(); |
895 |
|
|
896 |
|
Thread first = newStartedThread(new CheckedRunnable() { |
897 |
< |
void realRun() throws InterruptedException { |
897 |
> |
public void realRun() throws InterruptedException { |
898 |
|
Integer i = SIZE + 1; |
899 |
|
q.transfer(i); |
900 |
< |
threadAssertTrue(!q.contains(i)); |
901 |
< |
threadAssertEquals(1, q.size()); |
900 |
> |
assertTrue(!q.contains(i)); |
901 |
> |
assertEquals(1, q.size()); |
902 |
|
}}); |
903 |
|
|
904 |
|
Thread interruptedThread = newStartedThread( |
905 |
|
new CheckedInterruptedRunnable() { |
906 |
< |
void realRun() throws InterruptedException { |
906 |
> |
public void realRun() throws InterruptedException { |
907 |
|
while (q.size() == 0) |
908 |
|
Thread.yield(); |
909 |
|
q.transfer(SIZE); |
929 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
930 |
|
|
931 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
932 |
< |
void realRun() throws InterruptedException { |
932 |
> |
public void realRun() throws InterruptedException { |
933 |
|
q.transfer(four); |
934 |
< |
threadAssertFalse(q.contains(four)); |
935 |
< |
threadAssertEquals(three, q.poll()); |
934 |
> |
assertFalse(q.contains(four)); |
935 |
> |
assertSame(three, q.poll()); |
936 |
|
}}); |
937 |
|
|
938 |
|
Thread.sleep(SHORT_DELAY_MS); |
939 |
|
assertTrue(q.offer(three)); |
940 |
< |
assertEquals(four, q.poll()); |
940 |
> |
assertSame(four, q.poll()); |
941 |
|
t.join(); |
942 |
|
} |
943 |
|
|
950 |
|
= new LinkedTransferQueue<Integer>(); |
951 |
|
|
952 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
953 |
< |
void realRun() throws InterruptedException { |
953 |
> |
public void realRun() throws InterruptedException { |
954 |
|
q.transfer(SIZE); |
955 |
|
checkEmpty(q); |
956 |
|
}}); |
992 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
993 |
|
|
994 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
995 |
< |
void realRun() { |
995 |
> |
public void realRun() { |
996 |
|
while (! q.hasWaitingConsumer()) |
997 |
|
Thread.yield(); |
998 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
999 |
< |
threadAssertTrue(q.isEmpty()); |
1000 |
< |
threadAssertTrue(q.size() == 0); |
1001 |
< |
threadAssertTrue(q.tryTransfer(hotPotato)); |
998 |
> |
assertTrue(q.hasWaitingConsumer()); |
999 |
> |
assertTrue(q.isEmpty()); |
1000 |
> |
assertEquals(q.size(), 0); |
1001 |
> |
assertTrue(q.tryTransfer(hotPotato)); |
1002 |
|
}}); |
1003 |
|
|
1004 |
< |
assertTrue(q.poll(MEDIUM_DELAY_MS, MILLISECONDS) == hotPotato); |
1004 |
> |
assertSame(hotPotato, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
1005 |
|
checkEmpty(q); |
1006 |
|
t.join(); |
1007 |
|
} |
1015 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1016 |
|
|
1017 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1018 |
< |
void realRun() { |
1018 |
> |
public void realRun() { |
1019 |
|
while (! q.hasWaitingConsumer()) |
1020 |
|
Thread.yield(); |
1021 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
1022 |
< |
threadAssertTrue(q.isEmpty()); |
1023 |
< |
threadAssertTrue(q.size() == 0); |
1024 |
< |
threadAssertTrue(q.tryTransfer(hotPotato)); |
1021 |
> |
assertTrue(q.hasWaitingConsumer()); |
1022 |
> |
assertTrue(q.isEmpty()); |
1023 |
> |
assertEquals(q.size(), 0); |
1024 |
> |
assertTrue(q.tryTransfer(hotPotato)); |
1025 |
|
}}); |
1026 |
|
|
1027 |
< |
assertTrue(q.take() == hotPotato); |
1027 |
> |
assertSame(q.take(), hotPotato); |
1028 |
|
checkEmpty(q); |
1029 |
|
t.join(); |
1030 |
|
} |
1037 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1038 |
|
|
1039 |
|
Thread toInterrupt = newStartedThread(new CheckedInterruptedRunnable() { |
1040 |
< |
void realRun() throws InterruptedException { |
1040 |
> |
public void realRun() throws InterruptedException { |
1041 |
|
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
1042 |
|
}}); |
1043 |
|
|
1053 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1054 |
|
|
1055 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1056 |
< |
void realRun() throws InterruptedException { |
1057 |
< |
threadAssertFalse |
1058 |
< |
(q.tryTransfer(new Object(), |
1059 |
< |
SHORT_DELAY_MS, MILLISECONDS)); |
1056 |
> |
public void realRun() throws InterruptedException { |
1057 |
> |
long t0 = System.nanoTime(); |
1058 |
> |
assertFalse(q.tryTransfer(new Object(), |
1059 |
> |
SHORT_DELAY_MS, MILLISECONDS)); |
1060 |
> |
long elapsed = NANOSECONDS.toMillis(System.nanoTime() - t0); |
1061 |
> |
assertTrue(elapsed >= SHORT_DELAY_MS); |
1062 |
|
}}); |
1063 |
|
|
1080 |
– |
Thread.sleep(SMALL_DELAY_MS); |
1064 |
|
checkEmpty(q); |
1065 |
< |
t.join(); |
1065 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
1066 |
> |
checkEmpty(q); |
1067 |
|
} |
1068 |
|
|
1069 |
|
/** |
1075 |
|
assertTrue(q.offer(four)); |
1076 |
|
|
1077 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1078 |
< |
void realRun() throws InterruptedException { |
1079 |
< |
threadAssertTrue(q.tryTransfer(five, |
1080 |
< |
MEDIUM_DELAY_MS, MILLISECONDS)); |
1097 |
< |
threadAssertTrue(q.isEmpty()); |
1078 |
> |
public void realRun() throws InterruptedException { |
1079 |
> |
assertTrue(q.tryTransfer(five, MEDIUM_DELAY_MS, MILLISECONDS)); |
1080 |
> |
assertTrue(q.isEmpty()); |
1081 |
|
}}); |
1082 |
|
|
1083 |
|
Thread.sleep(SHORT_DELAY_MS); |
1084 |
|
assertEquals(2, q.size()); |
1085 |
< |
assertEquals(four, q.poll()); |
1086 |
< |
assertEquals(five, q.poll()); |
1085 |
> |
assertSame(four, q.poll()); |
1086 |
> |
assertSame(five, q.poll()); |
1087 |
|
checkEmpty(q); |
1088 |
|
t.join(); |
1089 |
|
} |
1098 |
|
assertEquals(1, q.size()); |
1099 |
|
assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS)); |
1100 |
|
assertEquals(1, q.size()); |
1101 |
< |
assertEquals(four, q.poll()); |
1101 |
> |
assertSame(four, q.poll()); |
1102 |
|
assertNull(q.poll()); |
1103 |
|
checkEmpty(q); |
1104 |
|
} |