13 |
|
import java.io.ObjectOutputStream; |
14 |
|
import java.util.ArrayList; |
15 |
|
import java.util.Arrays; |
16 |
– |
import java.util.ConcurrentModificationException; |
16 |
|
import java.util.Iterator; |
17 |
|
import java.util.List; |
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 { |
52 |
|
try { |
53 |
|
q.element(); |
54 |
|
shouldThrow(); |
55 |
< |
} catch (NoSuchElementException success) { |
49 |
< |
} |
55 |
> |
} catch (NoSuchElementException success) {} |
56 |
|
try { |
57 |
|
q.iterator().next(); |
58 |
|
shouldThrow(); |
59 |
< |
} catch (NoSuchElementException success) { |
54 |
< |
} |
59 |
> |
} catch (NoSuchElementException success) {} |
60 |
|
try { |
61 |
|
q.remove(); |
62 |
|
shouldThrow(); |
63 |
< |
} catch (NoSuchElementException success) { |
59 |
< |
} |
63 |
> |
} catch (NoSuchElementException success) {} |
64 |
|
} |
65 |
|
|
66 |
|
/** |
80 |
|
try { |
81 |
|
new LinkedTransferQueue(null); |
82 |
|
shouldThrow(); |
83 |
< |
} catch (NullPointerException success) { |
80 |
< |
} |
83 |
> |
} catch (NullPointerException success) {} |
84 |
|
} |
85 |
|
|
86 |
|
/** |
92 |
|
Integer[] ints = new Integer[SIZE]; |
93 |
|
new LinkedTransferQueue(Arrays.asList(ints)); |
94 |
|
shouldThrow(); |
95 |
< |
} catch (NullPointerException success) { |
93 |
< |
} |
95 |
> |
} catch (NullPointerException success) {} |
96 |
|
} |
97 |
|
|
98 |
|
/** |
107 |
|
} |
108 |
|
new LinkedTransferQueue(Arrays.asList(ints)); |
109 |
|
shouldThrow(); |
110 |
< |
} catch (NullPointerException success) { |
109 |
< |
} |
110 |
> |
} catch (NullPointerException success) {} |
111 |
|
} |
112 |
|
|
113 |
|
/** |
159 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
160 |
|
q.offer(null); |
161 |
|
shouldThrow(); |
162 |
< |
} catch (NullPointerException success) { |
162 |
< |
} |
162 |
> |
} catch (NullPointerException success) {} |
163 |
|
} |
164 |
|
|
165 |
|
/** |
170 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
171 |
|
q.add(null); |
172 |
|
shouldThrow(); |
173 |
< |
} catch (NullPointerException success) { |
174 |
< |
} |
173 |
> |
} catch (NullPointerException success) {} |
174 |
|
} |
175 |
|
|
176 |
|
/** |
181 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
182 |
|
q.addAll(null); |
183 |
|
shouldThrow(); |
184 |
< |
} catch (NullPointerException success) { |
186 |
< |
} |
184 |
> |
} catch (NullPointerException success) {} |
185 |
|
} |
186 |
|
|
187 |
|
/** |
192 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
193 |
|
q.addAll(q); |
194 |
|
shouldThrow(); |
195 |
< |
} catch (IllegalArgumentException success) { |
198 |
< |
} |
195 |
> |
} catch (IllegalArgumentException success) {} |
196 |
|
} |
197 |
|
|
198 |
|
/** |
204 |
|
Integer[] ints = new Integer[SIZE]; |
205 |
|
q.addAll(Arrays.asList(ints)); |
206 |
|
shouldThrow(); |
207 |
< |
} catch (NullPointerException success) { |
211 |
< |
} |
207 |
> |
} catch (NullPointerException success) {} |
208 |
|
} |
209 |
|
|
210 |
|
/** |
220 |
|
} |
221 |
|
q.addAll(Arrays.asList(ints)); |
222 |
|
shouldThrow(); |
223 |
< |
} catch (NullPointerException success) { |
228 |
< |
} |
223 |
> |
} catch (NullPointerException success) {} |
224 |
|
} |
225 |
|
|
226 |
|
/** |
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); |
292 |
|
*/ |
293 |
|
public void testBlockingTake() throws InterruptedException { |
294 |
|
final LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
295 |
< |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
296 |
< |
void realRun() throws InterruptedException { |
295 |
> |
Thread t = new Thread(new CheckedRunnable() { |
296 |
> |
public void realRun() throws InterruptedException { |
297 |
|
for (int i = 0; i < SIZE; ++i) { |
298 |
< |
threadAssertEquals(i, (int) q.take()); |
298 |
> |
assertEquals(i, (int) q.take()); |
299 |
|
} |
300 |
< |
q.take(); |
300 |
> |
try { |
301 |
> |
q.take(); |
302 |
> |
shouldThrow(); |
303 |
> |
} catch (InterruptedException success) {} |
304 |
|
}}); |
305 |
< |
Thread.sleep(SMALL_DELAY_MS); |
305 |
> |
|
306 |
> |
t.start(); |
307 |
> |
Thread.sleep(SHORT_DELAY_MS); |
308 |
|
t.interrupt(); |
309 |
|
t.join(); |
310 |
|
checkEmpty(q); |
355 |
|
*/ |
356 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
357 |
|
final LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
358 |
< |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
359 |
< |
void realRun() throws InterruptedException { |
358 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
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, |
363 |
< |
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 |
|
} |
366 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
366 |
> |
try { |
367 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
368 |
> |
shouldThrow(); |
369 |
> |
} catch (InterruptedException success) {} |
370 |
|
}}); |
369 |
– |
Thread.sleep(SMALL_DELAY_MS); |
370 |
– |
t.interrupt(); |
371 |
– |
t.join(); |
372 |
– |
checkEmpty(q); |
373 |
– |
} |
371 |
|
|
375 |
– |
/** |
376 |
– |
* timed poll before a delayed offer fails; after offer succeeds; |
377 |
– |
* on interruption throws |
378 |
– |
*/ |
379 |
– |
public void testTimedPollWithOffer() throws InterruptedException { |
380 |
– |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
381 |
– |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
382 |
– |
void realRun() throws InterruptedException { |
383 |
– |
threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
384 |
– |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
385 |
– |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
386 |
– |
}}); |
372 |
|
Thread.sleep(SMALL_DELAY_MS); |
388 |
– |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
373 |
|
t.interrupt(); |
374 |
|
t.join(); |
375 |
+ |
checkEmpty(q); |
376 |
|
} |
377 |
|
|
378 |
|
/** |
402 |
|
try { |
403 |
|
q.element(); |
404 |
|
shouldThrow(); |
405 |
< |
} catch (NoSuchElementException success) { |
421 |
< |
} |
405 |
> |
} catch (NoSuchElementException success) {} |
406 |
|
checkEmpty(q); |
407 |
|
} |
408 |
|
|
417 |
|
try { |
418 |
|
q.remove(); |
419 |
|
shouldThrow(); |
420 |
< |
} catch (NoSuchElementException success) { |
437 |
< |
} |
420 |
> |
} catch (NoSuchElementException success) {} |
421 |
|
checkEmpty(q); |
422 |
|
} |
423 |
|
|
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 |
|
/** |
554 |
|
* toArray(null) throws NullPointerException |
555 |
|
*/ |
556 |
|
public void testToArray_BadArg() { |
557 |
+ |
LinkedTransferQueue q = populatedQueue(SIZE); |
558 |
|
try { |
575 |
– |
LinkedTransferQueue q = populatedQueue(SIZE); |
559 |
|
Object o[] = q.toArray(null); |
560 |
|
shouldThrow(); |
561 |
< |
} catch (NullPointerException success) { |
579 |
< |
} |
561 |
> |
} catch (NullPointerException success) {} |
562 |
|
} |
563 |
|
|
564 |
|
/** |
565 |
|
* toArray(incompatible array type) throws CCE |
566 |
|
*/ |
567 |
|
public void testToArray1_BadArg() { |
568 |
+ |
LinkedTransferQueue q = populatedQueue(SIZE); |
569 |
|
try { |
587 |
– |
LinkedTransferQueue q = populatedQueue(SIZE); |
570 |
|
Object o[] = q.toArray(new String[10]); |
571 |
|
shouldThrow(); |
572 |
< |
} catch (ArrayStoreException success) { |
591 |
< |
} |
572 |
> |
} catch (ArrayStoreException success) {} |
573 |
|
} |
574 |
|
|
575 |
|
/** |
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, |
682 |
< |
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)); |
706 |
< |
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 |
|
}}); |
726 |
|
try { |
727 |
|
q.drainTo(null); |
728 |
|
shouldThrow(); |
729 |
< |
} catch (NullPointerException success) { |
751 |
< |
} |
729 |
> |
} catch (NullPointerException success) {} |
730 |
|
} |
731 |
|
|
732 |
|
/** |
737 |
|
try { |
738 |
|
q.drainTo(q); |
739 |
|
shouldThrow(); |
740 |
< |
} catch (IllegalArgumentException success) { |
763 |
< |
} |
740 |
> |
} catch (IllegalArgumentException success) {} |
741 |
|
} |
742 |
|
|
743 |
|
/** |
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(); |
793 |
|
try { |
794 |
|
q.drainTo(null, SIZE); |
795 |
|
shouldThrow(); |
796 |
< |
} catch (NullPointerException success) { |
820 |
< |
} |
796 |
> |
} catch (NullPointerException success) {} |
797 |
|
} |
798 |
|
|
799 |
|
/** |
804 |
|
try { |
805 |
|
q.drainTo(q, SIZE); |
806 |
|
shouldThrow(); |
807 |
< |
} catch (IllegalArgumentException success) { |
832 |
< |
} |
807 |
> |
} catch (IllegalArgumentException success) {} |
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(); |
862 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
863 |
|
q.transfer(null); |
864 |
|
shouldThrow(); |
865 |
< |
} catch (NullPointerException ex) { |
891 |
< |
} |
865 |
> |
} catch (NullPointerException success) {} |
866 |
|
} |
867 |
|
|
868 |
|
/** |
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 |
|
}}); |
969 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
970 |
|
q.tryTransfer(null); |
971 |
|
shouldThrow(); |
972 |
< |
} catch (NullPointerException ex) { |
999 |
< |
} |
972 |
> |
} catch (NullPointerException success) {} |
973 |
|
} |
974 |
|
|
975 |
|
/** |
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 |
|
|
1089 |
– |
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)); |
1106 |
< |
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 |
|
} |