1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include John Vint |
6 |
|
*/ |
7 |
|
|
8 |
< |
import java.io.BufferedInputStream; |
9 |
< |
import java.io.BufferedOutputStream; |
10 |
< |
import java.io.ByteArrayInputStream; |
11 |
< |
import java.io.ByteArrayOutputStream; |
12 |
< |
import java.io.ObjectInputStream; |
13 |
< |
import java.io.ObjectOutputStream; |
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.concurrent.*; |
17 |
< |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
18 |
< |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
16 |
> |
import java.util.Queue; |
17 |
> |
import java.util.concurrent.BlockingQueue; |
18 |
> |
import java.util.concurrent.CountDownLatch; |
19 |
> |
import java.util.concurrent.Executors; |
20 |
> |
import java.util.concurrent.ExecutorService; |
21 |
> |
import java.util.concurrent.LinkedTransferQueue; |
22 |
> |
|
23 |
|
import junit.framework.Test; |
23 |
– |
import junit.framework.TestSuite; |
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() { |
39 |
|
} |
40 |
|
|
41 |
|
public static void main(String[] args) { |
42 |
< |
junit.textui.TestRunner.run(suite()); |
42 |
> |
main(suite(), args); |
43 |
|
} |
44 |
|
|
45 |
|
public static Test suite() { |
46 |
|
return newTestSuite(LinkedTransferQueueTest.class, |
47 |
< |
new Generic().testSuite()); |
48 |
< |
} |
42 |
< |
|
43 |
< |
void checkEmpty(BlockingQueue q) { |
44 |
< |
try { |
45 |
< |
assertTrue(q.isEmpty()); |
46 |
< |
assertEquals(0, q.size()); |
47 |
< |
assertNull(q.peek()); |
48 |
< |
assertNull(q.poll()); |
49 |
< |
assertNull(q.poll(0, MILLISECONDS)); |
50 |
< |
assertEquals(q.toString(), "[]"); |
51 |
< |
assertTrue(Arrays.equals(q.toArray(), new Object[0])); |
52 |
< |
assertFalse(q.iterator().hasNext()); |
53 |
< |
try { |
54 |
< |
q.element(); |
55 |
< |
shouldThrow(); |
56 |
< |
} catch (NoSuchElementException success) {} |
57 |
< |
try { |
58 |
< |
q.iterator().next(); |
59 |
< |
shouldThrow(); |
60 |
< |
} catch (NoSuchElementException success) {} |
61 |
< |
try { |
62 |
< |
q.remove(); |
63 |
< |
shouldThrow(); |
64 |
< |
} catch (NoSuchElementException success) {} |
65 |
< |
} catch (InterruptedException ie) { |
66 |
< |
threadUnexpectedException(ie); |
67 |
< |
} |
47 |
> |
new Generic().testSuite(), |
48 |
> |
CollectionTest.testSuite(new Implementation())); |
49 |
|
} |
50 |
|
|
51 |
|
/** |
73 |
|
* NullPointerException |
74 |
|
*/ |
75 |
|
public void testConstructor3() { |
76 |
+ |
Collection<Integer> elements = Arrays.asList(new Integer[SIZE]); |
77 |
|
try { |
78 |
< |
Integer[] ints = new Integer[SIZE]; |
97 |
< |
new LinkedTransferQueue(Arrays.asList(ints)); |
78 |
> |
new LinkedTransferQueue(elements); |
79 |
|
shouldThrow(); |
80 |
|
} catch (NullPointerException success) {} |
81 |
|
} |
85 |
|
* throws NullPointerException |
86 |
|
*/ |
87 |
|
public void testConstructor4() { |
88 |
+ |
Integer[] ints = new Integer[SIZE]; |
89 |
+ |
for (int i = 0; i < SIZE - 1; ++i) |
90 |
+ |
ints[i] = i; |
91 |
+ |
Collection<Integer> elements = Arrays.asList(ints); |
92 |
|
try { |
93 |
< |
Integer[] ints = new Integer[SIZE]; |
109 |
< |
for (int i = 0; i < SIZE - 1; ++i) { |
110 |
< |
ints[i] = i; |
111 |
< |
} |
112 |
< |
new LinkedTransferQueue(Arrays.asList(ints)); |
93 |
> |
new LinkedTransferQueue(elements); |
94 |
|
shouldThrow(); |
95 |
|
} catch (NullPointerException success) {} |
96 |
|
} |
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 |
|
|
139 |
|
/** |
159 |
– |
* offer(null) throws NullPointerException |
160 |
– |
*/ |
161 |
– |
public void testOfferNull() { |
162 |
– |
try { |
163 |
– |
LinkedTransferQueue q = new LinkedTransferQueue(); |
164 |
– |
q.offer(null); |
165 |
– |
shouldThrow(); |
166 |
– |
} catch (NullPointerException success) {} |
167 |
– |
} |
168 |
– |
|
169 |
– |
/** |
170 |
– |
* add(null) throws NullPointerException |
171 |
– |
*/ |
172 |
– |
public void testAddNull() { |
173 |
– |
try { |
174 |
– |
LinkedTransferQueue q = new LinkedTransferQueue(); |
175 |
– |
q.add(null); |
176 |
– |
shouldThrow(); |
177 |
– |
} catch (NullPointerException success) {} |
178 |
– |
} |
179 |
– |
|
180 |
– |
/** |
181 |
– |
* addAll(null) throws NullPointerException |
182 |
– |
*/ |
183 |
– |
public void testAddAll1() { |
184 |
– |
try { |
185 |
– |
LinkedTransferQueue q = new LinkedTransferQueue(); |
186 |
– |
q.addAll(null); |
187 |
– |
shouldThrow(); |
188 |
– |
} catch (NullPointerException success) {} |
189 |
– |
} |
190 |
– |
|
191 |
– |
/** |
140 |
|
* addAll(this) throws IllegalArgumentException |
141 |
|
*/ |
142 |
|
public void testAddAllSelf() { |
143 |
+ |
LinkedTransferQueue q = populatedQueue(SIZE); |
144 |
|
try { |
196 |
– |
LinkedTransferQueue q = populatedQueue(SIZE); |
145 |
|
q.addAll(q); |
146 |
|
shouldThrow(); |
147 |
|
} catch (IllegalArgumentException success) {} |
148 |
|
} |
149 |
|
|
150 |
|
/** |
203 |
– |
* addAll of a collection with null elements throws NullPointerException |
204 |
– |
*/ |
205 |
– |
public void testAddAll2() { |
206 |
– |
try { |
207 |
– |
LinkedTransferQueue q = new LinkedTransferQueue(); |
208 |
– |
Integer[] ints = new Integer[SIZE]; |
209 |
– |
q.addAll(Arrays.asList(ints)); |
210 |
– |
shouldThrow(); |
211 |
– |
} catch (NullPointerException success) {} |
212 |
– |
} |
213 |
– |
|
214 |
– |
/** |
151 |
|
* addAll of a collection with any null elements throws |
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 { |
220 |
– |
LinkedTransferQueue q = new LinkedTransferQueue(); |
221 |
– |
Integer[] ints = new Integer[SIZE]; |
222 |
– |
for (int i = 0; i < SIZE - 1; ++i) { |
223 |
– |
ints[i] = i; |
224 |
– |
} |
160 |
|
q.addAll(Arrays.asList(ints)); |
161 |
|
shouldThrow(); |
162 |
|
} catch (NullPointerException success) {} |
180 |
|
} |
181 |
|
|
182 |
|
/** |
248 |
– |
* put(null) throws NullPointerException |
249 |
– |
*/ |
250 |
– |
public void testPutNull() throws InterruptedException { |
251 |
– |
try { |
252 |
– |
LinkedTransferQueue q = new LinkedTransferQueue(); |
253 |
– |
q.put(null); |
254 |
– |
shouldThrow(); |
255 |
– |
} catch (NullPointerException success) {} |
256 |
– |
} |
257 |
– |
|
258 |
– |
/** |
183 |
|
* all elements successfully put are contained |
184 |
|
*/ |
185 |
|
public void testPut() { |
186 |
|
LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
187 |
|
for (int i = 0; i < SIZE; ++i) { |
188 |
< |
assertEquals(q.size(), i); |
188 |
> |
assertEquals(i, q.size()); |
189 |
|
q.put(i); |
190 |
|
assertTrue(q.contains(i)); |
191 |
|
} |
205 |
|
* take removes existing elements until empty, then blocks interruptibly |
206 |
|
*/ |
207 |
|
public void testBlockingTake() throws InterruptedException { |
208 |
< |
final BlockingQueue<Integer> q = populatedQueue(SIZE); |
209 |
< |
final CountDownLatch aboutToWait = new CountDownLatch(1); |
208 |
> |
final BlockingQueue q = populatedQueue(SIZE); |
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) { |
213 |
< |
assertEquals(i, (int) q.take()); |
213 |
> |
assertEquals(i, q.take()); |
214 |
|
} |
215 |
< |
aboutToWait.countDown(); |
215 |
> |
|
216 |
> |
Thread.currentThread().interrupt(); |
217 |
> |
try { |
218 |
> |
q.take(); |
219 |
> |
shouldThrow(); |
220 |
> |
} catch (InterruptedException success) {} |
221 |
> |
assertFalse(Thread.interrupted()); |
222 |
> |
|
223 |
> |
pleaseInterrupt.countDown(); |
224 |
|
try { |
225 |
|
q.take(); |
226 |
|
shouldThrow(); |
227 |
|
} catch (InterruptedException success) {} |
228 |
+ |
assertFalse(Thread.interrupted()); |
229 |
|
}}); |
230 |
|
|
231 |
< |
aboutToWait.await(); |
232 |
< |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
231 |
> |
await(pleaseInterrupt); |
232 |
> |
assertThreadStaysAlive(t); |
233 |
|
t.interrupt(); |
234 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
302 |
< |
checkEmpty(q); |
234 |
> |
awaitTermination(t); |
235 |
|
} |
236 |
|
|
237 |
|
/** |
264 |
|
public void testTimedPoll() throws InterruptedException { |
265 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
266 |
|
for (int i = 0; i < SIZE; ++i) { |
267 |
< |
long t0 = System.nanoTime(); |
268 |
< |
assertEquals(i, (int) q.poll(SMALL_DELAY_MS, MILLISECONDS)); |
269 |
< |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
270 |
< |
} |
271 |
< |
long t0 = System.nanoTime(); |
272 |
< |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
273 |
< |
assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); |
267 |
> |
long startTime = System.nanoTime(); |
268 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
269 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
270 |
> |
} |
271 |
> |
long startTime = System.nanoTime(); |
272 |
> |
assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
273 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
274 |
|
checkEmpty(q); |
275 |
|
} |
276 |
|
|
376 |
|
} |
377 |
|
|
378 |
|
/** |
447 |
– |
* remove(x) removes x and returns true if present |
448 |
– |
*/ |
449 |
– |
public void testRemoveElement() throws InterruptedException { |
450 |
– |
LinkedTransferQueue q = populatedQueue(SIZE); |
451 |
– |
for (int i = 1; i < SIZE; i += 2) { |
452 |
– |
assertTrue(q.remove(i)); |
453 |
– |
} |
454 |
– |
for (int i = 0; i < SIZE; i += 2) { |
455 |
– |
assertTrue(q.remove(i)); |
456 |
– |
assertFalse(q.remove(i + 1)); |
457 |
– |
} |
458 |
– |
checkEmpty(q); |
459 |
– |
} |
460 |
– |
|
461 |
– |
/** |
379 |
|
* An add following remove(x) succeeds |
380 |
|
*/ |
381 |
|
public void testRemoveElementAndAdd() throws InterruptedException { |
491 |
|
} |
492 |
|
|
493 |
|
/** |
577 |
– |
* toArray(null) throws NullPointerException |
578 |
– |
*/ |
579 |
– |
public void testToArray_NullArg() { |
580 |
– |
LinkedTransferQueue q = populatedQueue(SIZE); |
581 |
– |
try { |
582 |
– |
q.toArray(null); |
583 |
– |
shouldThrow(); |
584 |
– |
} catch (NullPointerException success) {} |
585 |
– |
} |
586 |
– |
|
587 |
– |
/** |
494 |
|
* toArray(incompatible array type) throws ArrayStoreException |
495 |
|
*/ |
496 |
|
public void testToArray1_BadArg() { |
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 |
|
/** |
586 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
587 |
|
String s = q.toString(); |
588 |
|
for (int i = 0; i < SIZE; ++i) { |
589 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
589 |
> |
assertTrue(s.contains(String.valueOf(i))); |
590 |
|
} |
591 |
|
} |
592 |
|
|
595 |
|
*/ |
596 |
|
public void testOfferInExecutor() { |
597 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
598 |
< |
final CountDownLatch threadsStarted = new CountDownLatch(2); |
598 |
> |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
599 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
600 |
|
|
601 |
|
executor.execute(new CheckedRunnable() { |
602 |
|
public void realRun() throws InterruptedException { |
684 |
– |
threadsStarted.countDown(); |
603 |
|
threadsStarted.await(); |
604 |
< |
assertTrue(q.offer(one, MEDIUM_DELAY_MS, MILLISECONDS)); |
604 |
> |
assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS)); |
605 |
|
}}); |
606 |
|
|
607 |
|
executor.execute(new CheckedRunnable() { |
608 |
|
public void realRun() throws InterruptedException { |
691 |
– |
threadsStarted.countDown(); |
609 |
|
threadsStarted.await(); |
610 |
|
assertSame(one, q.take()); |
611 |
|
checkEmpty(q); |
619 |
|
*/ |
620 |
|
public void testPollInExecutor() { |
621 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
622 |
< |
final CountDownLatch threadsStarted = new CountDownLatch(2); |
622 |
> |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
623 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
624 |
|
|
625 |
|
executor.execute(new CheckedRunnable() { |
626 |
|
public void realRun() throws InterruptedException { |
627 |
|
assertNull(q.poll()); |
711 |
– |
threadsStarted.countDown(); |
628 |
|
threadsStarted.await(); |
629 |
< |
assertSame(one, q.poll(SMALL_DELAY_MS, MILLISECONDS)); |
629 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
630 |
|
checkEmpty(q); |
631 |
|
}}); |
632 |
|
|
633 |
|
executor.execute(new CheckedRunnable() { |
634 |
|
public void realRun() throws InterruptedException { |
719 |
– |
threadsStarted.countDown(); |
635 |
|
threadsStarted.await(); |
636 |
|
q.put(one); |
637 |
|
}}); |
643 |
|
* A deserialized serialized queue has same elements in same order |
644 |
|
*/ |
645 |
|
public void testSerialization() throws Exception { |
646 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
647 |
< |
|
733 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
734 |
< |
ObjectOutputStream out |
735 |
< |
= new ObjectOutputStream(new BufferedOutputStream(bout)); |
736 |
< |
out.writeObject(q); |
737 |
< |
out.close(); |
738 |
< |
|
739 |
< |
ByteArrayInputStream bin |
740 |
< |
= new ByteArrayInputStream(bout.toByteArray()); |
741 |
< |
ObjectInputStream in |
742 |
< |
= new ObjectInputStream(new BufferedInputStream(bin)); |
743 |
< |
LinkedTransferQueue r = (LinkedTransferQueue) in.readObject(); |
646 |
> |
Queue x = populatedQueue(SIZE); |
647 |
> |
Queue y = serialClone(x); |
648 |
|
|
649 |
< |
assertEquals(q.size(), r.size()); |
650 |
< |
assertEquals(q.toString(), r.toString()); |
651 |
< |
assertTrue(Arrays.equals(q.toArray(), r.toArray())); |
652 |
< |
while (!q.isEmpty()) { |
653 |
< |
assertEquals(q.remove(), r.remove()); |
649 |
> |
assertNotSame(y, x); |
650 |
> |
assertEquals(x.size(), y.size()); |
651 |
> |
assertEquals(x.toString(), y.toString()); |
652 |
> |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
653 |
> |
while (!x.isEmpty()) { |
654 |
> |
assertFalse(y.isEmpty()); |
655 |
> |
assertEquals(x.remove(), y.remove()); |
656 |
|
} |
657 |
< |
} |
752 |
< |
|
753 |
< |
/** |
754 |
< |
* drainTo(null) throws NullPointerException |
755 |
< |
*/ |
756 |
< |
public void testDrainToNull() { |
757 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
758 |
< |
try { |
759 |
< |
q.drainTo(null); |
760 |
< |
shouldThrow(); |
761 |
< |
} catch (NullPointerException success) {} |
762 |
< |
} |
763 |
< |
|
764 |
< |
/** |
765 |
< |
* drainTo(this) throws IllegalArgumentException |
766 |
< |
*/ |
767 |
< |
public void testDrainToSelf() { |
768 |
< |
LinkedTransferQueue q = populatedQueue(SIZE); |
769 |
< |
try { |
770 |
< |
q.drainTo(q); |
771 |
< |
shouldThrow(); |
772 |
< |
} catch (IllegalArgumentException success) {} |
657 |
> |
assertTrue(y.isEmpty()); |
658 |
|
} |
659 |
|
|
660 |
|
/** |
664 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
665 |
|
ArrayList l = new ArrayList(); |
666 |
|
q.drainTo(l); |
667 |
< |
assertEquals(q.size(), 0); |
668 |
< |
assertEquals(l.size(), SIZE); |
667 |
> |
assertEquals(0, q.size()); |
668 |
> |
assertEquals(SIZE, l.size()); |
669 |
|
for (int i = 0; i < SIZE; ++i) { |
670 |
< |
assertEquals(l.get(i), i); |
670 |
> |
assertEquals(i, l.get(i)); |
671 |
|
} |
672 |
|
q.add(zero); |
673 |
|
q.add(one); |
676 |
|
assertTrue(q.contains(one)); |
677 |
|
l.clear(); |
678 |
|
q.drainTo(l); |
679 |
< |
assertEquals(q.size(), 0); |
680 |
< |
assertEquals(l.size(), 2); |
679 |
> |
assertEquals(0, q.size()); |
680 |
> |
assertEquals(2, l.size()); |
681 |
|
for (int i = 0; i < 2; ++i) { |
682 |
< |
assertEquals(l.get(i), i); |
682 |
> |
assertEquals(i, l.get(i)); |
683 |
|
} |
684 |
|
} |
685 |
|
|
695 |
|
ArrayList l = new ArrayList(); |
696 |
|
q.drainTo(l); |
697 |
|
assertTrue(l.size() >= SIZE); |
698 |
< |
for (int i = 0; i < SIZE; ++i) { |
699 |
< |
assertEquals(l.get(i), i); |
815 |
< |
} |
698 |
> |
for (int i = 0; i < SIZE; ++i) |
699 |
> |
assertEquals(i, l.get(i)); |
700 |
|
awaitTermination(t, MEDIUM_DELAY_MS); |
701 |
|
assertTrue(q.size() + l.size() >= SIZE); |
702 |
|
} |
703 |
|
|
704 |
|
/** |
821 |
– |
* drainTo(null, n) throws NullPointerException |
822 |
– |
*/ |
823 |
– |
public void testDrainToNullN() { |
824 |
– |
LinkedTransferQueue q = populatedQueue(SIZE); |
825 |
– |
try { |
826 |
– |
q.drainTo(null, SIZE); |
827 |
– |
shouldThrow(); |
828 |
– |
} catch (NullPointerException success) {} |
829 |
– |
} |
830 |
– |
|
831 |
– |
/** |
832 |
– |
* drainTo(this, n) throws IllegalArgumentException |
833 |
– |
*/ |
834 |
– |
public void testDrainToSelfN() { |
835 |
– |
LinkedTransferQueue q = populatedQueue(SIZE); |
836 |
– |
try { |
837 |
– |
q.drainTo(q, SIZE); |
838 |
– |
shouldThrow(); |
839 |
– |
} catch (IllegalArgumentException success) {} |
840 |
– |
} |
841 |
– |
|
842 |
– |
/** |
705 |
|
* drainTo(c, n) empties first min(n, size) elements of queue into c |
706 |
|
*/ |
707 |
|
public void testDrainToN() { |
713 |
|
ArrayList l = new ArrayList(); |
714 |
|
q.drainTo(l, i); |
715 |
|
int k = (i < SIZE) ? i : SIZE; |
716 |
< |
assertEquals(l.size(), k); |
717 |
< |
assertEquals(q.size(), SIZE - k); |
718 |
< |
for (int j = 0; j < k; ++j) { |
719 |
< |
assertEquals(l.get(j), j); |
720 |
< |
} |
859 |
< |
while (q.poll() != null) |
860 |
< |
; |
716 |
> |
assertEquals(k, l.size()); |
717 |
> |
assertEquals(SIZE - k, q.size()); |
718 |
> |
for (int j = 0; j < k; ++j) |
719 |
> |
assertEquals(j, l.get(j)); |
720 |
> |
do {} while (q.poll() != null); |
721 |
|
} |
722 |
|
} |
723 |
|
|
727 |
|
*/ |
728 |
|
public void testWaitingConsumer() throws InterruptedException { |
729 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
730 |
< |
assertEquals(q.getWaitingConsumerCount(), 0); |
730 |
> |
assertEquals(0, q.getWaitingConsumerCount()); |
731 |
|
assertFalse(q.hasWaitingConsumer()); |
732 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
733 |
|
|
735 |
|
public void realRun() throws InterruptedException { |
736 |
|
threadStarted.countDown(); |
737 |
|
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
738 |
< |
assertEquals(q.getWaitingConsumerCount(), 0); |
738 |
> |
assertEquals(0, q.getWaitingConsumerCount()); |
739 |
|
assertFalse(q.hasWaitingConsumer()); |
740 |
|
}}); |
741 |
|
|
742 |
|
threadStarted.await(); |
743 |
|
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
744 |
< |
assertEquals(q.getWaitingConsumerCount(), 1); |
744 |
> |
assertEquals(1, q.getWaitingConsumerCount()); |
745 |
|
assertTrue(q.hasWaitingConsumer()); |
746 |
|
|
747 |
|
assertTrue(q.offer(one)); |
748 |
< |
assertEquals(q.getWaitingConsumerCount(), 0); |
748 |
> |
assertEquals(0, q.getWaitingConsumerCount()); |
749 |
|
assertFalse(q.hasWaitingConsumer()); |
750 |
|
|
751 |
|
awaitTermination(t, MEDIUM_DELAY_MS); |
869 |
|
* tryTransfer(null) throws NullPointerException |
870 |
|
*/ |
871 |
|
public void testTryTransfer1() { |
872 |
+ |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
873 |
|
try { |
1013 |
– |
final LinkedTransferQueue q = new LinkedTransferQueue(); |
874 |
|
q.tryTransfer(null); |
875 |
|
shouldThrow(); |
876 |
|
} catch (NullPointerException success) {} |
932 |
|
} |
933 |
|
|
934 |
|
/** |
935 |
< |
* tryTransfer waits the amount given if interrupted, and |
1076 |
< |
* throws interrupted exception |
935 |
> |
* tryTransfer blocks interruptibly if no takers |
936 |
|
*/ |
937 |
|
public void testTryTransfer5() throws InterruptedException { |
938 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
939 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
939 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
940 |
> |
assertTrue(q.isEmpty()); |
941 |
|
|
942 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
943 |
|
public void realRun() throws InterruptedException { |
944 |
< |
long t0 = System.nanoTime(); |
945 |
< |
threadStarted.countDown(); |
944 |
> |
Thread.currentThread().interrupt(); |
945 |
> |
try { |
946 |
> |
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
947 |
> |
shouldThrow(); |
948 |
> |
} catch (InterruptedException success) {} |
949 |
> |
assertFalse(Thread.interrupted()); |
950 |
> |
|
951 |
> |
pleaseInterrupt.countDown(); |
952 |
|
try { |
953 |
|
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
954 |
|
shouldThrow(); |
955 |
|
} catch (InterruptedException success) {} |
956 |
< |
assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); |
956 |
> |
assertFalse(Thread.interrupted()); |
957 |
|
}}); |
958 |
|
|
959 |
< |
threadStarted.await(); |
960 |
< |
Thread.sleep(SHORT_DELAY_MS); |
959 |
> |
await(pleaseInterrupt); |
960 |
> |
assertThreadStaysAlive(t); |
961 |
|
t.interrupt(); |
962 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
962 |
> |
awaitTermination(t); |
963 |
|
checkEmpty(q); |
964 |
|
} |
965 |
|
|
973 |
|
public void realRun() throws InterruptedException { |
974 |
|
long t0 = System.nanoTime(); |
975 |
|
assertFalse(q.tryTransfer(new Object(), |
976 |
< |
SHORT_DELAY_MS, MILLISECONDS)); |
977 |
< |
assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); |
976 |
> |
timeoutMillis(), MILLISECONDS)); |
977 |
> |
assertTrue(millisElapsedSince(t0) >= timeoutMillis()); |
978 |
|
checkEmpty(q); |
979 |
|
}}); |
980 |
|
|
981 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
981 |
> |
awaitTermination(t); |
982 |
|
checkEmpty(q); |
983 |
|
} |
984 |
|
|
1006 |
|
} |
1007 |
|
|
1008 |
|
/** |
1009 |
< |
* tryTransfer attempts to enqueue into the q and fails returning |
1010 |
< |
* false not enqueueing and the successive poll is null |
1009 |
> |
* tryTransfer attempts to enqueue into the queue and fails |
1010 |
> |
* returning false not enqueueing and the successive poll is null |
1011 |
|
*/ |
1012 |
|
public void testTryTransfer8() throws InterruptedException { |
1013 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1014 |
|
assertTrue(q.offer(four)); |
1015 |
|
assertEquals(1, q.size()); |
1016 |
|
long t0 = System.nanoTime(); |
1017 |
< |
assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS)); |
1018 |
< |
assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); |
1017 |
> |
assertFalse(q.tryTransfer(five, timeoutMillis(), MILLISECONDS)); |
1018 |
> |
assertTrue(millisElapsedSince(t0) >= timeoutMillis()); |
1019 |
|
assertEquals(1, q.size()); |
1020 |
|
assertSame(four, q.poll()); |
1021 |
|
assertNull(q.poll()); |
1033 |
|
assertFalse(q.isEmpty()); |
1034 |
|
return q; |
1035 |
|
} |
1036 |
+ |
|
1037 |
+ |
/** |
1038 |
+ |
* remove(null), contains(null) always return false |
1039 |
+ |
*/ |
1040 |
+ |
public void testNeverContainsNull() { |
1041 |
+ |
Collection<?>[] qs = { |
1042 |
+ |
new LinkedTransferQueue<Object>(), |
1043 |
+ |
populatedQueue(2), |
1044 |
+ |
}; |
1045 |
+ |
|
1046 |
+ |
for (Collection<?> q : qs) { |
1047 |
+ |
assertFalse(q.contains(null)); |
1048 |
+ |
assertFalse(q.remove(null)); |
1049 |
+ |
} |
1050 |
+ |
} |
1051 |
|
} |