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 |
|
/** |
274 |
|
} |
275 |
|
|
276 |
|
/** |
277 |
< |
* take blocks interruptibly when empty |
283 |
< |
*/ |
284 |
< |
public void testTakeFromEmpty() throws InterruptedException { |
285 |
< |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
286 |
< |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
287 |
< |
void realRun() throws InterruptedException { |
288 |
< |
q.take(); |
289 |
< |
}}); |
290 |
< |
Thread.sleep(SHORT_DELAY_MS); |
291 |
< |
t.interrupt(); |
292 |
< |
t.join(); |
293 |
< |
} |
294 |
< |
|
295 |
< |
/** |
296 |
< |
* 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); |
281 |
< |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
282 |
< |
void realRun() throws InterruptedException { |
281 |
> |
Thread t = new Thread(new CheckedRunnable() { |
282 |
> |
public void realRun() throws InterruptedException { |
283 |
|
for (int i = 0; i < SIZE; ++i) { |
284 |
< |
threadAssertEquals(i, (int) q.take()); |
284 |
> |
assertEquals(i, (int) q.take()); |
285 |
|
} |
286 |
< |
q.take(); |
286 |
> |
try { |
287 |
> |
q.take(); |
288 |
> |
shouldThrow(); |
289 |
> |
} catch (InterruptedException success) {} |
290 |
|
}}); |
291 |
< |
Thread.sleep(SMALL_DELAY_MS); |
291 |
> |
|
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); |
346 |
< |
assertTrue(millisElapsed < SMALL_DELAY_MS); |
331 |
> |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
332 |
|
} |
333 |
|
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
334 |
|
checkEmpty(q); |
340 |
|
*/ |
341 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
342 |
|
final LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
343 |
< |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
344 |
< |
void realRun() throws InterruptedException { |
343 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
344 |
> |
public void realRun() throws InterruptedException { |
345 |
|
for (int i = 0; i < SIZE; ++i) { |
346 |
|
long t0 = System.nanoTime(); |
347 |
< |
threadAssertEquals(i, (int) q.poll(LONG_DELAY_MS, |
348 |
< |
MILLISECONDS)); |
364 |
< |
long millisElapsed = (System.nanoTime() - t0)/(1024 * 1024); |
365 |
< |
assertTrue(millisElapsed < SMALL_DELAY_MS); |
347 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
348 |
> |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
349 |
|
} |
350 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
350 |
> |
try { |
351 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
352 |
> |
shouldThrow(); |
353 |
> |
} catch (InterruptedException success) {} |
354 |
|
}}); |
369 |
– |
Thread.sleep(SMALL_DELAY_MS); |
370 |
– |
t.interrupt(); |
371 |
– |
t.join(); |
372 |
– |
checkEmpty(q); |
373 |
– |
} |
355 |
|
|
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 |
– |
}}); |
356 |
|
Thread.sleep(SMALL_DELAY_MS); |
388 |
– |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
357 |
|
t.interrupt(); |
358 |
< |
t.join(); |
358 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
359 |
> |
checkEmpty(q); |
360 |
|
} |
361 |
|
|
362 |
|
/** |
386 |
|
try { |
387 |
|
q.element(); |
388 |
|
shouldThrow(); |
389 |
< |
} catch (NoSuchElementException success) { |
421 |
< |
} |
389 |
> |
} catch (NoSuchElementException success) {} |
390 |
|
checkEmpty(q); |
391 |
|
} |
392 |
|
|
401 |
|
try { |
402 |
|
q.remove(); |
403 |
|
shouldThrow(); |
404 |
< |
} catch (NoSuchElementException success) { |
437 |
< |
} |
404 |
> |
} catch (NoSuchElementException success) {} |
405 |
|
checkEmpty(q); |
406 |
|
} |
407 |
|
|
430 |
|
assertTrue(q.remove(one)); |
431 |
|
assertTrue(q.remove(two)); |
432 |
|
assertTrue(q.add(three)); |
433 |
< |
assertTrue(q.take() == three); |
433 |
> |
assertSame(q.take(), three); |
434 |
|
} |
435 |
|
|
436 |
|
/** |
512 |
|
} |
513 |
|
|
514 |
|
/** |
515 |
< |
* toArray contains all elements |
515 |
> |
* toArray() contains all elements |
516 |
|
*/ |
517 |
|
public void testToArray() throws InterruptedException { |
518 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
538 |
|
* toArray(null) throws NullPointerException |
539 |
|
*/ |
540 |
|
public void testToArray_BadArg() { |
541 |
+ |
LinkedTransferQueue q = populatedQueue(SIZE); |
542 |
|
try { |
575 |
– |
LinkedTransferQueue q = populatedQueue(SIZE); |
543 |
|
Object o[] = q.toArray(null); |
544 |
|
shouldThrow(); |
545 |
< |
} catch (NullPointerException success) { |
579 |
< |
} |
545 |
> |
} catch (NullPointerException success) {} |
546 |
|
} |
547 |
|
|
548 |
|
/** |
549 |
< |
* toArray with incompatible array type throws CCE |
549 |
> |
* toArray(incompatible array type) throws CCE |
550 |
|
*/ |
551 |
|
public void testToArray1_BadArg() { |
552 |
+ |
LinkedTransferQueue q = populatedQueue(SIZE); |
553 |
|
try { |
587 |
– |
LinkedTransferQueue q = populatedQueue(SIZE); |
554 |
|
Object o[] = q.toArray(new String[10]); |
555 |
|
shouldThrow(); |
556 |
< |
} catch (ArrayStoreException success) { |
591 |
< |
} |
556 |
> |
} catch (ArrayStoreException success) {} |
557 |
|
} |
558 |
|
|
559 |
|
/** |
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 |
|
|
642 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
643 |
|
|
644 |
|
executor.execute(new CheckedRunnable() { |
645 |
< |
void realRun() { |
646 |
< |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, |
682 |
< |
MILLISECONDS)); |
645 |
> |
public void realRun() { |
646 |
> |
assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
647 |
|
}}); |
648 |
|
|
649 |
|
executor.execute(new CheckedRunnable() { |
650 |
< |
void realRun() throws InterruptedException { |
650 |
> |
public void realRun() throws InterruptedException { |
651 |
|
Thread.sleep(SMALL_DELAY_MS); |
652 |
< |
threadAssertEquals(one, q.take()); |
652 |
> |
assertSame(one, q.take()); |
653 |
|
}}); |
654 |
|
|
655 |
|
joinPool(executor); |
656 |
|
} |
657 |
|
|
658 |
|
/** |
659 |
< |
* poll retrieves elements across Executor threads |
659 |
> |
* timed poll retrieves elements across Executor threads |
660 |
|
*/ |
661 |
|
public void testPollInExecutor() { |
662 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
663 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
664 |
|
|
665 |
|
executor.execute(new CheckedRunnable() { |
666 |
< |
void realRun() throws InterruptedException { |
667 |
< |
threadAssertNull(q.poll()); |
668 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, |
669 |
< |
MILLISECONDS)); |
706 |
< |
threadAssertTrue(q.isEmpty()); |
666 |
> |
public void realRun() throws InterruptedException { |
667 |
> |
assertNull(q.poll()); |
668 |
> |
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
669 |
> |
assertTrue(q.isEmpty()); |
670 |
|
}}); |
671 |
|
|
672 |
|
executor.execute(new CheckedRunnable() { |
673 |
< |
void realRun() throws InterruptedException { |
673 |
> |
public void realRun() throws InterruptedException { |
674 |
|
Thread.sleep(SMALL_DELAY_MS); |
675 |
|
q.put(one); |
676 |
|
}}); |
710 |
|
try { |
711 |
|
q.drainTo(null); |
712 |
|
shouldThrow(); |
713 |
< |
} catch (NullPointerException success) { |
751 |
< |
} |
713 |
> |
} catch (NullPointerException success) {} |
714 |
|
} |
715 |
|
|
716 |
|
/** |
721 |
|
try { |
722 |
|
q.drainTo(q); |
723 |
|
shouldThrow(); |
724 |
< |
} catch (IllegalArgumentException success) { |
763 |
< |
} |
724 |
> |
} catch (IllegalArgumentException success) {} |
725 |
|
} |
726 |
|
|
727 |
|
/** |
751 |
|
} |
752 |
|
|
753 |
|
/** |
754 |
< |
* drainTo empties full queue, unblocking a waiting put. |
754 |
> |
* drainTo(c) empties full queue, unblocking a waiting put. |
755 |
|
*/ |
756 |
|
public void testDrainToWithActivePut() throws InterruptedException { |
757 |
|
final LinkedTransferQueue q = populatedQueue(SIZE); |
758 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
759 |
< |
void realRun() { |
759 |
> |
public void realRun() { |
760 |
|
q.put(SIZE + 1); |
761 |
|
}}); |
762 |
|
ArrayList l = new ArrayList(); |
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 |
|
|
777 |
|
try { |
778 |
|
q.drainTo(null, SIZE); |
779 |
|
shouldThrow(); |
780 |
< |
} catch (NullPointerException success) { |
820 |
< |
} |
780 |
> |
} catch (NullPointerException success) {} |
781 |
|
} |
782 |
|
|
783 |
|
/** |
788 |
|
try { |
789 |
|
q.drainTo(q, SIZE); |
790 |
|
shouldThrow(); |
791 |
< |
} catch (IllegalArgumentException success) { |
832 |
< |
} |
791 |
> |
} catch (IllegalArgumentException success) {} |
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(); |
814 |
|
} |
815 |
|
|
816 |
|
/** |
817 |
< |
* poll and take decrement the waiting consumer count |
817 |
> |
* timed poll() or take() increments the waiting consumer count; |
818 |
> |
* offer(e) decrements the waiting consumer count |
819 |
|
*/ |
820 |
|
public void testWaitingConsumer() throws InterruptedException { |
821 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
822 |
< |
final ConsumerObserver waiting = new ConsumerObserver(); |
822 |
> |
assertEquals(q.getWaitingConsumerCount(), 0); |
823 |
> |
assertFalse(q.hasWaitingConsumer()); |
824 |
|
|
825 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
826 |
< |
void realRun() throws InterruptedException { |
826 |
> |
public void realRun() throws InterruptedException { |
827 |
|
Thread.sleep(SMALL_DELAY_MS); |
828 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
829 |
< |
waiting.setWaitingConsumer(q.getWaitingConsumerCount()); |
830 |
< |
threadAssertTrue(q.offer(new Object())); |
828 |
> |
assertTrue(q.hasWaitingConsumer()); |
829 |
> |
assertEquals(q.getWaitingConsumerCount(), 1); |
830 |
> |
assertTrue(q.offer(one)); |
831 |
> |
assertFalse(q.hasWaitingConsumer()); |
832 |
> |
assertEquals(q.getWaitingConsumerCount(), 0); |
833 |
|
}}); |
834 |
|
|
835 |
< |
assertTrue(q.poll(LONG_DELAY_MS, MILLISECONDS) != null); |
836 |
< |
assertTrue(q.getWaitingConsumerCount() |
837 |
< |
< waiting.getWaitingConsumers()); |
838 |
< |
t.join(); |
835 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
836 |
> |
assertEquals(q.getWaitingConsumerCount(), 0); |
837 |
> |
assertFalse(q.hasWaitingConsumer()); |
838 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
839 |
|
} |
840 |
|
|
841 |
|
/** |
846 |
|
LinkedTransferQueue q = new LinkedTransferQueue(); |
847 |
|
q.transfer(null); |
848 |
|
shouldThrow(); |
849 |
< |
} catch (NullPointerException ex) { |
887 |
< |
} |
849 |
> |
} catch (NullPointerException success) {} |
850 |
|
} |
851 |
|
|
852 |
|
/** |
858 |
|
= new LinkedTransferQueue<Integer>(); |
859 |
|
|
860 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
861 |
< |
void realRun() throws InterruptedException { |
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 |
|
/** |
878 |
|
= new LinkedTransferQueue<Integer>(); |
879 |
|
|
880 |
|
Thread first = newStartedThread(new CheckedRunnable() { |
881 |
< |
void realRun() throws InterruptedException { |
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( |
889 |
|
new CheckedInterruptedRunnable() { |
890 |
< |
void realRun() throws InterruptedException { |
890 |
> |
public void realRun() throws InterruptedException { |
891 |
|
while (q.size() == 0) |
892 |
|
Thread.yield(); |
893 |
|
q.transfer(SIZE); |
913 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
914 |
|
|
915 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
916 |
< |
void realRun() throws InterruptedException { |
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 |
|
/** |
934 |
|
= new LinkedTransferQueue<Integer>(); |
935 |
|
|
936 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
937 |
< |
void realRun() throws InterruptedException { |
937 |
> |
public void realRun() throws InterruptedException { |
938 |
|
q.transfer(SIZE); |
939 |
|
checkEmpty(q); |
940 |
|
}}); |
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 |
|
/** |
953 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
954 |
|
q.tryTransfer(null); |
955 |
|
shouldThrow(); |
956 |
< |
} catch (NullPointerException ex) { |
995 |
< |
} |
956 |
> |
} catch (NullPointerException success) {} |
957 |
|
} |
958 |
|
|
959 |
|
/** |
976 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
977 |
|
|
978 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
979 |
< |
void realRun() { |
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 |
< |
assertTrue(q.poll(MEDIUM_DELAY_MS, MILLISECONDS) == hotPotato); |
988 |
> |
assertSame(hotPotato, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
989 |
|
checkEmpty(q); |
990 |
< |
t.join(); |
990 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
991 |
|
} |
992 |
|
|
993 |
|
/** |
999 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1000 |
|
|
1001 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1002 |
< |
void realRun() { |
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 |
< |
assertTrue(q.take() == hotPotato); |
1011 |
> |
assertSame(q.take(), hotPotato); |
1012 |
|
checkEmpty(q); |
1013 |
< |
t.join(); |
1013 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
1014 |
|
} |
1015 |
|
|
1016 |
|
/** |
1021 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1022 |
|
|
1023 |
|
Thread toInterrupt = newStartedThread(new CheckedInterruptedRunnable() { |
1024 |
< |
void realRun() throws InterruptedException { |
1024 |
> |
public void realRun() throws InterruptedException { |
1025 |
|
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
1026 |
|
}}); |
1027 |
|
|
1037 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1038 |
|
|
1039 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1040 |
< |
void realRun() throws InterruptedException { |
1041 |
< |
threadAssertFalse |
1042 |
< |
(q.tryTransfer(new Object(), |
1043 |
< |
SHORT_DELAY_MS, MILLISECONDS)); |
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 |
|
|
1085 |
– |
Thread.sleep(SMALL_DELAY_MS); |
1047 |
|
checkEmpty(q); |
1048 |
< |
t.join(); |
1048 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
1049 |
> |
checkEmpty(q); |
1050 |
|
} |
1051 |
|
|
1052 |
|
/** |
1058 |
|
assertTrue(q.offer(four)); |
1059 |
|
|
1060 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1061 |
< |
void realRun() throws InterruptedException { |
1062 |
< |
threadAssertTrue(q.tryTransfer(five, |
1063 |
< |
MEDIUM_DELAY_MS, MILLISECONDS)); |
1102 |
< |
threadAssertTrue(q.isEmpty()); |
1061 |
> |
public void realRun() throws InterruptedException { |
1062 |
> |
assertTrue(q.tryTransfer(five, MEDIUM_DELAY_MS, MILLISECONDS)); |
1063 |
> |
assertTrue(q.isEmpty()); |
1064 |
|
}}); |
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()); |
1124 |
< |
checkEmpty(q); |
1084 |
> |
assertSame(four, q.poll()); |
1085 |
|
assertNull(q.poll()); |
1086 |
+ |
checkEmpty(q); |
1087 |
|
} |
1088 |
|
|
1089 |
|
private LinkedTransferQueue<Integer> populatedQueue(int n) { |
1097 |
|
assertFalse(q.isEmpty()); |
1098 |
|
return q; |
1099 |
|
} |
1139 |
– |
|
1140 |
– |
private static class ConsumerObserver { |
1141 |
– |
|
1142 |
– |
private int waitingConsumers; |
1143 |
– |
|
1144 |
– |
private ConsumerObserver() { |
1145 |
– |
} |
1146 |
– |
|
1147 |
– |
private void setWaitingConsumer(int i) { |
1148 |
– |
this.waitingConsumers = i; |
1149 |
– |
} |
1150 |
– |
|
1151 |
– |
private int getWaitingConsumers() { |
1152 |
– |
return waitingConsumers; |
1153 |
– |
} |
1154 |
– |
} |
1100 |
|
} |