5 |
|
* Other contributors include John Vint |
6 |
|
*/ |
7 |
|
|
8 |
< |
import junit.framework.*; |
9 |
< |
import java.util.Arrays; |
8 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
9 |
> |
|
10 |
|
import java.util.ArrayList; |
11 |
+ |
import java.util.Arrays; |
12 |
|
import java.util.Collection; |
13 |
|
import java.util.Iterator; |
14 |
|
import java.util.List; |
15 |
|
import java.util.NoSuchElementException; |
16 |
|
import java.util.Queue; |
17 |
|
import java.util.concurrent.BlockingQueue; |
18 |
+ |
import java.util.concurrent.Callable; |
19 |
|
import java.util.concurrent.CountDownLatch; |
20 |
|
import java.util.concurrent.Executors; |
21 |
|
import java.util.concurrent.ExecutorService; |
22 |
|
import java.util.concurrent.LinkedTransferQueue; |
23 |
< |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
24 |
< |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
23 |
> |
|
24 |
> |
import junit.framework.Test; |
25 |
|
|
26 |
|
@SuppressWarnings({"unchecked", "rawtypes"}) |
27 |
|
public class LinkedTransferQueueTest extends JSR166TestCase { |
26 |
– |
|
28 |
|
public static class Generic extends BlockingQueueTest { |
29 |
|
protected BlockingQueue emptyCollection() { |
30 |
|
return new LinkedTransferQueue(); |
32 |
|
} |
33 |
|
|
34 |
|
public static void main(String[] args) { |
35 |
< |
junit.textui.TestRunner.run(suite()); |
35 |
> |
main(suite(), args); |
36 |
|
} |
37 |
|
|
38 |
|
public static Test suite() { |
39 |
+ |
class Implementation implements CollectionImplementation { |
40 |
+ |
public Class<?> klazz() { return LinkedTransferQueue.class; } |
41 |
+ |
public Collection emptyCollection() { return new LinkedTransferQueue(); } |
42 |
+ |
public Object makeElement(int i) { return i; } |
43 |
+ |
public boolean isConcurrent() { return true; } |
44 |
+ |
public boolean permitsNulls() { return false; } |
45 |
+ |
} |
46 |
|
return newTestSuite(LinkedTransferQueueTest.class, |
47 |
< |
new Generic().testSuite()); |
47 |
> |
new Generic().testSuite(), |
48 |
> |
CollectionTest.testSuite(new Implementation())); |
49 |
|
} |
50 |
|
|
51 |
|
/** |
86 |
|
*/ |
87 |
|
public void testConstructor4() { |
88 |
|
Integer[] ints = new Integer[SIZE]; |
89 |
< |
for (int i = 0; i < SIZE-1; ++i) |
89 |
> |
for (int i = 0; i < SIZE - 1; ++i) |
90 |
|
ints[i] = i; |
91 |
|
Collection<Integer> elements = Arrays.asList(ints); |
92 |
|
try { |
123 |
|
* remainingCapacity() always returns Integer.MAX_VALUE |
124 |
|
*/ |
125 |
|
public void testRemainingCapacity() { |
126 |
< |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
126 |
> |
BlockingQueue q = populatedQueue(SIZE); |
127 |
|
for (int i = 0; i < SIZE; ++i) { |
128 |
|
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
129 |
|
assertEquals(SIZE - i, q.size()); |
130 |
< |
q.remove(); |
130 |
> |
assertEquals(i, q.remove()); |
131 |
|
} |
132 |
|
for (int i = 0; i < SIZE; ++i) { |
133 |
|
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
134 |
|
assertEquals(i, q.size()); |
135 |
< |
q.add(i); |
135 |
> |
assertTrue(q.add(i)); |
136 |
|
} |
137 |
|
} |
138 |
|
|
140 |
|
* addAll(this) throws IllegalArgumentException |
141 |
|
*/ |
142 |
|
public void testAddAllSelf() { |
143 |
+ |
LinkedTransferQueue q = populatedQueue(SIZE); |
144 |
|
try { |
135 |
– |
LinkedTransferQueue q = populatedQueue(SIZE); |
145 |
|
q.addAll(q); |
146 |
|
shouldThrow(); |
147 |
|
} catch (IllegalArgumentException success) {} |
152 |
|
* NullPointerException after possibly adding some elements |
153 |
|
*/ |
154 |
|
public void testAddAll3() { |
155 |
+ |
LinkedTransferQueue q = new LinkedTransferQueue(); |
156 |
+ |
Integer[] ints = new Integer[SIZE]; |
157 |
+ |
for (int i = 0; i < SIZE - 1; ++i) |
158 |
+ |
ints[i] = i; |
159 |
|
try { |
147 |
– |
LinkedTransferQueue q = new LinkedTransferQueue(); |
148 |
– |
Integer[] ints = new Integer[SIZE]; |
149 |
– |
for (int i = 0; i < SIZE - 1; ++i) { |
150 |
– |
ints[i] = i; |
151 |
– |
} |
160 |
|
q.addAll(Arrays.asList(ints)); |
161 |
|
shouldThrow(); |
162 |
|
} catch (NullPointerException success) {} |
183 |
|
* all elements successfully put are contained |
184 |
|
*/ |
185 |
|
public void testPut() { |
186 |
< |
LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
186 |
> |
LinkedTransferQueue<Integer> q = new LinkedTransferQueue<>(); |
187 |
|
for (int i = 0; i < SIZE; ++i) { |
188 |
|
assertEquals(i, q.size()); |
189 |
|
q.put(i); |
209 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
210 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
211 |
|
public void realRun() throws InterruptedException { |
212 |
< |
for (int i = 0; i < SIZE; ++i) { |
205 |
< |
assertEquals(i, q.take()); |
206 |
< |
} |
212 |
> |
for (int i = 0; i < SIZE; i++) assertEquals(i, q.take()); |
213 |
|
|
214 |
|
Thread.currentThread().interrupt(); |
215 |
|
try { |
227 |
|
}}); |
228 |
|
|
229 |
|
await(pleaseInterrupt); |
230 |
< |
assertThreadStaysAlive(t); |
230 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); |
231 |
|
t.interrupt(); |
232 |
|
awaitTermination(t); |
233 |
|
} |
261 |
|
*/ |
262 |
|
public void testTimedPoll() throws InterruptedException { |
263 |
|
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 |
– |
} |
264 |
|
long startTime = System.nanoTime(); |
265 |
+ |
for (int i = 0; i < SIZE; ++i) |
266 |
+ |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
267 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
268 |
+ |
|
269 |
+ |
startTime = System.nanoTime(); |
270 |
|
assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
271 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
272 |
|
checkEmpty(q); |
278 |
|
*/ |
279 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
280 |
|
final BlockingQueue<Integer> q = populatedQueue(SIZE); |
281 |
< |
final CountDownLatch aboutToWait = new CountDownLatch(1); |
281 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
282 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
283 |
|
public void realRun() throws InterruptedException { |
284 |
< |
for (int i = 0; i < SIZE; ++i) { |
279 |
< |
long t0 = System.nanoTime(); |
284 |
> |
for (int i = 0; i < SIZE; i++) |
285 |
|
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
286 |
< |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
287 |
< |
} |
288 |
< |
long t0 = System.nanoTime(); |
289 |
< |
aboutToWait.countDown(); |
286 |
> |
|
287 |
> |
Thread.currentThread().interrupt(); |
288 |
> |
try { |
289 |
> |
q.poll(randomTimeout(), randomTimeUnit()); |
290 |
> |
shouldThrow(); |
291 |
> |
} catch (InterruptedException success) {} |
292 |
> |
assertFalse(Thread.interrupted()); |
293 |
> |
|
294 |
> |
pleaseInterrupt.countDown(); |
295 |
|
try { |
296 |
< |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
296 |
> |
q.poll(LONGER_DELAY_MS, MILLISECONDS); |
297 |
|
shouldThrow(); |
298 |
< |
} catch (InterruptedException success) { |
299 |
< |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
290 |
< |
} |
298 |
> |
} catch (InterruptedException success) {} |
299 |
> |
assertFalse(Thread.interrupted()); |
300 |
|
}}); |
301 |
|
|
302 |
< |
aboutToWait.await(); |
303 |
< |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
302 |
> |
await(pleaseInterrupt); |
303 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
304 |
|
t.interrupt(); |
305 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
305 |
> |
awaitTermination(t); |
306 |
|
checkEmpty(q); |
307 |
|
} |
308 |
|
|
315 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
316 |
|
public void realRun() throws InterruptedException { |
317 |
|
Thread.currentThread().interrupt(); |
318 |
< |
for (int i = 0; i < SIZE; ++i) { |
319 |
< |
long t0 = System.nanoTime(); |
311 |
< |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
312 |
< |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
313 |
< |
} |
318 |
> |
for (int i = 0; i < SIZE; ++i) |
319 |
> |
assertEquals(i, (int) q.poll(randomTimeout(), randomTimeUnit())); |
320 |
|
try { |
321 |
< |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
321 |
> |
q.poll(randomTimeout(), randomTimeUnit()); |
322 |
|
shouldThrow(); |
323 |
|
} catch (InterruptedException success) {} |
324 |
+ |
assertFalse(Thread.interrupted()); |
325 |
|
}}); |
326 |
|
|
327 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
327 |
> |
awaitTermination(t); |
328 |
|
checkEmpty(q); |
329 |
|
} |
330 |
|
|
420 |
|
*/ |
421 |
|
public void testContainsAll() { |
422 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
423 |
< |
LinkedTransferQueue<Integer> p = new LinkedTransferQueue<Integer>(); |
423 |
> |
LinkedTransferQueue<Integer> p = new LinkedTransferQueue<>(); |
424 |
|
for (int i = 0; i < SIZE; ++i) { |
425 |
|
assertTrue(q.containsAll(p)); |
426 |
|
assertFalse(p.containsAll(q)); |
470 |
|
*/ |
471 |
|
public void testToArray() { |
472 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
473 |
< |
Object[] o = q.toArray(); |
474 |
< |
for (int i = 0; i < o.length; i++) { |
475 |
< |
assertSame(o[i], q.poll()); |
476 |
< |
} |
473 |
> |
Object[] a = q.toArray(); |
474 |
> |
assertSame(Object[].class, a.getClass()); |
475 |
> |
for (Object o : a) |
476 |
> |
assertSame(o, q.poll()); |
477 |
> |
assertTrue(q.isEmpty()); |
478 |
|
} |
479 |
|
|
480 |
|
/** |
485 |
|
Integer[] ints = new Integer[SIZE]; |
486 |
|
Integer[] array = q.toArray(ints); |
487 |
|
assertSame(ints, array); |
488 |
< |
for (int i = 0; i < ints.length; i++) { |
489 |
< |
assertSame(ints[i], q.poll()); |
490 |
< |
} |
488 |
> |
for (Integer o : ints) |
489 |
> |
assertSame(o, q.poll()); |
490 |
> |
assertTrue(q.isEmpty()); |
491 |
|
} |
492 |
|
|
493 |
|
/** |
507 |
|
public void testIterator() throws InterruptedException { |
508 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
509 |
|
Iterator it = q.iterator(); |
510 |
< |
int i = 0; |
511 |
< |
while (it.hasNext()) { |
512 |
< |
assertEquals(it.next(), i++); |
513 |
< |
} |
510 |
> |
int i; |
511 |
> |
for (i = 0; it.hasNext(); i++) |
512 |
> |
assertTrue(q.contains(it.next())); |
513 |
> |
assertEquals(i, SIZE); |
514 |
> |
assertIteratorExhausted(it); |
515 |
> |
|
516 |
> |
it = q.iterator(); |
517 |
> |
for (i = 0; it.hasNext(); i++) |
518 |
> |
assertEquals(it.next(), q.take()); |
519 |
|
assertEquals(i, SIZE); |
520 |
+ |
assertIteratorExhausted(it); |
521 |
+ |
} |
522 |
+ |
|
523 |
+ |
/** |
524 |
+ |
* iterator of empty collection has no elements |
525 |
+ |
*/ |
526 |
+ |
public void testEmptyIterator() { |
527 |
+ |
assertIteratorExhausted(new LinkedTransferQueue().iterator()); |
528 |
|
} |
529 |
|
|
530 |
|
/** |
550 |
|
* iterator ordering is FIFO |
551 |
|
*/ |
552 |
|
public void testIteratorOrdering() { |
553 |
< |
final LinkedTransferQueue<Integer> q |
533 |
< |
= new LinkedTransferQueue<Integer>(); |
553 |
> |
final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<>(); |
554 |
|
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
555 |
|
q.add(one); |
556 |
|
q.add(two); |
595 |
|
public void testOfferInExecutor() { |
596 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
597 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
598 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
599 |
< |
|
580 |
< |
executor.execute(new CheckedRunnable() { |
581 |
< |
public void realRun() throws InterruptedException { |
582 |
< |
threadsStarted.await(); |
583 |
< |
assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS)); |
584 |
< |
}}); |
598 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
599 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
600 |
|
|
601 |
< |
executor.execute(new CheckedRunnable() { |
602 |
< |
public void realRun() throws InterruptedException { |
603 |
< |
threadsStarted.await(); |
604 |
< |
assertSame(one, q.take()); |
605 |
< |
checkEmpty(q); |
606 |
< |
}}); |
601 |
> |
executor.execute(new CheckedRunnable() { |
602 |
> |
public void realRun() throws InterruptedException { |
603 |
> |
threadsStarted.await(); |
604 |
> |
long startTime = System.nanoTime(); |
605 |
> |
assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS)); |
606 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
607 |
> |
}}); |
608 |
|
|
609 |
< |
joinPool(executor); |
609 |
> |
executor.execute(new CheckedRunnable() { |
610 |
> |
public void realRun() throws InterruptedException { |
611 |
> |
threadsStarted.await(); |
612 |
> |
assertSame(one, q.take()); |
613 |
> |
checkEmpty(q); |
614 |
> |
}}); |
615 |
> |
} |
616 |
|
} |
617 |
|
|
618 |
|
/** |
621 |
|
public void testPollInExecutor() { |
622 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
623 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
624 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
625 |
< |
|
604 |
< |
executor.execute(new CheckedRunnable() { |
605 |
< |
public void realRun() throws InterruptedException { |
606 |
< |
assertNull(q.poll()); |
607 |
< |
threadsStarted.await(); |
608 |
< |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
609 |
< |
checkEmpty(q); |
610 |
< |
}}); |
624 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
625 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
626 |
|
|
627 |
< |
executor.execute(new CheckedRunnable() { |
628 |
< |
public void realRun() throws InterruptedException { |
629 |
< |
threadsStarted.await(); |
630 |
< |
q.put(one); |
631 |
< |
}}); |
627 |
> |
executor.execute(new CheckedRunnable() { |
628 |
> |
public void realRun() throws InterruptedException { |
629 |
> |
assertNull(q.poll()); |
630 |
> |
threadsStarted.await(); |
631 |
> |
long startTime = System.nanoTime(); |
632 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
633 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
634 |
> |
checkEmpty(q); |
635 |
> |
}}); |
636 |
|
|
637 |
< |
joinPool(executor); |
637 |
> |
executor.execute(new CheckedRunnable() { |
638 |
> |
public void realRun() throws InterruptedException { |
639 |
> |
threadsStarted.await(); |
640 |
> |
q.put(one); |
641 |
> |
}}); |
642 |
> |
} |
643 |
|
} |
644 |
|
|
645 |
|
/** |
646 |
< |
* A deserialized serialized queue has same elements in same order |
646 |
> |
* A deserialized/reserialized queue has same elements in same order |
647 |
|
*/ |
648 |
|
public void testSerialization() throws Exception { |
649 |
|
Queue x = populatedQueue(SIZE); |
650 |
|
Queue y = serialClone(x); |
651 |
|
|
652 |
< |
assertTrue(x != y); |
652 |
> |
assertNotSame(y, x); |
653 |
|
assertEquals(x.size(), y.size()); |
654 |
|
assertEquals(x.toString(), y.toString()); |
655 |
|
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
700 |
|
assertTrue(l.size() >= SIZE); |
701 |
|
for (int i = 0; i < SIZE; ++i) |
702 |
|
assertEquals(i, l.get(i)); |
703 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
703 |
> |
awaitTermination(t); |
704 |
|
assertTrue(q.size() + l.size() >= SIZE); |
705 |
|
} |
706 |
|
|
720 |
|
assertEquals(SIZE - k, q.size()); |
721 |
|
for (int j = 0; j < k; ++j) |
722 |
|
assertEquals(j, l.get(j)); |
723 |
< |
while (q.poll() != null) |
700 |
< |
; |
723 |
> |
do {} while (q.poll() != null); |
724 |
|
} |
725 |
|
} |
726 |
|
|
737 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
738 |
|
public void realRun() throws InterruptedException { |
739 |
|
threadStarted.countDown(); |
740 |
+ |
long startTime = System.nanoTime(); |
741 |
|
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
742 |
|
assertEquals(0, q.getWaitingConsumerCount()); |
743 |
|
assertFalse(q.hasWaitingConsumer()); |
744 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
745 |
|
}}); |
746 |
|
|
747 |
|
threadStarted.await(); |
748 |
< |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
749 |
< |
assertEquals(1, q.getWaitingConsumerCount()); |
750 |
< |
assertTrue(q.hasWaitingConsumer()); |
748 |
> |
Callable<Boolean> oneConsumer |
749 |
> |
= new Callable<Boolean>() { public Boolean call() { |
750 |
> |
return q.hasWaitingConsumer() |
751 |
> |
&& q.getWaitingConsumerCount() == 1; }}; |
752 |
> |
waitForThreadToEnterWaitState(t, oneConsumer); |
753 |
|
|
754 |
|
assertTrue(q.offer(one)); |
755 |
|
assertEquals(0, q.getWaitingConsumerCount()); |
756 |
|
assertFalse(q.hasWaitingConsumer()); |
757 |
|
|
758 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
758 |
> |
awaitTermination(t); |
759 |
|
} |
760 |
|
|
761 |
|
/** |
770 |
|
} |
771 |
|
|
772 |
|
/** |
773 |
< |
* transfer waits until a poll occurs. The transfered element |
774 |
< |
* is returned by this associated poll. |
773 |
> |
* transfer waits until a poll occurs. The transferred element |
774 |
> |
* is returned by the associated poll. |
775 |
|
*/ |
776 |
|
public void testTransfer2() throws InterruptedException { |
777 |
< |
final LinkedTransferQueue<Integer> q |
751 |
< |
= new LinkedTransferQueue<Integer>(); |
777 |
> |
final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<>(); |
778 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
779 |
|
|
780 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
785 |
|
}}); |
786 |
|
|
787 |
|
threadStarted.await(); |
788 |
< |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
789 |
< |
assertEquals(1, q.size()); |
788 |
> |
Callable<Boolean> oneElement |
789 |
> |
= new Callable<Boolean>() { public Boolean call() { |
790 |
> |
return !q.isEmpty() && q.size() == 1; }}; |
791 |
> |
waitForThreadToEnterWaitState(t, oneElement); |
792 |
> |
|
793 |
|
assertSame(five, q.poll()); |
794 |
|
checkEmpty(q); |
795 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
795 |
> |
awaitTermination(t); |
796 |
|
} |
797 |
|
|
798 |
|
/** |
799 |
|
* transfer waits until a poll occurs, and then transfers in fifo order |
800 |
|
*/ |
801 |
|
public void testTransfer3() throws InterruptedException { |
802 |
< |
final LinkedTransferQueue<Integer> q |
774 |
< |
= new LinkedTransferQueue<Integer>(); |
802 |
> |
final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<>(); |
803 |
|
|
804 |
|
Thread first = newStartedThread(new CheckedRunnable() { |
805 |
|
public void realRun() throws InterruptedException { |
806 |
|
q.transfer(four); |
807 |
< |
assertTrue(!q.contains(four)); |
807 |
> |
assertFalse(q.contains(four)); |
808 |
|
assertEquals(1, q.size()); |
809 |
|
}}); |
810 |
|
|
847 |
|
assertEquals(1, q.size()); |
848 |
|
assertTrue(q.offer(three)); |
849 |
|
assertSame(four, q.poll()); |
850 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
850 |
> |
awaitTermination(t); |
851 |
|
} |
852 |
|
|
853 |
|
/** |
854 |
< |
* transfer waits until a take occurs. The transfered element |
855 |
< |
* is returned by this associated take. |
854 |
> |
* transfer waits until a take occurs. The transferred element |
855 |
> |
* is returned by the associated take. |
856 |
|
*/ |
857 |
|
public void testTransfer5() throws InterruptedException { |
858 |
< |
final LinkedTransferQueue<Integer> q |
831 |
< |
= new LinkedTransferQueue<Integer>(); |
858 |
> |
final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<>(); |
859 |
|
|
860 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
861 |
|
public void realRun() throws InterruptedException { |
869 |
|
assertEquals(1, q.size()); |
870 |
|
assertSame(four, q.take()); |
871 |
|
checkEmpty(q); |
872 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
872 |
> |
awaitTermination(t); |
873 |
|
} |
874 |
|
|
875 |
|
/** |
876 |
|
* tryTransfer(null) throws NullPointerException |
877 |
|
*/ |
878 |
|
public void testTryTransfer1() { |
879 |
+ |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
880 |
|
try { |
853 |
– |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
881 |
|
q.tryTransfer(null); |
882 |
|
shouldThrow(); |
883 |
|
} catch (NullPointerException success) {} |
911 |
|
assertTrue(q.tryTransfer(hotPotato)); |
912 |
|
}}); |
913 |
|
|
914 |
< |
assertSame(hotPotato, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
914 |
> |
long startTime = System.nanoTime(); |
915 |
> |
assertSame(hotPotato, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
916 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
917 |
|
checkEmpty(q); |
918 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
918 |
> |
awaitTermination(t); |
919 |
|
} |
920 |
|
|
921 |
|
/** |
937 |
|
|
938 |
|
assertSame(q.take(), hotPotato); |
939 |
|
checkEmpty(q); |
940 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
940 |
> |
awaitTermination(t); |
941 |
|
} |
942 |
|
|
943 |
|
/** |
952 |
|
public void realRun() throws InterruptedException { |
953 |
|
Thread.currentThread().interrupt(); |
954 |
|
try { |
955 |
< |
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
955 |
> |
q.tryTransfer(new Object(), randomTimeout(), randomTimeUnit()); |
956 |
|
shouldThrow(); |
957 |
|
} catch (InterruptedException success) {} |
958 |
|
assertFalse(Thread.interrupted()); |
959 |
|
|
960 |
|
pleaseInterrupt.countDown(); |
961 |
|
try { |
962 |
< |
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
962 |
> |
q.tryTransfer(new Object(), LONGER_DELAY_MS, MILLISECONDS); |
963 |
|
shouldThrow(); |
964 |
|
} catch (InterruptedException success) {} |
965 |
|
assertFalse(Thread.interrupted()); |
966 |
|
}}); |
967 |
|
|
968 |
|
await(pleaseInterrupt); |
969 |
< |
assertThreadStaysAlive(t); |
969 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
970 |
|
t.interrupt(); |
971 |
|
awaitTermination(t); |
972 |
|
checkEmpty(q); |
980 |
|
|
981 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
982 |
|
public void realRun() throws InterruptedException { |
983 |
< |
long t0 = System.nanoTime(); |
983 |
> |
long startTime = System.nanoTime(); |
984 |
|
assertFalse(q.tryTransfer(new Object(), |
985 |
|
timeoutMillis(), MILLISECONDS)); |
986 |
< |
assertTrue(millisElapsedSince(t0) >= timeoutMillis()); |
986 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
987 |
|
checkEmpty(q); |
988 |
|
}}); |
989 |
|
|
1001 |
|
|
1002 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1003 |
|
public void realRun() throws InterruptedException { |
1004 |
< |
assertTrue(q.tryTransfer(five, MEDIUM_DELAY_MS, MILLISECONDS)); |
1004 |
> |
long startTime = System.nanoTime(); |
1005 |
> |
assertTrue(q.tryTransfer(five, LONG_DELAY_MS, MILLISECONDS)); |
1006 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1007 |
|
checkEmpty(q); |
1008 |
|
}}); |
1009 |
|
|
1013 |
|
assertSame(four, q.poll()); |
1014 |
|
assertSame(five, q.poll()); |
1015 |
|
checkEmpty(q); |
1016 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
1016 |
> |
awaitTermination(t); |
1017 |
|
} |
1018 |
|
|
1019 |
|
/** |
1024 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1025 |
|
assertTrue(q.offer(four)); |
1026 |
|
assertEquals(1, q.size()); |
1027 |
< |
long t0 = System.nanoTime(); |
1027 |
> |
long startTime = System.nanoTime(); |
1028 |
|
assertFalse(q.tryTransfer(five, timeoutMillis(), MILLISECONDS)); |
1029 |
< |
assertTrue(millisElapsedSince(t0) >= timeoutMillis()); |
1029 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
1030 |
|
assertEquals(1, q.size()); |
1031 |
|
assertSame(four, q.poll()); |
1032 |
|
assertNull(q.poll()); |
1034 |
|
} |
1035 |
|
|
1036 |
|
private LinkedTransferQueue<Integer> populatedQueue(int n) { |
1037 |
< |
LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
1037 |
> |
LinkedTransferQueue<Integer> q = new LinkedTransferQueue<>(); |
1038 |
|
checkEmpty(q); |
1039 |
|
for (int i = 0; i < n; i++) { |
1040 |
|
assertEquals(i, q.size()); |
1044 |
|
assertFalse(q.isEmpty()); |
1045 |
|
return q; |
1046 |
|
} |
1047 |
+ |
|
1048 |
+ |
/** |
1049 |
+ |
* remove(null), contains(null) always return false |
1050 |
+ |
*/ |
1051 |
+ |
public void testNeverContainsNull() { |
1052 |
+ |
Collection<?>[] qs = { |
1053 |
+ |
new LinkedTransferQueue<Object>(), |
1054 |
+ |
populatedQueue(2), |
1055 |
+ |
}; |
1056 |
+ |
|
1057 |
+ |
for (Collection<?> q : qs) { |
1058 |
+ |
assertFalse(q.contains(null)); |
1059 |
+ |
assertFalse(q.remove(null)); |
1060 |
+ |
} |
1061 |
+ |
} |
1062 |
|
} |