24 |
|
|
25 |
|
@SuppressWarnings({"unchecked", "rawtypes"}) |
26 |
|
public class LinkedTransferQueueTest extends JSR166TestCase { |
27 |
+ |
static class Implementation implements CollectionImplementation { |
28 |
+ |
public Class<?> klazz() { return LinkedTransferQueue.class; } |
29 |
+ |
public Collection emptyCollection() { return new LinkedTransferQueue(); } |
30 |
+ |
public Object makeElement(int i) { return i; } |
31 |
+ |
public boolean isConcurrent() { return true; } |
32 |
+ |
public boolean permitsNulls() { return false; } |
33 |
+ |
} |
34 |
|
|
35 |
|
public static class Generic extends BlockingQueueTest { |
36 |
|
protected BlockingQueue emptyCollection() { |
44 |
|
|
45 |
|
public static Test suite() { |
46 |
|
return newTestSuite(LinkedTransferQueueTest.class, |
47 |
< |
new Generic().testSuite()); |
47 |
> |
new Generic().testSuite(), |
48 |
> |
CollectionTest.testSuite(new Implementation())); |
49 |
|
} |
50 |
|
|
51 |
|
/** |
263 |
|
*/ |
264 |
|
public void testTimedPoll() throws InterruptedException { |
265 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
258 |
– |
for (int i = 0; i < SIZE; ++i) { |
259 |
– |
long startTime = System.nanoTime(); |
260 |
– |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
261 |
– |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
262 |
– |
} |
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); |
283 |
|
final CountDownLatch aboutToWait = new CountDownLatch(1); |
284 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
285 |
|
public void realRun() throws InterruptedException { |
286 |
< |
for (int i = 0; i < SIZE; ++i) { |
287 |
< |
long t0 = System.nanoTime(); |
286 |
> |
long startTime = System.nanoTime(); |
287 |
> |
for (int i = 0; i < SIZE; ++i) |
288 |
|
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
281 |
– |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
282 |
– |
} |
283 |
– |
long t0 = System.nanoTime(); |
289 |
|
aboutToWait.countDown(); |
290 |
|
try { |
291 |
< |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
291 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
292 |
|
shouldThrow(); |
293 |
< |
} catch (InterruptedException success) { |
294 |
< |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
290 |
< |
} |
293 |
> |
} catch (InterruptedException success) {} |
294 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
295 |
|
}}); |
296 |
|
|
297 |
|
aboutToWait.await(); |
298 |
< |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
298 |
> |
waitForThreadToEnterWaitState(t); |
299 |
|
t.interrupt(); |
300 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
300 |
> |
awaitTermination(t); |
301 |
|
checkEmpty(q); |
302 |
|
} |
303 |
|
|
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) { |
310 |
< |
long t0 = System.nanoTime(); |
314 |
> |
for (int i = 0; i < SIZE; ++i) |
315 |
|
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
312 |
– |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
313 |
– |
} |
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 |
|
|
591 |
|
public void testOfferInExecutor() { |
592 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
593 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
594 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
595 |
< |
|
593 |
< |
executor.execute(new CheckedRunnable() { |
594 |
< |
public void realRun() throws InterruptedException { |
595 |
< |
threadsStarted.await(); |
596 |
< |
assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS)); |
597 |
< |
}}); |
594 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
595 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
596 |
|
|
597 |
< |
executor.execute(new CheckedRunnable() { |
598 |
< |
public void realRun() throws InterruptedException { |
599 |
< |
threadsStarted.await(); |
600 |
< |
assertSame(one, q.take()); |
601 |
< |
checkEmpty(q); |
602 |
< |
}}); |
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 |
< |
joinPool(executor); |
605 |
> |
executor.execute(new CheckedRunnable() { |
606 |
> |
public void realRun() throws InterruptedException { |
607 |
> |
threadsStarted.await(); |
608 |
> |
assertSame(one, q.take()); |
609 |
> |
checkEmpty(q); |
610 |
> |
}}); |
611 |
> |
} |
612 |
|
} |
613 |
|
|
614 |
|
/** |
617 |
|
public void testPollInExecutor() { |
618 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
619 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
620 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
621 |
< |
|
617 |
< |
executor.execute(new CheckedRunnable() { |
618 |
< |
public void realRun() throws InterruptedException { |
619 |
< |
assertNull(q.poll()); |
620 |
< |
threadsStarted.await(); |
621 |
< |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
622 |
< |
checkEmpty(q); |
623 |
< |
}}); |
620 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
621 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
622 |
|
|
623 |
< |
executor.execute(new CheckedRunnable() { |
624 |
< |
public void realRun() throws InterruptedException { |
625 |
< |
threadsStarted.await(); |
626 |
< |
q.put(one); |
627 |
< |
}}); |
623 |
> |
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 |
|
|
633 |
< |
joinPool(executor); |
633 |
> |
executor.execute(new CheckedRunnable() { |
634 |
> |
public void realRun() throws InterruptedException { |
635 |
> |
threadsStarted.await(); |
636 |
> |
q.put(one); |
637 |
> |
}}); |
638 |
> |
} |
639 |
|
} |
640 |
|
|
641 |
|
/** |
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); |
976 |
|
|
977 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
978 |
|
public void realRun() throws InterruptedException { |
979 |
< |
long t0 = System.nanoTime(); |
979 |
> |
long startTime = System.nanoTime(); |
980 |
|
assertFalse(q.tryTransfer(new Object(), |
981 |
|
timeoutMillis(), MILLISECONDS)); |
982 |
< |
assertTrue(millisElapsedSince(t0) >= timeoutMillis()); |
982 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
983 |
|
checkEmpty(q); |
984 |
|
}}); |
985 |
|
|
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 |
|
/** |
1020 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1021 |
|
assertTrue(q.offer(four)); |
1022 |
|
assertEquals(1, q.size()); |
1023 |
< |
long t0 = System.nanoTime(); |
1023 |
> |
long startTime = System.nanoTime(); |
1024 |
|
assertFalse(q.tryTransfer(five, timeoutMillis(), MILLISECONDS)); |
1025 |
< |
assertTrue(millisElapsedSince(t0) >= timeoutMillis()); |
1025 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
1026 |
|
assertEquals(1, q.size()); |
1027 |
|
assertSame(four, q.poll()); |
1028 |
|
assertNull(q.poll()); |